Wednesday 22 July 2020

Kubernetes: Init Containers

Init Container is an additional container in a Pod and is responsible to perform some initialization tasks before the regular container started.

 

Regular container will not start until the init container completes its task. By any chance, init container failed to start, regular container will not start.

 

initContainerDemo.yml
apiVersion: v1
kind: Pod
metadata:
  name: init-example-demo
  labels:
    app: hello-world
    author: krishna
    serviceType: user-service
spec:
  initContainers:
  - name: setup-service
    image: busybox
    command: ["/bin/sh"]
    args: ["-c", "sleep 10; echo 'Environment Setup is done';"]

  containers:
    - name: user-service
      image: jboss/wildfly

Let’s create a Pod using the above definition file.

$kubectl create -f initContainerDemo.yml 
pod/init-example-demo created

Let’s query pods.

$kubectl get pods
NAME                READY   STATUS     RESTARTS   AGE
init-example-demo   0/1     Init:0/1   0          6s

As you see the pod is in Init state. It is in the process of running the init container. Regular containers will not start until the init container completes successfully.

 

Wait for 10 seconds and query pods again.

$kubectl get pods
NAME                READY   STATUS    RESTARTS   AGE
init-example-demo   1/1     Running   0          42s

From the output, you can confirm that the regular container is in Running state.

 

Note

You can have more than one init-container in a Pod.


Previous                                                    Next                                                    Home



No comments:

Post a Comment