Kubernetes : Init Containers in Pods

Prathap
2 min readOct 22, 2022

--

Pods can have one or more Init containers which run before the application containers are started.

Init containers contain utilities or setup scripts not present in application image.

  • Init containers always runs to completion
  • Each Init container must complete successfully before next one starts
  • Because Init containers runs to completion before any application container starts, Init containers offers the mechanism to block or delay the application container start-up until a set of preconditions are met. Once the preconditions are met, all the application containers can start in parallel.

Examples:

Here are some scenarios for how to use Init containers,

  • Clone a Git repository into a Volume
  • Wait for sometime before starting the app container with a command like sleep 60
  • Place values into a configuration file and run a template tool to dynamically generate a configuration file for the main app container. For example, place the POD_IP value in a configuration and generate the main app configuration file

This example defines a simple Pod that has two init containers. The first waits for myservice, and the second waits for mydb. Once both init containers complete, the Pod runs the app container from its spec section.

--

--