我使用 Kubernetes 来设置我的 Gitlab 实例来进行部署,并使用 Kubernetes 集群来执行 CI。 Kubernetes是由Gitlab管理的,所以我自己从来没有接触过kubernetes。 Gitlab 安装了四个可用的软件包:Helm Tiller Ingress、Cert-Manager、Prometheus 和 GitLab Runner。
我使用以下教程在准系统服务器上安装了 Kubernetes 集群:https://vitux.com/install-and-deploy-kubernetes-on-ubuntu/ 。服务器的操作系统是Ubuntu 18.04minimal。我发现Gitlab无法在1.16版本的Kubernetes上安装Helm Tiller,所以我在服务器上安装了1.15.5-00版本的Kubernetes。
问题:
我有一个项目,我想在其中构建一个 Docker 镜像。我尝试使用 dind 服务通过部署在 kubernetes 平台上的 gitlab runner 来构建 docker 镜像。
构建过程失败并显示以下输出:
Running with gitlab-runner 12.1.0 (de7731dd)
on runner-gitlab-runner-699dc9bcc8-sgmcw -YPHFGCL
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image docker:stable ...
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Running on runner--yphfgcl-project-97-concurrent-0qj6sn via runner-gitlab-runner-699dc9bcc8-sgmcw...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/sadion/ci-test/.git/
Created fresh repository.
From https://git.sadion.net/sadion/ci-test
* [new branch] master -> origin/master
Checking out d179001c as master...
Skipping Git submodules setup
$ docker --version
Docker version 19.03.4, build 9013bf583a
$ docker build -t $TEST_NAME .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ERROR: Job failed: command terminated with exit code 1
源文件:
我使用的 Dockerfile 非常简单而且有效,因为我能够在本地计算机上构建镜像:
FROM httpd:2.4
COPY ./index.html /usr/local/apache2/htdocs/
我使用的 .gitlab-ci.yml
文件是:
image: docker:stable
variables:
TEST_NAME: local/test
services:
- docker:dind
stages:
- build
before_script:
- docker info
build_docker_image:
stage: build
before_script:
- docker --version
script:
- docker build -t $TEST_NAME .
tags:
- build
- kubernetes
尝试导出 DOCKER_HOST
我还尝试导出 DOCKER_HOST
变量。但配置后我得到了同样的错误:
image: docker:stable
variables:
TEST_NAME: local/test
DOCKER_HOST: tcp://localhost:2375
services:
- docker:dind
stages:
- build
before_script:
- docker info
build_docker_image:
stage: build
before_script:
- docker --version
script:
- docker build -t $TEST_NAME .
tags:
- build
- kubernetes
运行者的输出是:
Running with gitlab-runner 12.1.0 (de7731dd)
on runner-gitlab-runner-699dc9bcc8-sgmcw -YPHFGCL
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image docker:stable ...
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Running on runner--yphfgcl-project-97-concurrent-042nmk via runner-gitlab-runner-699dc9bcc8-sgmcw...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/sadion/ci-test/.git/
Created fresh repository.
From https://git.sadion.net/sadion/ci-test
* [new branch] master -> origin/master
Checking out 57b6be1d as master...
Skipping Git submodules setup
$ docker --version
Docker version 19.03.4, build 9013bf583a
$ docker build -t $TEST_NAME .
Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?
ERROR: Job failed: command terminated with exit code 1
最佳答案
所以我找到了解决方案。我认为问题是我没有指定证书目录。通过将目录更改为""
,它开始工作。
image: docker:stable
variables:
TEST_NAME: local/test
DOCKER_HOST: tcp://localhost:2375
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
before_script:
- docker info
build_docker_image:
stage: build
script:
- docker build -t $TEST_NAME .
tags:
- build
- kubernetes
- test
关于docker - 无法使用 Kubernetes 执行器访问 gitlab runner 中的 docker 守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847455/