- 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: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.blue pod reveals an Init Containers section:
Example excerpt from kubectl describe pod blue (condensed):
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 podpurple shows Init:0/2 in kubectl get pods:
purple shows two init containers and the pod remains in Pending until both init containers complete:
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 thered 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:
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 redshows the init container asTerminated/CompletedwithExit 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 podorange is stuck with Init:CrashLoopBackOff. First check the pod status:
kubectl logs orange returns an error:
-c flag to specify the init container:
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;:
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)