Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create init container in kubernetes Jobs?

Tags:

kubernetes

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
like image 814
Rajen Patel Avatar asked Jan 24 '26 01:01

Rajen Patel


1 Answers

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
like image 150
joshkurz Avatar answered Jan 25 '26 23:01

joshkurz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!