2

I've created a JOB pod with 2 INIT containers At pod creation, my job completed successfully but no sign of the init containers

To me, the job should have wait for the completion of the 2 init containers before starting

My job blueprint (taken from Kubernetes documentation) in case you'd like to reproduce the problem

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

When checking my job 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

NO SIGN OF THE INIT CONTAINERS !!!

My Environment: --- Kubernetes version (use kubectl version): Client Version: 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", Compiler:"gc", Platform:"linux/amd64"} Server Version: 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", Compiler:"gc", Platform:"linux/amd64"}

I'm working in a Cluster from OpenStack

OS is Red Hat Enterprise Linux Server 7.3 (Maipo)

Thanks in advance for any help.

Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
Paul Pozicheun
  • 29
  • 1
  • 1
  • 2

3 Answers3

2

Try adding the annotation to the spec template rather than the Job object:

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
  • 4,972
  • 4
  • 37
  • 50
  • Hi Vascop, Thanks for your interest. – Paul Pozicheun Oct 19 '17 at 11:55
  • Unfortunately, I've already tried this but I'm afraid that with Kube 1.5 the annotation field must be added in the metadata part of the job. I've seen that in documentation https://v1-5.docs.kubernetes.io/docs/concepts/workloads/pods/init-containers/. I've also tried to modify the API version, but jobs need the batch/v1 and it may not fit with init containers. Thanks again for your help. Regards, PP – Paul Pozicheun Oct 19 '17 at 11:56
  • 1
    What is proposed by Vascop is working at least for kubernetes v1.7 The indentation inside the example is wrong but the proposed solution is ok. – dbenque Oct 19 '17 at 13:56
2

Your annotations are in the wrong place. Specifically for init containers, they should be defined in the pod metadata, whereas you have added it to the job metadata.

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
camh
  • 40,988
  • 13
  • 62
  • 70
  • Hi everybody and thank you for all you answers. You are right that the annotations are in the wrong place (but not for Kube 1.5 as they must be placed as I did). Real problem is that INIT containers for JOB don't work before Kube 1.6 (I tried with this version and 1.7 and it worked). – Paul Pozicheun Oct 30 '17 at 09:35
1

Init containers in jobs don't work before 1.6 version. You will have to upgrade to 1.6+ version to make it work

Vikas Tawniya
  • 1,323
  • 1
  • 13
  • 22