FIELD NOTESKUBERNETES

Init containers: setup that must finish first

Init containers run in order, one at a time, and must exit 0 before the app starts. They run once per Pod, not once per release, so keep them idempotent, keep migrations out of them and watch the resources they reserve.

By Kevin4 min readPublished
ON THIS PAGE
A Pod starting up: init containers fix-permissions and render-config run one after the other and exit 0 while the Pod is Pending, then the app container starts and the Pod becomes Ready and joins its Service. Drawn in archhy.

An init container is the one place in a Pod where Kubernetes promises order: each one runs to the end, one at a time, and nothing under containers starts until the last has exited 0. Use it for setup the app cannot start without, written so that running it twice does no harm. The job people most often give it, a database migration, is usually the wrong one.

Based on the article from Gazeek, our sister site for hands-on CKA and CKAD labs: Init containers in Kubernetes: how they work, with examples. Worked manifests, the Docker Compose equivalent and a walk through every Init: status are there.

The guarantee, precisely#

  • In order, one at a time. The kubelet runs them as listed, and the next waits for the previous one to exit successfully.
  • Retried on failure. A failed init container is run again with back-off. If the Pod's restart policy is Never, the whole Pod fails instead.
  • The Pod waits. Until they all succeed the Pod is Pending, its Initialized condition is false, it cannot be Ready and no Service sends it traffic.
  • No probes. Success means exiting 0, so readiness, liveness and startup probes are rejected.
  • Its own image and its own view. An init container can carry tools, and mount Secrets, that the app container never sees. That is a security feature as much as a convenience: the app image stays small and its attack surface with it.

Once per Pod, not once per release#

"Runs once before the app" is true for a single Pod, and that is where the trouble starts. Every new Pod runs its init containers: every replica, every rollout, every eviction and reschedule. Occasionally the same Pod runs them again, for example when its sandbox container is restarted. The Kubernetes documentation says plainly that init code must be idempotent, and that anything writing to an emptyDir should expect to find its output already there.

Now put a schema migration in one. A Deployment with several replicas starts several copies of that migration at once on every rollout, and again whenever a node drains. Some migration tools take a lock and cope; many do not, and you discover which during an incident. Migrations are a once-per-release event, so run them once per release: as a Job your pipeline waits for before it rolls the Deployment. Keep init containers for work that belongs to one Pod.

Good fits are small and repeatable: render a config file from a template and a Secret into a shared volume, fix the ownership of a volume, or fetch an asset the app expects on disk. Write the result so a second run is safe:

YAML
initContainers:
- name: render-config
  image: busybox:1.36
  command: ['sh', '-c', 'sed "s/@DB@/$DB_HOST/" /tmpl/app.conf > /work/app.conf.tmp && mv /work/app.conf.tmp /work/app.conf']

Writing to a temporary file and renaming it means a run that dies halfway never leaves the app a half-written config.

Two costs that hide#

The reservation outlives the work. Init containers run alone, so the scheduler does not add their requests to the app's. It takes the larger of the biggest init container's request and what the rest of the Pod needs, and the node keeps that amount reserved after initialisation is over. A setup step that asks for 2 GiB in front of an app that needs 256 MiB holds 2 GiB for the life of the Pod. Size init containers as carefully as the app; requests and limits explains how the scheduler counts.

Waiting for a dependency hides a missing retry. The classic init container loops until a database name resolves. It checks once, at start-up. The database can vanish a minute later, so the app needs its own retries anyway, and once it has them the wait loop only delays start-up. It also never gives up: the Pod sits in Init:0/1 indefinitely, and activeDeadlineSeconds is no fix outside a Job, because it limits the Pod's whole life, not just its setup.

Init container or sidecar#

If the helper finishes, it is an init container. If it has to keep running, add restartPolicy: Always and it becomes a native sidecar, which starts in its place in the same list and stays up beside the app. Forget that field on a helper that never exits and the Pod waits at Init forever, and because nothing has failed there is no error to find. The two also combine: a sidecar listed above an init container is already running when that init container starts, so setup can go through a mesh proxy. Native sidecar containers covers what that field changes and when a helper does not belong in the Pod at all.

Watching a Pod step through Init:0/2, Init:1/2 and PodInitializing teaches this faster than reading about it. Gazeek's CKAD Pods module has those labs on a real cluster.

The diagram#

A Pod's start-up as a sequence: two init containers run and exit in order, the Pod stays Pending with Initialized false, and only then does the app start and the Pod become Ready. Drawn in archhy. Open it in archhy and press Make a copy to adapt it.

Go deeper#

Gazeek's full article has a Pod that waits for a Service and writes a page before Nginx starts, the Compose version, and a debugging guide for each Init: status.