Debug CrashLoopBackOff and Pending Pods, 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.
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.
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, ReasonCompleted: the process ended normally, but a long-running app should never end, so it is restarted anyway.1, ReasonError: 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 ReasonOOMKilled, 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:
| STATUS | What it means | Look in the Events for |
|---|---|---|
ErrImagePull, then ImagePullBackOff | The node cannot download the image: a wrong name or tag, or a private registry without credentials | Failed to pull image and the reason |
Running, READY 0/1 | The container runs, but its readiness probe fails, so it gets no traffic | Unhealthy: Readiness probe failed |
CreateContainerConfigError | An env entry names a ConfigMap, Secret or key that does not exist | The missing object's name |
ContainerCreating for minutes | Often a volume that cannot be mounted, such as a missing ConfigMap | FailedMount |
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.
As a checklist you can reuse on any cluster:
- Right cluster and namespace? Check
kubectl config current-context. kubectl get pods: read STATUS, READY and RESTARTS.kubectl describe pod: read the Events, then State and Last State.- If it crashed,
kubectl logs --previous. - 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
- Setup Kubernetes cluster monitoring the easy way: set up cluster monitoring, so restarts and failing pods show up on a dashboard before anyone runs kubectl.