How to create init container in kubernetes Jobs?

4/22/2020

Used below job.yaml for creating job. Init containers are not getting created.

[root@app]# kubectl version Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.5", GitCommit:"", GitTreeState:"clean", BuildDate:"2019-10-15T19:16:51Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.5", GitCommit:"", GitTreeState:"clean", BuildDate:"2019-10-15T19:07:57Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

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
-- Rajen Patel
kubernetes

2 Answers

4/23/2020

This annotation is not supported since kubernetes version 1.8.

In Kubernetes v1.8 Release notes you can read:

The deprecated alpha and beta initContainer annotations are no longer supported. Init containers must be specified using the initContainers field in the pod spec.

Please refer to up-to-date documentation about initContainers

-- HelloWorld
Source: StackOverflow

4/23/2020

InitContainers are set in the pod spec. Not the metadata. The Pod Spec is the same for Jobs, Deployments, or anything that creates pods.

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]
      - name: init-mydb
        image: busybox:1.28
        command: ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"]
      restartPolicy: Never
-- joshkurz
Source: StackOverflow