🎓 A1Learner
🔍
0 of 51 lessons completed

Kubernetes: Fix CrashLoopBackOff

CrashLoopBackOff means the container starts, exits, and restarts in a loop. The cause is in the previous logs or in the exit code.

See the loop

The pod list shows the status and the restart count. A climbing restart count confirms the loop.

kubectl get pods -l app=web

Read the previous logs

The crashed container printed its error before it exited. The previous flag recovers that output.

kubectl logs web-7d9f4b6c8-x2k9m --previous

Read the exit code

Describe shows the last state of the container. The exit code points at the kind of fault.

kubectl describe pod web-7d9f4b6c8-x2k9m | grep -A 3 'Last State'

Fix the cause, not the pod

The logs blame the database address. Fix the ConfigMap value and the new pods start cleanly.

kubectl get pods -l app=web

Example

kubectl get pods -l app=web
kubectl logs web-7d9f4b6c8-x2k9m --previous
kubectl describe pod web-7d9f4b6c8-x2k9m | grep -A 3 'Last State'

Key points

  • CrashLoopBackOff: the container keeps exiting, so Kubernetes keeps restarting it with a growing wait
  • Use kubectl logs --previous to read the output of the container that crashed
  • Exit code 1 means the app itself reported an error
  • Exit code 137 means the container was killed for using too much memory
  • Exit code 143 means the container was stopped by a termination signal
  • Exit code 127 means the command inside the container was not found
  • Deleting the pod is not a fix, because the controller recreates it identically

In this example, the first command shows the pod in CrashLoopBackOff with five restarts. The second command reads the previous logs. The output shows a refused database connection. The third command shows the exit code. The code is 1, so the app itself reported the error and the database address is the fix.

Note

A liveness probe that checks a dependency can restart a healthy app when the dependency is down.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

A pod shows CrashLoopBackOff with exit code 137. What does the code mean?