【问题标题】:kubernetes jobs init containerKubernetes 作业初始化容器
【发布时间】:2017-10-19 10:56:19
【问题描述】:

我创建了一个带有 2 个 INIT 容器的 JOB pod 在创建 pod 时,我的工作成功完成,但没有 init 容器的迹象

对我来说,工作应该在开始之前等待 2 个 init 容器完成

我的工作蓝图(取自 Kubernetes 文档)以防您想重现问题

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
annotations:
  pod.beta.kubernetes.io/init-containers: '[
    {
      "name": "init-myservice",
      "image": "busybox",
      "command": ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]
    },
    {
      "name": "init-mydb",
      "image": "busybox",
      "command": ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"]
    }
    ]'
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

在检查我的工作 pod 时

$ kubectl describe pod pi-v2dn9
Name: pi-v2dn9
Namespace: default
Security Policy: anyuid
Node: 192.168.111.4/192.168.111.4
Start Time: Thu, 19 Oct 2017 08:58:39 +0000
Labels: controller-uid=b3091c77-b4ab-11e7-a3ea-fa163ea1c70b
job-name=pi
Status: Succeeded
IP: 10.131.0.46
Controllers: Job/pi
Containers:
pi:
Container ID: docker://4bc5bb4c9fc65c1aa1999c3bdc09b01e54043dcdd464410edd0c9cad334c9c67
Image: perl
Image ID: docker-pullable://docker.io/perl@sha256:80bd8136a0f3e2c7d202236fc5d8f1192dbfa9ec661ecdd5e96a446e9c7913a8
Port:
Command:
perl
-Mbignum=bpi
-wle
print bpi(2000)
State: Terminated
Reason: Completed
Exit Code: 0
Started: Thu, 19 Oct 2017 08:58:53 +0000
Finished: Thu, 19 Oct 2017 08:58:58 +0000
Ready: False
Restart Count: 0
Volume Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-fxdf1 (ro)
Environment Variables: 
Conditions:
Type Status
Initialized True
Ready False
PodScheduled True
Volumes:
default-token-fxdf1:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-fxdf1
QoS Class: BestEffort
Tolerations: 
Events:
FirstSeen LastSeen Count From SubObjectPath Type Reason Message

26s 26s 1 {default-scheduler } Normal Scheduled Successfully assigned pi-v2dn9 to 192.168.111.4
25s 25s 1 {kubelet 192.168.111.4} spec.containers{pi} Normal Pulling pulling image "perl"
12s 12s 1 {kubelet 192.168.111.4} spec.containers{pi} Normal Pulled Successfully pulled image "perl"
12s 12s 1 {kubelet 192.168.111.4} spec.containers{pi} Normal Created Created container with docker id 4bc5bb4c9fc6; Security:[seccomp=unconfined]
12s 12s 1 {kubelet 192.168.111.4} spec.containers{pi} Normal Started Started container with docker id 4bc5bb4c9fc6

没有初始化容器的迹象!!!

我的环境: --- Kubernetes 版本(使用 kubectl 版本): 客户端版本:version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2+43a9be4", GitCommit:"43a9be4", GitTreeState:"clean", BuildDate:"2017-04-20T15: 38:11Z”,GoVersion:“go1.7.4”,编译器:“gc”,平台:“linux/amd64”} 服务器版本:version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2+43a9be4", GitCommit:"43a9be4", GitTreeState:"clean", BuildDate:"2017-04-20T15: 38:11Z”,GoVersion:“go1.7.4”,编译器:“gc”,平台:“linux/amd64”}

我在 OpenStack 的集群中工作

操作系统是 Red Hat Enterprise Linux Server 7.3 (Maipo)

提前感谢您的帮助。

【问题讨论】:

    标签: kubernetes containers jobs init kubernetes-jobs


    【解决方案1】:

    您的注释在错误的位置。特别是对于 init 容器,它们应该在 pod 元数据中定义,而您已将其添加到作业元数据中。

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      template:
        metadata:
          name: pi
          annotations:
            pod.beta.kubernetes.io/init-containers: '[
              {
                "name": "init-myservice",
                "image": "busybox",
                "command": ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]
              },
              {
                "name": "init-mydb",
                "image": "busybox",
                "command": ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"]
              }
            ]'
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never
    

    【讨论】:

    • 大家好,感谢您的所有回答。注释在错误的位置是对的(但对于 Kube 1.5 来说不是,因为它们必须像我一样放置)。真正的问题是 JOB 的 INIT 容器在 Kube 1.6 之前不起作用(我尝试使用这个版本和 1.7 并且它起作用了)。
    【解决方案2】:

    尝试将注释添加到规范模板而不是 Job 对象:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      template:
        metadata:
          name: pi
        annotations:
          pod.beta.kubernetes.io/init-containers: '[
            {
              "name": "init-myservice",
              "image": "busybox",
              "command": ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]
            },
            {
              "name": "init-mydb",
              "image": "busybox",
              "command": ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"]
            }
            ]'
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never
    

    【讨论】:

    • 您好 Vascop,感谢您的关注。
    • 不幸的是,我已经尝试过了,但恐怕在 Kube 1.5 中,注释字段必须添加到作业的元数据部分。我在文档v1-5.docs.kubernetes.io/docs/concepts/workloads/pods/… 中看到了这一点。我也尝试修改 API 版本,但作业需要批处理/v1,它可能不适合 init 容器。再次感谢你的帮助。问候,PP
    • Vascop 提出的建议至少适用于 kubernetes v1.7 示例中的缩进是错误的,但建议的解决方案是可以的。
    【解决方案3】:

    作业中的初始化容器在 1.6 版本之前不起作用。您必须升级到 1.6+ 版本才能使用它

    【讨论】:

      猜你喜欢
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2017-07-14
      • 2019-10-20
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多