Gitlab中配置Redis主从架构CI(跨服务通信)

Configuring Redis master-slave architecture in Gitlab CI (cross-service comunication)

我正在尝试设置仅包含 2 个服务的简单 redis 配置:master 和 slave。

这是我的 .gitlab-ci.yml 主从设置的一部分:

integration:
  extends: .test
  variables:
    FF_NETWORK_PER_BUILD: "true"
  services:
    - name: redis:latest
      alias: "redis-master"
      command: [ "redis-server", "--port", "6379" ]
    - name: redis:latest
      alias: "redis-slave"
      command: [ "redis-server", "--slaveof", "slave-master", "6379" ]
script:
    - nc -z -v -w30 -n redis-master 6379 // fails to resolve hostname redis-master ...
    // wait for readiness of both databases
    // run tests

出于隐私原因进行了简化

因为我需要在这些服务之间进行通信,所以我知道我需要使用名为 FF_NETWORK_PER_BUILD 的功能标志,其中:

create and use new user-defined Docker brdge network per build.

上述文件还指出

Both the build job container, and the service container(s) will be able to resolve each other’s hostnames (and aliases).

当涉及到 redis-masterredis-slave 之间的连接时,一切都按预期工作,slave 与 master 正确同步,但是当我尝试在脚本部分使用他们的别名与他们联系,结果出错:

Could not resolve hostname "redis-master": Name or service not known. QUITTING.

所以问题是为什么我不能在脚本部分使用别名?如果不可能,我如何获取他们的 IP,以便在他们准备好连接时我可以对他们执行 ping 操作?

旁注:

当我不使用 FF_NETWORK_PER_BUILD 标志别名时更有趣的是在脚本部分工作正常(但同时 redis-slave 无法与 redis-master 通信,所以这就是为什么我不能忽略这个标志。

在您的 nc 命令中,您使用的是 -n 选项,即每个人执行以下操作

-n Do not do any DNS or service lookups on any specified addresses, hostnames or ports.

所以基本上你是在关闭你的 dns 搜索,这里有一个小测试来说明:

$ nc -z google.com 80; echo $?
0
$ nc -n -z google.com 80; echo $?
nc: getaddrinfo for host "google.com" port 80: Name or service not known
1

删除 -n 应该可以修复您的管道