在Kubernetes中使用不同的命令运行docker容器

时间:2017-10-20 14:45:37

标签: docker kubernetes gitlab gitlab-omnibus

我想最初从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"]

来自entrypoint.sh

的摘录
...
...
case ${1} 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描述了运行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

2 个答案:

答案 0 :(得分:3)

我认为这可能只是Pod规范的混合。在Kubernetes中,command相当于Docker ENTRYPOINTargs相当于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:
...

答案 1 :(得分:0)

更新:这不是正确的答案。请参考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:
...