Init Containers -  Kubernetes

Init Containers - Kubernetes

Init containers are special containers & kubernetes will always execute them to completion before the main containers are executed.

In simple, before the main containers start it depends on some of the prerequisite processes, work, and utility to run so that the main container can run smoothly. So the prerequisite is meant to run in a separate container called an init container.

Use case example:-

We have 2 init-containers where the first container will simply print a message and the second one will sleep for 30 seconds after which our main containers will come into action.

Properties of Init containers:-

  • It contains utilities or setup scripts that are not present in the App image (making the image lightweight).

  • Always run to completion.

  • Init containers execute sequentially & each of the init containers must be succeeded before the next can run.

Syntax to create init containers using yaml:-

apiVersion: v1
kind: Pod
metadata:
  name: init-container
spec:
  initContainers:
  - name: init_container_1
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running']
  - name: init_container_1
    image: alpine
    command: ['sleep', '60']
  containers:
  - name: Web_app_container_1
    image: nginx
  - name: Web_app_container_2
    image: nginx

How Init container comes to action in kubernetes:-

[1]. First, the kubelet will wait until the networking & storage are ready so that it can start running init containers.

[2]. Kublet then runs the pod's init containers in the order they appear in the pod's spec.

[3]. Each init container must exit successfully before the next container starts.

[4]. A pod cannot be in a ready state until all init containers have succeeded.

[5]. If the pod restarts, or is restarted, all init containers will be executed again.

Thank you so much for taking your valuable time for reading.

I have tried my level best to explain as much information as possible in the easiest manner. Any feedback for further improvement will be highly appreciated!

We can connect at Rahul Kumar Verma | LinkedIn Rahul Kumar Verma (@RahulKu28171925) / Twitter