CrashLoopBackOff: why your container keeps restarting
Reproduced on k3s v1.31.5 (k3d) with kubectl v1.36.1.
What you see
$ kubectl get pod crash-demo -n lab
NAME READY STATUS RESTARTS AGE
crash-demo 0/1 CrashLoopBackOff 4 (56s ago) 2m34s
$ kubectl logs crash-demo -n lab --previous
config file /etc/app.conf not found
starting
$ kubectl describe pod crash-demo -n lab
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Restart Count: 4
Events:
Warning BackOff 56s (x9 over 2m26s) kubelet spec.containers{app}: Back-off restarting failed container app in pod crash-demo_lab(...)What it means
The container ran and then exited. The pod's restartPolicy says to restart it, so the kubelet does. Each time it exits again, the kubelet waits longer before the next start. The delay starts at 10 seconds, doubles each time and is capped at five minutes.
While the kubelet is waiting, the status reads CrashLoopBackOff. Right after an exit it can briefly read Error or Completed instead. In our lab the same pod flipped between Error and CrashLoopBackOff. Both point at the same problem.
The back-off resets once a container runs for 10 minutes without exiting. So a pod that crashes every few hours shows a low restart delay, not CrashLoopBackOff.
Common causes
The app exits on start
A missing config file, a bad flag or a missing environment variable makes the process exit with an error. This is the most common cause.
How to tell: Last State is Terminated with Reason Error and a small exit code such as 1 or 2. The previous logs show the error message.
A dependency is not reachable
The app tries to connect to a database, queue or API at startup and exits when it fails. It will keep crashing until the dependency is up.
How to tell: Previous logs show connection refused, timeout or DNS lookup errors just before the exit.
The container runs out of memory
The process goes over its memory limit and the kernel kills it. The restart loop looks the same as an app crash from the outside.
How to tell: Last State Reason is OOMKilled with Exit Code 137. See the OOMKilled page.
The liveness probe kills it
A failing liveness probe makes the kubelet stop and restart a container that may be working fine. Repeated probe kills lead to the same back-off.
How to tell: Events show "failed liveness probe, will be restarted" and Unhealthy warnings.
The main process finishes on purpose
A script or one-off task runs inside a Deployment. It exits 0 when done, but a Deployment always restarts its containers.
How to tell: Exit Code 0 with Reason Completed. The logs end normally.
Permission or file system errors
The image expects to write to a path that is read-only or owned by another user. This often appears after adding runAsNonRoot or readOnlyRootFilesystem.
How to tell: Previous logs show permission denied or read-only file system.
The command or entrypoint is wrong
The command in the pod spec or the image points at a binary that is not in the image, or at a file that is not executable. The process never gets going, so there are no app logs to read.
How to tell: Last State reason StartError with exit code 128, or exit code 126 or 127 from a shell wrapper. The message names the missing or non-executable file. See the CreateContainerError page.
How to diagnose it
kubectl get pod <pod> -n <namespace>Check RESTARTS and how long ago the last restart was. A climbing count confirms the loop.
kubectl logs <pod> -n <namespace> --previousThese are the logs of the container that just died. The last lines usually name the cause. Add -c <container> for multi-container pods.
kubectl describe pod <pod> -n <namespace>Read Last State: the Reason (Error, OOMKilled, Completed) and the Exit Code. Then read Events for probe failures.
kubectl get pod <pod> -n <namespace> -o jsonpath='{.status.containerStatuses[*].lastState.terminated}'Gives the exit code, reason and start and finish times as JSON. A start and finish in the same second means it fails at startup.
kubectl get events -n <namespace> --field-selector involvedObject.name=<pod> --sort-by=.lastTimestampShows the order of events. Look for Killing or Unhealthy entries before each restart.
kubectl debug <pod> -n <namespace> -it --copy-to=<pod>-debug --container=<container> -- shStarts a copy of the pod with a shell instead of the normal command. Use it to check files, env vars and network from inside. The image needs a shell.
How to fix it
Fix the config and redeploy
Correct the missing env var, ConfigMap value or flag that the logs point to. Then roll out again.
Trade-off: You need to find the right fix first. If the cause is still unclear, roll back instead of guessing in production.
Roll back to the last working version
If the crash started with a new release, go back to the previous ReplicaSet while you investigate.
kubectl rollout undo deployment/<deployment> -n <namespace>Trade-off: You lose whatever the new release shipped. It also does not help if the cause is outside the image, such as a changed Secret.
Retry dependencies instead of exiting
Make the app retry its database or API connection with a back-off instead of exiting on the first failure.
Trade-off: The pod may look Running while it cannot serve. Pair this with a readiness probe so it gets no traffic until it connects.
Use the right workload type
If the process is meant to finish, run it as a Job or CronJob, not a Deployment.
Trade-off: Jobs have their own retry and cleanup settings (backoffLimit, ttlSecondsAfterFinished) that you need to set.
How to stop it coming back
- Validate config at startup and log one clear line naming what is missing before exiting.
- Alert on restart count growth, not only on pod phase. A crashing pod can still show Running between restarts.
- Use maxUnavailable and readiness probes on rollouts so a crashing release does not replace all healthy pods.
- Keep config changes and image changes in separate rollouts so you know which one broke things.
How KubeGraf investigates it
KubeGraf watches your clusters for failures such as CrashLoopBackOff, OOMKilled pods and stuck rollouts. How KubeGraf handles Kubernetes incidents.
Frequently asked questions
How do I fix CrashLoopBackOff in Kubernetes?
Run kubectl logs with --previous to see why the last container exited. Run kubectl describe pod to see its exit code and Last State. Fix what they point to, or roll back if the crash started with a new release.
What does Back-off restarting failed container mean?
It is the kubelet event behind CrashLoopBackOff. The container exited and the kubelet is waiting before it starts it again. The delay starts at 10 seconds, doubles each time and is capped at five minutes.
What does CrashLoopBackOff with exit code 1 mean?
Exit code 1 means the app itself exited with an error. Common reasons are a missing config file, a bad flag or a missing environment variable. The previous logs usually show the message.
Why does my pod keep restarting?
The container exits and the restart policy starts it again. The usual reasons are an app error on start, a dependency it cannot reach, an out of memory kill or a failing liveness probe. Last State in kubectl describe pod tells you which.
Last updated . Try KubeGraf Freemium.
Related
- OOMKilled
- Liveness probe failed / Readiness probe failed
- Init:CrashLoopBackOff
- Exit Code 137 / Exit Code 143
- Working a CrashLoopBackOff: the first five minutes
- Kubernetes Root Cause Analysis: A Complete Guide