Course contents · Lesson 20 of 22

Debug CrashLoopBackOff and Pending Pods, Hands-On

Hands-On with kind · Lesson 20 of 22 · 11 min Hands-on

You apply a manifest, run kubectl get pods, and instead of Running you see CrashLoopBackOff, or a pod that sits at Pending for ten minutes. The temptation is to change things at random and apply again. This lesson gives you a fixed order of checks instead, and you practise it on two pods that fail on purpose in your kind cluster from lesson 17.

The first three commands for any broken pod

Whatever the symptom, start with the same three commands, all met in lesson 18. Here they are for the lab's pod named crashloop:

kubectl get pods
kubectl describe pod crashloop
kubectl logs crashloop

get pods is the dashboard light: STATUS names the problem, READY counts ready containers and RESTARTS counts restarts. describe pod prints each container's state and, at the very bottom, the pod's Events, newest last; most answers are there. logs prints what the app itself wrote, so it helps only once a container has run. For a pod that never started, the Events are all the evidence you have.

CrashLoopBackOff: crash, wait, restart

CrashLoopBackOff means the container started and exited, the kubelet (the node agent) restarted it, it exited again, and the kubelet is now waiting before the next try. That growing wait is the back-off. It is not a cause, only the sign that a container keeps exiting. With the default restartPolicy: Always from lesson 9, even a clean exit is restarted, so an app that simply finishes loops too.

In the timeline, notice how fast the wait grows: it doubles from 10 seconds to a cap of five minutes, reached about five minutes after the first crash. So if the cause was outside the pod, such as a database that was down, the pod can take up to five minutes to notice it is back. After 10 minutes of running without trouble, the delay resets.

Top: the crash loop cycle. Running, the app starts; Error, it exits with code 1; CrashLoopBackOff, the kubelet waits; after the wait, the kubelet starts it again. Below, a bar chart of the default wait before each restart: 10, 20, 40, 80 and 160 seconds, which is about 5 minutes after the first crash, then 300 seconds for the 6th wait and every later one, the cap of 5 minutes. Footer: if the container runs 10 minutes without a crash, the wait resets to 10 seconds
Retries come fast at first, then at most one every five minutes.

Between tries, the run you want to read has already ended. kubectl logs --previous (or -p) prints the logs of the previous instance of the container, the run that crashed. Plain kubectl logs reads the current instance if one is running, and that run may have only just begun. For the lab's pod:

kubectl logs crashloop --previous

You should see the app's own explanation, which is the cause:

app starting
error: required setting GREETING is not set

Before the first restart there is no previous run, and kubectl returns an error saying so; wait a few seconds. Next, see how the run ended. In kubectl describe pod crashloop, Last State holds the crashed run's Reason and exit code, the number a process returns when it ends. The diagram shows where to look.

Trimmed kubectl describe pod crashloop output with times illustrative. Status: Running, annotated: the phase stays Running. Container app. State: Waiting, Reason: CrashLoopBackOff, annotated: now waiting out the back-off. Last State: Terminated, Reason: Error, annotated: the run that crashed. Exit Code: 1, annotated: the app's own error. Started and Finished in the same second, annotated: it crashed at once. Ready: False. Restart Count: 4, annotated: restarts so far, as in RESTARTS. Events at the bottom: Warning, BackOff, 6s (x7 over 95s), kubelet, Back-off restarting failed container, annotated: events, newest last, the kubelet's BackOff warning
State is now, Last State is the crash: read the second one.

Notice that the pod's phase stays Running: CrashLoopBackOff is a container's waiting reason, not a phase. Three exit codes cover most crashes:

  • 0, Reason Completed: the process ended normally, but a long-running app should never end, so it is restarted anyway.
  • 1, Reason Error: the app stopped itself because of an error, such as a missing setting. Its logs usually say which.
  • 137: 128 plus 9, so the process was killed by signal 9, SIGKILL, which the kernel uses to end a process at once. With Reason OOMKilled, it went over its memory limit, as in lesson 15; raise the limit or fix the leak.

