Skip to main content
This lab demonstrates how init containers work in Kubernetes. You’ll learn how to:
  • Identify pods that include init containers
  • Inspect init container states and logs
  • Add an init container to an existing pod
  • Debug and fix a failing init container

1) Identify which pod has an init container

List pods in the cluster:
Note: kubectl get pods provides a concise summary but does not show init containers. To view init containers and their states, use kubectl describe pod <pod-name>.
kubectl get pods summarizes containers but does not show init containers. Use kubectl describe pod <pod-name> to inspect init containers and their states.
Describing the blue pod reveals an Init Containers section: Example excerpt from kubectl describe pod blue (condensed):
Answer: the blue pod has an init container named init-myservice. Its image is busybox and its state shows Terminated with Reason: Completed and Exit Code: 0, indicating the init container ran successfully and exited before the main container started.

2) Inspecting a pod with multiple init containers

A pod purple shows Init:0/2 in kubectl get pods:
Describing purple shows two init containers and the pod remains in Pending until both init containers complete:
Because init containers run sequentially, the total delay before the application container starts equals the sum of the init containers’ durations (in this example, 10 + 20 = 30 minutes), and the pod will show Init:X/Y until all init containers complete. Quick reference — pod init summary:

3) Add an init container to an existing pod (red)

Task: update the red pod to include an init container that uses the busybox image and sleeps for 20 seconds. Because many pod fields are immutable, the typical approach is to edit the pod manifest and recreate the pod. Add an initContainers section like this:
Apply the updated manifest with your preferred workflow (for example, kubectl apply -f red-pod.yaml after deleting the old pod) and then verify:
  • The pod restarts
  • The init container runs and completes before the main application container starts
  • kubectl describe pod red shows the init container as Terminated / Completed with Exit Code: 0
Some pod fields cannot be changed in-place. If you modify immutable fields (for example, container images in certain contexts), you may need to delete and recreate the pod or use kubectl replace --force -f <file> carefully. Always ensure this is safe for your environment.

4) Debug and fix a failing init container (orange)

A pod orange is stuck with Init:CrashLoopBackOff. First check the pod status:
Describe the pod to inspect the failing init container:
Because the main container hasn’t started, kubectl logs orange returns an error:
Inspect the init container logs directly using the -c flag to specify the init container:
The logs show a typo: sleeeep instead of sleep. The shell returned “not found”, causing the init container to exit with code 127 and enter CrashLoopBackOff. Fix the typo by editing the pod manifest (or replacing the pod) and updating the init container command to sleep 2;:
After applying the corrected manifest and recreating the pod, the init container completes and the application container starts:
Summary: The init container failed due to a typo in the command. Correcting the command allowed the init container to exit with Exit Code: 0, after which the main application started normally.

References

  • Kubernetes init containers concept
  • kubectl commands:
    • kubectl describe pod <pod-name>
    • kubectl logs <pod-name> -c <container-name>
    • kubectl apply -f <manifest.yaml>
    • kubectl delete pod <pod-name> (to recreate pods when necessary)
This concludes the init containers lab.

Watch Video