Kubernetes init containers

2 min read

Kubernetes init containers are a special container that runs before the main containers on the Pod. They are usually used used for setting up the environment and populate some shared storage to be used for the actual containers.

We can add them using spec.initContainers on the same way we use spec.containers:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  initContainers:
  - image: my-init-image
    name: my-init
  containers:
  - image: my-app-image
    name: my-app
  - image: my-other-app-image
    name: my-other-app

We must take into account that:

  • Init containers are not expected to keep running, they must finish it's task.
  • Each init container must complete successfully before the next one starts.

If we use --watch with kubectl get pods we can see how this works:

$ kubectl get pods --watch
NAME                          READY   STATUS            RESTARTS   AGE
pet2cattle-8475d6697-jbmsm    0/1     Pending           0          0s
pet2cattle-8475d6697-jbmsm    0/1     Pending           0          0s
pet2cattle-8475d6697-jbmsm    0/1     Init:0/4          0          0s
pet2cattle-8475d6697-2sk4m    0/1     Init:1/4          0          4s
pet2cattle-8475d6697-2sk4m    0/1     Init:2/4          0          5s
pet2cattle-8475d6697-2sk4m    0/1     Init:2/4          0          6s
pet2cattle-8475d6697-2sk4m    0/1     Init:3/4          0          7s
pet2cattle-8475d6697-2sk4m    0/1     PodInitializing   0          8s
pet2cattle-8475d6697-2sk4m    0/1     Running           0          9s
pet2cattle-8475d6697-2sk4m    0/1     Running           0          41s
pet2cattle-8475d6697-2sk4m    1/1     Running           0          2m20s

Posted on 21/06/2021