Init Containers

The init containers option is available in Kubernetes environments used to run additional containers at startup that help initialize an application. Once the init containers have completed their initialization tasks, they terminate but leave the application container(s) running. For the App Server Agent installation, init containers are used as a delivery mechanism to copy the agent files into the application container at deploy time.

Overview on Kubernetes init containers

  • An init container is an additional container in a Pod that completes a task before the regular container is started
  • Init containers always run to completion.
  • Each init container must complete successfully before the next one starts.
  • The regular container will only be started once the init container has been started
  • An init container in a Pod must run and complete before any other application containers in the Pod started.
  • This is a great way to initialize a Kubernetes pod. You can pull any files, configurations, and so on with an init container.
  • Just as with any other application container, we can have more than one init container in a given Pod; but unlike an application container, each init container must run to completion before the next init container starts.

Differences from regular containers Official Docs

  • Init containers support all the fields and features of app containers, including resource limits, volumes, and security settings.
  • However, the resource requests and limits for an init container are handled differently, as documented in Resources.
  • init containers do not support lifecycle, livenessProbe, readinessProbe, or startupProbe because they must run to completion before the Pod can be ready.
  • If you specify multiple init containers for a Pod, kubelet runs each init container sequentially. Each init container must succeed before the next can run. When all of the init containers have run to completion, kubelet initializes the application containers for the Pod and runs them as usual.

Create a Pod with initContainers

vim pod-init-container.yml
apiVersion: v1
kind: Pod
metadata:
  name: myfirstpod
  labels:
  label1: harshal
  label2: gaurav
  label3: saurav
spec:
  containers:
    - name: firstcontainer
      image: coolgourav147/nginx-custom
      env:
        - name: myname
          value: Gaurav
        - name: city
          value: Jaipur
  initContainers:
    - name: initcontainer
      image: coolgourav147/nginx-custom
      args: ["sleep", "30"]

Next, we will create a Pod using kubectl command with this YAML file:

kubectl create -f pod-init-container.yml

let's list the running pod

C:\Users\Gaurav\Desktop\kubernetes-youtube>kubectl get pods
NAME         READY   STATUS     RESTARTS   AGE
myfirstpod   0/1     Init:0/1   0          23s

in the above output we can see that container is not in ready status. and status of pod is Init:0/1 means init container is running.

after init container exit successfully we can see the pod status is running now.

C:\Users\Gaurav\Desktop\kubernetes-youtube>kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
myfirstpod   1/1     Running   0          42s
test-vol     1/1     Running   2          3d16h