运行 docker 在 Kubernetes 中使用不同命令的容器
Run docker container with a different command in Kubernetes
我想首先从 GitLab 执行 app:rake db:setup
以便可以初始化数据库。
来自我的 GitLab YAML 的片段 (init-db.yaml)
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- app:rake db:setup
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
但是当我检查 pod 的状态时,我收到以下错误消息:
...
Command:
app:rake db:setup
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: ContainerCannotRun
Message: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\"app:rake db:setup\\": executable file not found in $PATH\"\n"
...
来自 Dockerfile
的片段
...
...
EXPOSE 22/tcp 80/tcp 443/tcp
VOLUME ["${GITLAB_DATA_DIR}", "${GITLAB_LOG_DIR}"]
WORKDIR ${GITLAB_INSTALL_DIR}
ENTRYPOINT ["/sbin/entrypoint.sh"]
CMD ["app:start"]
的片段
...
...
case in
app:init|app:start|app:sanitize|app:rake)
...
更新:
当我 运行 这个:
command:
- "app:rake"
- "db:setup"
我得到:
to open log file "/var/log/pods/5b604971-b5ac-11e7-9ee5-021bfed3b32a/gitlab_0.log": open /var/log/pods/5b604971-b5ac-11e7-9ee5-021bfed3b32a/gitlab_0.log: no such file or directory
当我尝试这个时:
command:
- "/bin/sh"
args:
- "-c"
- "app:rake db:setup"
我得到:/bin/sh: 1: app:rake: not found
此 link 描述了 运行ning app:rake db:setup
设置数据库的命令。
我之前在 Kubernetes 之外尝试过 运行 执行此命令,使用下面显示的代码它运行良好:
docker run --name gitlab -it --rm \
--link gitlab-postgresql:postgresql --link gitlab-redis:redisio \
--publish 443:443 --publish 80:80 \
--env 'GITLAB_PORT=80' \
--env 'GITLAB_SECRETS_DB_KEY_BASE=64_bit_key_A' \
--env 'GITLAB_SECRETS_SECRET_KEY_BASE=64_bit_key_B' \
--env 'GITLAB_SECRETS_OTP_KEY_BASE=64_bit_key_C' \
--volume /srv/docker/gitlab/gitlab:/home/git/data \
sameersbn/gitlab:9.3.9 app:rake db:setup
UPDATE_1:
$ kubectl describe pod gitlab-1108406018-3lvh7
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 18m default-scheduler Successfully assigned gitlab-1108406018-3lvh7 to rancher-a
Normal SuccessfulMountVolume 18m kubelet, rancher-a MountVolume.SetUp succeeded for volume "default-token-qv8dm"
Normal SuccessfulMountVolume 18m kubelet, rancher-a MountVolume.SetUp succeeded for volume "pvc-304c48a5-b430-11e7-9ee5-021bfed3b32a"
Normal Pulling 17m kubelet, rancher-a pulling image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9"
Normal Pulled 16m kubelet, rancher-a Successfully pulled image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9"
Normal Pulled 2m (x7 over 16m) kubelet, rancher-a Container image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9" already present on machine
Normal Created 2m (x8 over 16m) kubelet, rancher-a Created container
Normal Started 2m (x8 over 16m) kubelet, rancher-a Started container
Warning BackOff 8s (x56 over 15m) kubelet, rancher-a Back-off restarting failed container
Warning FailedSync 8s (x56 over 15m) kubelet, rancher-a Error syncing pod
更新: 这不是正确的答案。请参考Andy Shinn的回答。
每个命令都应该是命令数组中的一个项目。因为你想执行两个命令,试试这个:
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "app:rake"
- "db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
您还可以使用 args
参数在一行中写入 app:rake db:setup
命令对(基本上是在 shell 上下文中将命令强制为 运行) :
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "/bin/sh"
args:
- "-c"
- "app:rake db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
我认为这可能只是 Pod 规范的混淆。在 Kubernetes 中,command
相当于 Docker ENTRYPOINT
,args
相当于 CMD
。在你的情况下,我想你想要:
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "/sbin/entrypoint.sh"
args:
- "app:rake"
- "db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
我想首先从 GitLab 执行 app:rake db:setup
以便可以初始化数据库。
来自我的 GitLab YAML 的片段 (init-db.yaml)
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- app:rake db:setup
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
但是当我检查 pod 的状态时,我收到以下错误消息:
...
Command:
app:rake db:setup
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: ContainerCannotRun
Message: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\"app:rake db:setup\\": executable file not found in $PATH\"\n"
...
来自 Dockerfile
的片段...
...
EXPOSE 22/tcp 80/tcp 443/tcp
VOLUME ["${GITLAB_DATA_DIR}", "${GITLAB_LOG_DIR}"]
WORKDIR ${GITLAB_INSTALL_DIR}
ENTRYPOINT ["/sbin/entrypoint.sh"]
CMD ["app:start"]
的片段
...
...
case in
app:init|app:start|app:sanitize|app:rake)
...
更新: 当我 运行 这个:
command:
- "app:rake"
- "db:setup"
我得到:
to open log file "/var/log/pods/5b604971-b5ac-11e7-9ee5-021bfed3b32a/gitlab_0.log": open /var/log/pods/5b604971-b5ac-11e7-9ee5-021bfed3b32a/gitlab_0.log: no such file or directory
当我尝试这个时:
command:
- "/bin/sh"
args:
- "-c"
- "app:rake db:setup"
我得到:/bin/sh: 1: app:rake: not found
此 link 描述了 运行ning app:rake db:setup
设置数据库的命令。
我之前在 Kubernetes 之外尝试过 运行 执行此命令,使用下面显示的代码它运行良好:
docker run --name gitlab -it --rm \
--link gitlab-postgresql:postgresql --link gitlab-redis:redisio \
--publish 443:443 --publish 80:80 \
--env 'GITLAB_PORT=80' \
--env 'GITLAB_SECRETS_DB_KEY_BASE=64_bit_key_A' \
--env 'GITLAB_SECRETS_SECRET_KEY_BASE=64_bit_key_B' \
--env 'GITLAB_SECRETS_OTP_KEY_BASE=64_bit_key_C' \
--volume /srv/docker/gitlab/gitlab:/home/git/data \
sameersbn/gitlab:9.3.9 app:rake db:setup
UPDATE_1:
$ kubectl describe pod gitlab-1108406018-3lvh7
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 18m default-scheduler Successfully assigned gitlab-1108406018-3lvh7 to rancher-a
Normal SuccessfulMountVolume 18m kubelet, rancher-a MountVolume.SetUp succeeded for volume "default-token-qv8dm"
Normal SuccessfulMountVolume 18m kubelet, rancher-a MountVolume.SetUp succeeded for volume "pvc-304c48a5-b430-11e7-9ee5-021bfed3b32a"
Normal Pulling 17m kubelet, rancher-a pulling image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9"
Normal Pulled 16m kubelet, rancher-a Successfully pulled image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9"
Normal Pulled 2m (x7 over 16m) kubelet, rancher-a Container image "docker.artifactory.abc.net/sameersbn/gitlab:9.3.9" already present on machine
Normal Created 2m (x8 over 16m) kubelet, rancher-a Created container
Normal Started 2m (x8 over 16m) kubelet, rancher-a Started container
Warning BackOff 8s (x56 over 15m) kubelet, rancher-a Back-off restarting failed container
Warning FailedSync 8s (x56 over 15m) kubelet, rancher-a Error syncing pod
更新: 这不是正确的答案。请参考Andy Shinn的回答。
每个命令都应该是命令数组中的一个项目。因为你想执行两个命令,试试这个:
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "app:rake"
- "db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
您还可以使用 args
参数在一行中写入 app:rake db:setup
命令对(基本上是在 shell 上下文中将命令强制为 运行) :
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "/bin/sh"
args:
- "-c"
- "app:rake db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...
我认为这可能只是 Pod 规范的混淆。在 Kubernetes 中,command
相当于 Docker ENTRYPOINT
,args
相当于 CMD
。在你的情况下,我想你想要:
...
name: gitlab
image: docker.artifactory.abc.net/sameersbn/gitlab:9.3.9
command:
- "/sbin/entrypoint.sh"
args:
- "app:rake"
- "db:setup"
volumeMounts:
- name: gfs-vol-gitlab
mountPath: /home/git/data
ports:
- containerPort: 443
resources: {}
volumes:
...