Pending: the scheduler cannot place the pod

A pod stays Pending when the kube-scheduler from lesson 8 finds no node that can take it. No container ever starts, so kubectl logs has nothing to show. The lab's pending.yaml asks for far more CPU than your laptop has:

      resources:
        requests:
          cpu: "100"

That is 100 cores. The scheduler places pods by their requests, so no node fits. kubectl describe pod pending ends like this (trimmed; your age will differ):

Events:
  Type     Reason            Age   From               Message
  ----     ------            ----  ----               -------
  Warning  FailedScheduling  15s   default-scheduler  0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod

Read it as a count: of 1 node, 0 can take the pod, and 1 was ruled out for Insufficient cpu. Preemption, evicting lower-priority pods to make room, would not help either. Fix it by lowering the request to what the app needs, freeing room or adding a node.

Storage is the other common reason. If the pod uses a PersistentVolumeClaim from lesson 14 that cannot be bound, for example because its storageClassName is misspelt, the message says so: pod has unbound immediate PersistentVolumeClaims. Then run kubectl describe pvc with the claim's name and read its Events.

Other common failures, and a checklist

Four more problems show up often. Each has its own STATUS, so kubectl get pods already tells you where to look:

STATUSWhat it meansLook in the Events for
ErrImagePull, then ImagePullBackOffThe node cannot download the image: a wrong name or tag, or a private registry without credentialsFailed to pull image and the reason
Running, READY 0/1The container runs, but its readiness probe fails, so it gets no trafficUnhealthy: Readiness probe failed
CreateContainerConfigErrorAn env entry names a ConfigMap, Secret or key that does not existThe missing object's name
ContainerCreating for minutesOften a volume that cannot be mounted, such as a missing ConfigMapFailedMount

The flowchart puts it together. Notice that only the crash branch starts with logs: on the others, the container never started or is still running, and the Events record what went wrong.

Flowchart. Start with kubectl get pods and read STATUS and READY. What does STATUS say? Five branches. Pending, no node fits: describe pod, look for Insufficient cpu or an unbound claim, fix by lowering requests. ImagePullBackOff, cannot get image: describe pod, look for Failed to pull image, fix the name or tag. CrashLoopBackOff, keeps exiting: logs --previous, look for the app's error and the Exit Code, fix the app or config. CreateContainerConfigError: describe pod, look for a missing ConfigMap or key, add it or rename. Running 0/1, runs but not ready: describe pod, look for Unhealthy, probe failed, fix the probe or app. Footer: only crashes start with logs; the rest start with Events
STATUS picks the branch, and most branches end in the Events.

As a checklist you can reuse on any cluster:

  1. Right cluster and namespace? Check kubectl config current-context.
  2. kubectl get pods: read STATUS, READY and RESTARTS.
  3. kubectl describe pod: read the Events, then State and Last State.
  4. If it crashed, kubectl logs --previous.
  5. Change one thing, apply, and start again at step 2.

For the wider picture, this lists every recent event in the namespace, oldest first:

kubectl get events --sort-by=.lastTimestamp

You should see LAST SEEN, TYPE, REASON, OBJECT and MESSAGE columns, with Warning rows such as BackOff for pod/crashloop. Events are kept for one hour by default, so look soon. The newer kubectl events command sorts by time without the flag.

Check yourself

A pod is Pending and kubectl logs prints nothing. Why is that the wrong tool, and what should you read?

No node was chosen, so no container ever started or wrote anything. The scheduler's reason is in the FailedScheduling event at the bottom of kubectl describe pod.

Last State shows OOMKilled and exit code 137, and the logs just stop. Will more restarts fix it?

No. The kernel killed the container for going over its memory limit, so every run hits the same limit. Raise the limit or reduce the app's memory use.

A crashing pod's database was down for 20 minutes. It is back, yet the pod stays in CrashLoopBackOff for a few more minutes. Why?

The back-off had reached its five-minute cap, so the kubelet waits up to five minutes before the next restart. Only that next run can find the database.

Go deeper