Fix: CrashLoopBackOff in Kubernetes
The container starts, exits, and Kubernetes waits longer before each restart. Read the previous container's logs and its exit code to find out why it keeps dying.
Not checked by a trusted editor yet Last edited Outdated?
kubectl get pods shows a growing restart count and this status:
NAME READY STATUS RESTARTS AGE
api-5d8f7c6b9d-qm4tz 0/1 CrashLoopBackOff 6 (82s ago) 9m
and kubectl describe pod lists events like:
Warning BackOff kubelet Back-off restarting failed container api in pod api-5d8f7c6b9d-qm4tz_shop(...)
CrashLoopBackOff is not an error by itself. It means the container keeps exiting, and the kubelet is waiting before restarting it again. The delay doubles each time (10s, 20s, 40s and so on) up to five minutes, and resets after the container has run for ten minutes without problems. The real cause is whatever makes the process exit.
Step 1: read the logs of the crashed instance
kubectl logs api-5d8f7c6b9d-qm4tz -n shop --previous
kubectl logs api-5d8f7c6b9d-qm4tz -n shop -c api --previous
--previous shows the output of the container that just died. Without it you often see an empty log from the new container that has not started yet. Look at the last lines: a stack trace, “missing environment variable”, “connection refused” or “permission denied” usually names the cause.
Step 2: read the exit code and reason
kubectl describe pod api-5d8f7c6b9d-qm4tz -n shop
Under Last State: Terminated, check Reason and Exit Code:
Reason: Errorwith exit code1(or another small number): the application exited on its own. The logs explain why.Reason: OOMKilled, exit code137: the container went over its memory limit. See the OOMKilled fix.- Exit code
137without OOMKilled: the process was killed with SIGKILL, often after a failed liveness probe. - Exit code
0withReason: Completed: the process finished normally, but a Deployment expects it to run forever. The command is wrong, or the work belongs in a Job. - Exit code
127or126: the command was not found or not executable. Checkcommand/argsand the image’s entrypoint.
Common causes and fixes
- Bad configuration: a missing environment variable, a wrong ConfigMap key, a wrong database URL. Fix the ConfigMap or Secret, then
kubectl rollout restart deployment/api -n shop, because environment variables are only read at start. - A dependency is not reachable: the app exits when the database is down. Make it retry instead of exiting, or check the dependency with a readiness probe.
- Liveness probe too strict: the event list shows
Liveness probe failedand thenKilling. The app needs more time to start. Add astartupProbeor raisefailureThresholdandperiodSeconds; do not make liveness depend on other services. - Out of memory: raise the memory limit or fix the leak.
- Permissions: the image expects to run as root or write to a read-only filesystem, but the Pod’s
securityContextforbids it. The log typically says “permission denied”. - Wrong command:
commandin the manifest overrides the image ENTRYPOINT andargsoverrides CMD; a typo there exits immediately.
Step 3: inspect the container interactively
If the container dies too fast to look inside, start a copy of the pod with a shell instead of the normal command, investigate, then delete the copy:
kubectl debug api-5d8f7c6b9d-qm4tz -n shop -it --copy-to=api-debug --container=api -- sh
kubectl delete pod api-debug -n shop
This works only if the image contains a shell. For a minimal image, use kubectl debug with a separate --image=busybox:1.36 and --share-processes on the copy.
Step 4: confirm the fix
kubectl rollout status deployment/api -n shop
kubectl get pods -n shop -l app=api -w
The restart count stops growing and the pod stays Running with READY 1/1. If a new release introduced the crash, kubectl rollout undo deployment/api -n shop restores the previous version while you investigate.
Official documentation
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0139 Collect describe, events and logs of a failing Kubernetes pod A bash script that saves a pod's status, describe output, events, YAML, current and previous logs of every container and its node… Intermediate · Step by step · Linux, macOS Script Troubleshooting 15 Oct 2026
- 0140 kubectl cheat sheet: the commands you use every day A compact reference of everyday kubectl commands: contexts and namespaces, listing and inspecting objects, logs and exec, applying changes, rollouts, scaling and… Beginner · Cheat sheet · Any system Article Concepts · Troubleshooting 15 Oct 2026
- 0141 List Kubernetes pods that are not Running and Ready A bash script that lists every pod across namespaces that is Pending, failing or running but not ready, with the waiting reason,… Intermediate · Cheat sheet · Linux, macOS Script Troubleshooting 15 Oct 2026
- 0142 Fix: ImagePullBackOff and ErrImagePull in Kubernetes The pod cannot start because the node failed to pull the container image. Read the "Failed to pull image" event to see… Intermediate · Fixes an error · Linux Article Troubleshooting 15 Oct 2026
- 0144 Fix: Pod stuck in Pending with “0/3 nodes are available: Insufficient cpu” The scheduler cannot find a node with enough unreserved CPU for the pod's request. Compare the request with node allocatable resources, then… Advanced · Fixes an error · Linux Article Troubleshooting · Workloads & services 15 Oct 2026
- 0145 Fix: OOMKilled (exit code 137) in Kubernetes A container used more memory than its limit and the kernel killed it. Confirm the reason, measure real usage, then raise the… Advanced · Fixes an error · Linux Article Troubleshooting · Workloads & services 15 Oct 2026
Repos in the wild
- derailed/k9s derailed/k9s K9s is a terminal UI for Kubernetes that lets you browse resources, read logs, open shells and follow cluster changes from the keyboard. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,838 chars