Fix Kubernetes CrashLoopBackOff with describe and logs --previous
CrashLoopBackOff means a container keeps dying. Read Last State in describe and logs --previous to tell app errors, OOMKilled, and probe kills apart.
State: Waiting Reason: CrashLoopBackOff Last State: Terminated Reason: Error Exit Code: 1CrashLoopBackOff is a result, not a cause. The container keeps exiting and restarting, so kubelet is waiting between restarts. The cause is in the exit reason and logs of the container that just died. The block above is the kubectl describe pod example from the official Debug Running Pods page, for a container that only runs false.
[1] Read Last State in kubectl describe pod. State shows the current wait, so it always says CrashLoopBackOff. Why it died is in Reason and Exit Code under Last State.
$ kubectl describe pod <pod>What Last State shows | Go to |
|---|---|
Reason: Error with a non-zero code such as Exit Code: 1 | The app exited with an error → [2] |
Reason: OOMKilled | Memory limit exceeded → [3] |
Events with Liveness probe failed and Killing | The probe killed it → [4] |
Exit Code: 0 | The process finished and exited. The default restartPolicy of Always restarts even clean exits → [5] |
Check Events at the bottom of the same output. The official troubleshooting order is the same: logs (kubectl logs), events (kubectl describe pod), configuration (environment variables, volumes), resources (CPU, memory), then the application itself.
[2] Read the logs from before the crash with --previous. Plain kubectl logs shows the current instance, which is often empty or only has startup lines after a restart. -p (--previous) prints the logs of the previous instance if it exists.
$ kubectl logs <pod> --previous$ kubectl logs <pod> -c <container> --previousWith several containers, pick the failing one with -c (names are under Containers: in describe). The last few lines — an exception, No such file, a refused connection — usually hold the answer. If the log says exec format error, the problem is the image's CPU platform, not the app; see Docker exec format error.
If the log is empty, read the termination message. Whatever the container wrote to /dev/termination-log shows up as lastState.terminated.message:
$ kubectl get pod <pod> -o go-template="{{range .status.containerStatuses}}{{.lastState.terminated.message}}{{end}}"If the app never writes that file, set terminationMessagePolicy: FallbackToLogsOnError on the container. On an error exit, Kubernetes then uses the tail of the log (2048 bytes or 80 lines, whichever is smaller). It applies to new pods after you add it to the pod template and redeploy.
[3] For OOMKilled, check the memory limit. In the official example a container with a 100 MiB limit tries to allocate 250 MiB, gets killed, and kubelet keeps restarting it:
NAME READY STATUS RESTARTS AGEmemory-demo-2 0/1 OOMKilled 1 24sRead the reason and exit code together in lastState from kubectl get pod <pod> -o yaml:
lastState: terminated: … exitCode: 137 … reason: OOMKilledAfter repeated restarts the same pod is listed as CrashLoopBackOff, so judge by reason in lastState, not the STATUS column. Do not conclude from exit code 137 alone; confirm reason: OOMKilled. Then either raise resources.limits.memory to what the app really uses, or bring the app's memory use (heap size flags and the like) under the limit.
[4] Check whether a probe is killing it. If the liveness probe fails while the app is fine, kubelet kills the container and starts it again. The events look like this (official example):
Warning Unhealthy 10s (x3 over 20s) kubelet, node01 Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directoryNormal Killing 10s kubelet, node01 Container liveness failed liveness probe, will be restartedIn this case logs --previous often shows normal operation and no app error. Check that the probe's path and port match the app, and whether the probe gives up before the app finishes starting. For slow starters, the docs recommend a separate startupProbe with failureThreshold * periodSeconds longer than the worst-case startup (for example 30 × 10 = 300 seconds). Once the startup probe succeeds, the liveness probe takes over.
[5] If the exit code is 0 or the logs do not explain it, look inside. For containers that keep crashing, the official docs show how to start a copy of the pod with only the command changed to sh:
$ kubectl debug <pod> -it --copy-to=<pod>-debug --container=<container> -- shInside the copy, run the original start command by hand and check environment variables (env), mounted config files, and volume paths. A start script that launches a daemon in the background and returns exits with code 0 and restarts forever; keep the process in the foreground.
⚠️ The copy is one more pod with the same image and settings. If the app runs a database migration or writes to external systems at startup, running the original command in the copy does that again. Delete the copy when you are done:
$ kubectl delete pod <pod>-debug[6] Know how long to wait after a fix. kubelet's restart delay doubles from 10s to 20s, 40s and so on, capped at 300 seconds (5 minutes). After a container runs for 10 minutes without problems, the delay resets. So after you fix the config, an existing pod stays in CrashLoopBackOff until its next restart (up to 5 minutes). With a Deployment, the fixed template creates new pods; watch those.
If the pod sits in ImagePullBackOff, the container never started; go to Fix ImagePullBackOff and ErrImagePull instead. If kubectl itself says localhost:8080 was refused, start with that guide.