Init containers in Kubernetes

What an init container is, how init containers run one at a time before your app, a working Pod you can apply and watch, failing fast since Kubernetes 1.35, init containers versus sidecars, and how to fix a Pod stuck in Init.

GazeekKUBERNETES EXPLAINED·EPISODE 1
EPISODE 1

Init containers in Kubernetes

What an init container is, how init containers run one at a time before your app, a working Pod you can apply and watch, failing fast since Kubernetes 1.35, init containers versus sidecars, and how to fix a Pod stuck in Init.

0:00 / 7:24

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: How they run, step by step (new tab)
Copy and edit in archhy: The Pod, in a real cluster (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

After this lesson you can

  • Explain what an init container is and when the app starts
  • Write a Pod with two init containers and watch it start
  • Tell an init container from a sidecar
  • Debug a Pod stuck in Init with describe and logs
Transcript

Init containers in Kubernetes

Space Llama here. Today: init containers in Kubernetes. What they are, how they run, and a Pod you can try yourself. By the end, you'll write one, watch it start, and know how to fix a Pod that's stuck in Init.

What an init container is

An init container is a container that runs, and finishes, before the application containers in a Pod start. You list them under init containers in the Pod spec. They run one at a time, in the order you write them, and each one must exit with status zero. Only when every init container has succeeded do the app containers start.

Why use one

Use one to prepare something your app needs. Wait for a database. Download a config file. Fix permissions on a volume. An init container can carry tools like sed, awk or dig that the app image doesn't have, and it can be given Secrets the app containers can't see. So the app image stays smaller, with less to attack.

How they run, step by step

Here's a timeline: two init containers, then the app, nginx. Time runs left to right, with the Pod's status underneath. Init one, wait-for-db, starts once the Pod's network and storage are ready. Its setup can't succeed yet. By default, an init container that exits with an error is retried, and init two waits its turn. Until init one succeeds, the status reads Init zero of two, or Init Error between retries. Then init two, write-page, runs. While it runs, the status is Init one of two. When every init container has exited zero, the app starts. The status moves to Pod Initializing, then Running.

The Pod, in a real cluster

Here's that Pod in a real cluster. Two busybox init containers, and nginx as the app. Wait-for-db loops until the cluster's Domain Name System, DNS, resolves the Service called db. That proves the Service exists, not that the database is ready. Write-page leaves an index page in a shared volume, and nginx serves it once it starts. Every replica runs its own init containers, every time a Pod is created. So make them safe to repeat: idempotent, as the Kubernetes docs say.

The Pod's spec

Now the spec for that Pod. The full file, with its kind and name, is in the guide, linked below. Init containers get their own list, next to containers. The order you write them is the order they run. First, wait-for-db. Its command tries nslookup every two seconds, and exits with zero once the name resolves. Write-page is listed second, so it runs second. One command writes the index page, and it's done. It writes into a volume called html, mounted at slash work. Nginx, the app, goes under containers. It mounts the same volume as its web root, so it serves what the init container left behind. And the volume is an emptyDir: created with the Pod, and gone with the Pod.

Apply it and watch

Let's try it in the default namespace, before any Service called db exists. Save the full Pod from the guide as web dot yaml. Apply the Pod and watch it. The status stays at Init zero of two, because wait-for-db keeps looping. In a second terminal, create the db Service. Within a few seconds, the status moves to Init one of two, then Pod Initializing, then Running. Read the page from the nginx container, and there's the file the init container wrote.

Fail fast instead of retrying

By default, a failed init container is retried, with a growing back-off, if the Pod's restart policy is Always or On Failure. Since Kubernetes 1.35, a regular init container can set its own restart policy to Never or On Failure. With Never, a failed setup step fails the Pod straight away, even in a Pod that would otherwise retry.

Init container vs sidecar

People mix these up, so here's the difference. Since Kubernetes 1.29, native sidecars are declared under init containers too. An init container runs to completion, must exit with zero, and blocks the next container until it does. Add restart policy Always, and it becomes a sidecar instead. It starts, keeps running beside the app for the life of the Pod, and is restarted if it exits. Always is the only value that makes a sidecar.

Not the same as Docker --init

One more mix-up. Docker run dash dash init is not an init container. It runs a tiny init process as process ID one inside your container, to forward signals and reap zombie processes. The closest thing in Docker is Compose: depends on, with the condition service completed successfully. The setup service runs and exits, and only then does the app start.

A Pod stuck in Init

When a Pod won't start, the status column tells you where it is. Init N of M means N of M init containers have finished. Init Error means one exited with a non-zero code. Init CrashLoopBackOff means one keeps failing and the kubelet is backing off between retries. And Init ImagePullBackOff means it can't pull an init container's image.

Debug it

Start with describe. The Init Containers section shows each one's state, exit code and restart count, and the events at the bottom show image pull errors, missing volumes and scheduling problems. Then read the logs of the init container that's stuck. Name it with dash c: in a Pod with several containers, kubectl logs needs to be told which one. If it keeps crashing, add dash dash previous for the last attempt. And if it never started, there are no logs: the events in describe tell you why.

When not to use one

If the helper must keep running, use a sidecar. If a task should run once per release, like a database schema migration, remember every replica runs its init containers. A Job or a Helm hook runs it once per release, not once per Pod. And if your app already retries its database connection sensibly, a wait loop in front adds little.

For the CKAD and CKA

For the CKAD, the Certified Kubernetes Application Developer exam, expect to add an init container that waits for a Service or prepares a file. For the CKA, the Certified Kubernetes Administrator exam, a Pod stuck in Init is a troubleshooting task. Describe the Pod, then read the init container's logs.

Remember

Let's recap. Init containers run before your app, one at a time, in order, and each must exit with zero. Restart policy Always turns one into a sidecar. And if a Pod is stuck in Init, describe it, then read the init container's logs.

Practice and what's next

The full guide, with the YAML to copy and more debugging tips, is on the Gazeek blog. The link is in the description. Then try it on a real cluster in Gazeek Labs. Thanks for watching. More videos on the channel. Alpaca Mango. I'll be there. I'm always there. Mate.

Hands-on labs

Now try it on a real cluster

A real Kubernetes cluster in your browser for every lab, checked as you go.

See the labs

Gazeek Flash · free

Exam questions on your phone

Free questions on Application Design and Build: quick-fire batches and timed exams, on the web or Android.