Gitlab CI 无法解析 kubernetes runner 上的服务主机名

Gitlab CI cannot resolve service hostname on kubernetes runner

我在 Kubernetes 运行 上有一个配置为 运行 的 GitLab CI 管道。在我尝试为测试工作添加服务 (https://docs.gitlab.com/ee/ci/services/mysql.html) 之前,一切都很好。无法在 kubernetes 上解析服务主机名(例如:mysql),导致出现以下错误 dial tcp: lookup mysql on 10.96.0.10:53: no such host。但是,它适用于 docker 运行ner,但这不是我想要的。有什么办法可以

工作定义来自 .gitlab-ci.yml

test:
    stage: test
    variables:
        MYSQL_ROOT_PASSWORD: --top-secret--
        MYSQL_DATABASE: --top-secret--
        MYSQL_USER: --top-secret--
        MYSQL_PASSWORD: --top-secret--
    services:
      - mysql:latest
      - nats:latest
    script:
        - ping -c 2 mysql
        - go test -cover -coverprofile=coverage.prof.tmp ./...

编辑:

来自 runner-jd6sxcl7-project-430-concurrent-0g5bm8 pod 的日志显示服务已启动。 pod 内总共有 4 个容器:build,helper,svc-0 (mysql), svc-1 (nats)

svc-0 日志显示 mysql 服务已成功启动:

2019-12-09 21:52:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2019-12-09 21:52:07+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2019-12-09 21:52:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2019-12-09 21:52:08+00:00 [Note] [Entrypoint]: Initializing database files
2019-12-09T21:52:08.226747Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-12-09T21:52:08.233097Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 46

svc-1 日志显示 nats 服务也已成功启动:

[1] 2019/12/09 21:52:12.876121 [INF] Starting nats-server version 2.1.2
[1] 2019/12/09 21:52:12.876193 [INF] Git commit [679beda]
[1] 2019/12/09 21:52:12.876393 [INF] Starting http monitor on 0.0.0.0:8222
[1] 2019/12/09 21:52:12.876522 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2019/12/09 21:52:12.876548 [INF] Server id is NCPAQNFKKWPI67DZHSWN5EWOCQSRACFG2FXNGTLMW2NNRBAMLSDY4IYQ
[1] 2019/12/09 21:52:12.876552 [INF] Server is ready
[1] 2019/12/09 21:52:12.876881 [INF] Listening for route connections on 0.0.0.0:6222

根据您提供的文档 link,我看到 mysql 主机名应该可以访问。

How services are linked to the job

To better understand how the container linking works, read Linking containers together.

To summarize, if you add mysql as service to your application, the image will then be used to create a container that is linked to the job container.

The service container for MySQL will be accessible under the hostname mysql. So, in order to access your database service you have to connect to the host named mysql instead of a socket or localhost. Read more in accessing the services.

也来自文档

If you don’t specify a service alias, when the job is run, service will be started and you will have access to it from your build container

那你能不能看看mysql服务启动的时候有没有报错

我提供了这个作为答案,因为我无法将其放入评论中。

这是旧版本 GitLab Runner (< 12.8) 或旧版本 Kubernetes (< 1.7) 中的 运行 Kubernetes 执行程序的已知问题。

来自Kubernetes executor documentation of GitLab Runner

  • Since GitLab Runner 12.8 and Kubernetes 1.7, the services are accessible via their DNS names. If you are using an older version you will have to use localhost.

(强调我的)

重要的是要记住 Kubernetes 执行器的其他限制和含义。来自同一文档:

Note that when services and containers are running in the same Kubernetes pod, they are all sharing the same localhost address.

因此,即使您能够使用特定于服务的主机名来与您的服务通信,它实际上都在 localhost (127.0.0.1) 下面。

因此,请记住同一文档中的其他重要限制:

  • You cannot use several services using the same port

(感谢 @user3154003 for the link to the GitLab Runner issue in a currently deleted answer 为我指明了这个答案的正确方向。)