Debug Init Containers

This page shows how to investigate problems related to the execution of Init Containers. The example command lines below refer to the Pod as <pod-name> and the Init Containers as <init-container-1> and <init-container-2>.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Checking the status of Init Containers

Display the status of your pod:

  1. kubectl get pod <pod-name>

For example, a status of Init:1/2 indicates that one of two Init Containers has completed successfully:

  1. NAME READY STATUS RESTARTS AGE
  2. <pod-name> 0/1 Init:1/2 0 7s

See Understanding Pod status for more examples of status values and their meanings.

Getting details about Init Containers

View more detailed information about Init Container execution:

  1. kubectl describe pod <pod-name>

For example, a Pod with two Init Containers might show the following:

  1. Init Containers:
  2. <init-container-1>:
  3. Container ID: ...
  4. ...
  5. State: Terminated
  6. Reason: Completed
  7. Exit Code: 0
  8. Started: ...
  9. Finished: ...
  10. Ready: True
  11. Restart Count: 0
  12. ...
  13. <init-container-2>:
  14. Container ID: ...
  15. ...
  16. State: Waiting
  17. Reason: CrashLoopBackOff
  18. Last State: Terminated
  19. Reason: Error
  20. Exit Code: 1
  21. Started: ...
  22. Finished: ...
  23. Ready: False
  24. Restart Count: 3
  25. ...

You can also access the Init Container statuses programmatically by reading the status.initContainerStatuses field on the Pod Spec:

  1. kubectl get pod nginx --template '{{.status.initContainerStatuses}}'

This command will return the same information as above in raw JSON.

Accessing logs from Init Containers

Pass the Init Container name along with the Pod name to access its logs.

  1. kubectl logs <pod-name> -c <init-container-2>

Init Containers that run a shell script print commands as they’re executed. For example, you can do this in Bash by running set -x at the beginning of the script.

Understanding Pod status

A Pod status beginning with Init: summarizes the status of Init Container execution. The table below describes some example status values that you might see while debugging Init Containers.

StatusMeaning
Init:N/MThe Pod has M Init Containers, and N have completed so far.
Init:ErrorAn Init Container has failed to execute.
Init:CrashLoopBackOffAn Init Container has failed repeatedly.
PendingThe Pod has not yet begun executing Init Containers.
PodInitializing or RunningThe Pod has already finished executing Init Containers.