Init Containers

Rajesh
3 min readJan 22, 2024

What are Init Containers?

Init containers are exactly like regular containers, except:

  • Init containers always run to completion.
  • Each init container must complete successfully before the next one starts.

Init Container can:

  • Contain and run utilities that are not desirable to include in the app Container image for security reasons.
  • Contain utilities or custom code for setup that is not present in an app image. For example, there is no requirement to make an image FROM another image just to use a tool like sed, awk, python, or dig during setup.
  • Use Linux namespaces so that they have different file system views from app containers, such as access to secrets that application containers are not able to access.

Let’s delve into Init Container with Kodekloud Engineer task

Kodekloud Engineer is a platform to practice devops skills for free. Check it out:

Task:

  • Here Init Container used for writing data to a file, which was then used by main container.
  • Init container config was just like container config, except the initContainers in-place of containers.
  • NOTE: Init container should be run to completion.if the Pod has a restartPolicy of Never, and an init container fails during startup of that pod, kubernetes treats the overall Pod as failed . if the pod has restartPolicy of Always,the kubelet repeatedly restarts that init container until it succeeds.
  • The deployment manifest for the task below
  • We can see it is same like container config.
  • After creating the manifest, using kubectl to apply the deployment to the cluster.
  • Now checking the logs. In the main container config, We can see the command which consists of while loop, which repeatedly runs after 5 seconds and displaying the content of Init Container created. If the log contains the content of Init container created, then init container successfully run in to completion
  • We can see the log, we can confirm the task is successful . Finally submitting the task to check .
  • It is just example of init container. In real scenarios it is used in various use cases just as mentioned what init container can do.

Thank You For Reading ! :)

--

--