Skip to content
|By Muhammad Wilayat Shah|13 views • 13 unique IPs

Fix Kubernetes CrashLoopBackOff Errors Step by Step

A step-by-step way to fix Kubernetes CrashLoopBackOff: read the exit code, check kubectl logs --previous and events, then fix the real cause.

ShareLinkedInX
Fix Kubernetes CrashLoopBackOff Errors Step by Step

Last reviewed: 25 November 2026

The first time a pod of ours sat in CrashLoopBackOff, I did what most people do: I deleted it. Kubernetes created a new one, the new one crashed, and within a minute the RESTARTS column was climbing again. It was a small Node.js queue worker we had just moved from Docker Compose, where it had been fine. The fix in the end was one missing environment variable. The time went into not knowing where to look.

This guide is the order I now follow, and the order I ask my team to follow before anyone touches a manifest. Kubernetes CrashLoopBackOff is not the bug. It is Kubernetes telling you the container keeps exiting and it is waiting before trying again. The real cause is almost always in one of three places: the exit code, the logs of the previous container, or the events at the bottom of kubectl describe.

Every failure in this article was reproduced on a throwaway kind cluster running Kubernetes v1.34 on a Linux box, and the outputs are copied from it. Your pod names and timings will differ; the reasons and exit codes will not.

What CrashLoopBackOff actually means

A pod in a Deployment has restartPolicy: Always. When its container exits, for any reason and with any exit code, the kubelet starts it again. If it keeps exiting, the kubelet waits longer between attempts: 10 seconds, then 20, then 40, doubling up to a cap of five minutes. While it is waiting, kubectl get pods shows CrashLoopBackOff. In the brief moment after a crash you may see Error or Completed instead; it flips back to CrashLoopBackOff as soon as the next wait starts. If the container then runs cleanly for ten minutes, the back-off timer resets.

Animated diagram of the Kubernetes CrashLoopBackOff cycle: container starts, runs, exits or is killed, then waits 10s, 20s, 40s, 80s, 160s up to a 5 minute cap before restarting, until the root cause is fixed and the pod stays Running
Start, crash, wait, start again. The wait doubles each time until you fix the cause.

Two things follow from this. First, deleting the pod only resets the back-off; the replacement runs the same image with the same config and crashes the same way. Second, a container that exits with code 0 also loops. A script that finishes successfully inside a Deployment is "crashing" as far as Kubernetes is concerned.

A two-minute diagnosis

Run these four commands in order. Usually the third one names the cause.

1. Confirm the loop

Code
$ kubectl get pods -l app=orders-api
NAME                          READY   STATUS             RESTARTS      AGE
orders-api-677bfdfb88-qtlwp   0/1     CrashLoopBackOff   3 (23s ago)   59s

READY 0/1 and a RESTARTS count that grows every time you look is the signature. If STATUS says ImagePullBackOff, CreateContainerConfigError or Pending instead, the container never started; that is a different problem and I cover the config one below.

2. Read the last state and the exit code

Code
$ kubectl describe pod orders-api-677bfdfb88-qtlwp
...
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
    Ready:          False
    Restart Count:  4
...
Events:
  Warning  BackOff  4s (x9 over 109s)  kubelet  Back-off restarting failed container api in pod ...

State is what it is doing now (waiting). Last State is how the previous attempt died, and that is the part you want. If you only need the numbers, jsonpath is quicker:

Code
$ kubectl get pod api-oom -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason} {.status.containerStatuses[0].lastState.terminated.exitCode}'
OOMKilled 137

3. Read the logs of the crashed container, not the current one

Code
$ kubectl logs deploy/orders-api --previous
Error: DB_HOST is not set

Plain kubectl logs shows the container that is currently starting or waiting, which is often empty. --previous (or -p) shows the one that just died. For a pod with several containers add -c <name>. If you run it at the exact moment of a restart you can get unable to retrieve container logs; wait a few seconds and run it again.

4. Read the events

Code
$ kubectl get events --field-selector involvedObject.name=api-probe --sort-by=.lastTimestamp

Events are where Kubernetes explains kills it did itself: failed probes, missing secrets, a command that could not be executed. They are kept for a limited time (one hour by default), so check them early.

Now map what you found to a cause:

Animated walkthrough of CrashLoopBackOff exit codes: exit 1 is an application error read with kubectl logs --previous, exit 137 OOMKilled means raise the memory limit, liveness probe failed with exit 0 means fix the probe path, exit 128 StartError means the command or entrypoint is wrong
Exit code and Reason first. They tell you which section below to read.
  • Exit Code 1, Reason Error (or any small number): the application quit. Read --previous logs.
  • Exit Code 137, Reason OOMKilled: memory limit. Logs are usually empty.
  • Exit Code 0 or 137/143 with "Liveness probe failed" in events: the kubelet killed a working app because the probe was wrong.
  • Exit Code 128, Reason StartError or 127: the command or entrypoint does not exist.
  • Exit Code 0, Reason Completed: the process finished. It should not be in a Deployment.

Fixes, by cause

The app exits with code 1: config, env vars, migrations

This is the most common case for us. A Laravel or Node.js app reads an environment variable at boot, does not find it, and exits. On a developer laptop the value comes from a .env file that never made it into the image, which is correct, but nobody added it to the Deployment either.

The logs from the previous container name the problem. Fix the variable (ideally in the manifest you keep in Git) and watch the rollout:

Code
$ kubectl set env deploy/orders-api DB_HOST=orders-db
deployment.apps/orders-api env updated
$ kubectl rollout status deploy/orders-api
deployment "orders-api" successfully rolled out
$ kubectl get pods -l app=orders-api
NAME                         READY   STATUS    RESTARTS   AGE
orders-api-cb5984f87-s9p2l   1/1     Running   0          1s
$ kubectl logs deploy/orders-api
orders-api listening, db=orders-db
Animated terminal session fixing Kubernetes CrashLoopBackOff: kubectl get pods shows rising restarts, kubectl describe pod shows exit code 1, kubectl logs --previous reveals DB_HOST is not set, kubectl set env fixes it and the new pod is Running with 0 restarts
The whole loop on a real cluster: confirm, read the exit code, read the previous logs, fix, verify.

If the logs show a stack trace instead of a clear message, the bug is in the code or in a migration that ran at boot. Roll back while you look: kubectl rollout undo deploy/orders-api puts the previous ReplicaSet back.

A missing ConfigMap or Secret

If the env var comes from a Secret that does not exist, you usually do not get CrashLoopBackOff at all. The container is never created:

Code
$ kubectl get pod api-secret
NAME         READY   STATUS                       RESTARTS   AGE
api-secret   0/1     CreateContainerConfigError   0          105s
$ kubectl describe pod api-secret | tail -1
  Warning  Failed  11s (x10 over 111s)  kubelet  Error: secret "orders-db" not found

Create the Secret in the same namespace as the pod and the kubelet picks it up on its next attempt; in my test the pod went to Running without being recreated.

Code
$ kubectl create secret generic orders-db --from-literal=password='...'
secret/orders-db created

The CrashLoopBackOff version of this is subtler: the Secret exists but a key is wrong or empty, or the reference is marked optional: true. The container starts with a blank value and the app exits with code 1. Check what the container actually received with kubectl get secret orders-db -o jsonpath='{.data}' and compare key names character by character.

OOMKilled, exit code 137

Code
$ kubectl describe pod api-oom
    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    137
$ kubectl logs api-oom --previous
$

Empty logs and 137 together mean the kernel killed the process with SIGKILL when it hit resources.limits.memory; it never had a chance to print anything. My test container tried to allocate about 200 MiB under a 64 MiB limit. The fix is either a limit that matches what the app really needs, or an app that needs less:

Code
resources:
  requests:
    memory: "256Mi"
  limits:
    memory: "320Mi"

Measure before you guess: kubectl top pod needs metrics-server, and for Node.js set --max-old-space-size below the container limit so V8 fails with a readable error instead of being killed. The same exit code outside Kubernetes is covered in How to Fix Docker Container Exited with Code 137, and if the whole node is short of memory rather than one container, read How to Troubleshoot Linux Server Running Out of Memory.

A liveness probe kills a healthy container

This one confuses people because the logs look normal. The app starts, serves traffic, and then gets restarted:

Code
$ kubectl describe pod api-probe
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
...
  Warning  Unhealthy  36s (x10 over 106s)  kubelet  Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    36s (x5 over 101s)   kubelet  Container api failed liveness probe, will be restarted

Here nginx shut down cleanly when the kubelet asked it to, so the exit code is 0. An app that ignores SIGTERM ends with 137 after the grace period instead. Either way, the events are the evidence. In my test the probe pointed at /healthz, which that server does not have.

The fix is a probe that checks a real endpoint, and a startupProbe for apps that boot slowly (a Laravel app warming caches, a JVM service, Next.js on a cold start). Liveness and readiness checks do not start until the startup probe has passed:

Code
startupProbe:
  httpGet: { path: /, port: 80 }
  periodSeconds: 5
  failureThreshold: 12     # up to 60s to boot
livenessProbe:
  httpGet: { path: /, port: 80 }
  periodSeconds: 10
  failureThreshold: 3

Keep the liveness endpoint cheap and local. If it checks the database, a slow database restarts every pod at once and makes things worse. A failing readiness probe does not restart anything; it only removes the pod from Service endpoints, so a pod stuck at 0/1 Running with zero restarts is a readiness problem, not a CrashLoopBackOff.

Wrong command or entrypoint: exit 128 or 127

Code
$ kubectl describe pod api-badcmd
    Last State:     Terminated
      Reason:       StartError
      Message:      ... exec: "/app/start.sh": stat /app/start.sh: no such file or directory
      Exit Code:    128

When command points at a file that is not in the image, the runtime cannot start the process at all and you get 128 with StartError. When the command runs through a shell and the binary inside it is missing, the shell exits with 127 and the logs say so:

Code
$ kubectl logs api-127
sh: php-fpm8.3: not found

Remember that command in a pod spec replaces the image's ENTRYPOINT and args replaces CMD. To look inside the image without fighting the loop, make a debug copy of the pod with a harmless command:

Code
$ kubectl debug api-badcmd --copy-to=api-badcmd-debug --container=api -- sleep 3600
$ kubectl exec api-badcmd-debug -c api -- ls /app
ls: /app: No such file or directory

So the script was never copied into the image. Delete the debug pod when you are done.

Permission denied on a volume

Code
$ kubectl logs api-perm --previous
sh: can't create /data/boot.log: Permission denied

The container runs as a non-root user (runAsUser: 1000) and the mounted directory belongs to root. For most persistent volumes, setting securityContext.fsGroup on the pod lets Kubernetes fix group ownership at mount time. That does not apply to hostPath volumes, which is what I used in the test, so there I used an init container that runs as root once and hands the directory over:

Code
initContainers:
- name: fix-perms
  image: busybox:1.36
  securityContext: { runAsUser: 0 }
  command: ["sh", "-c", "chown -R 1000:1000 /data"]
  volumeMounts: [{ name: data, mountPath: /data }]

After that change the pod went to Running with zero restarts. Do not "fix" this by running the app as root; fix the ownership.

A dependency is not ready yet

An API pod that exits when it cannot reach the database will loop until the database is up. The growing back-off then makes recovery slower than it needs to be. Two fixes, and I prefer to have both: make the app retry its connection instead of exiting, and add an init container that waits:

Code
initContainers:
- name: wait-for-db
  image: busybox:1.36
  command: ["sh", "-c", "until nc -z -w 2 orders-db 5432; do echo waiting for orders-db; sleep 2; done"]
Code
$ kubectl get pod api-wait-db
NAME          READY   STATUS     RESTARTS   AGE
api-wait-db   0/1     Init:0/1   0          100s
$ kubectl logs api-wait-db -c wait-for-db | tail -1
waiting for orders-db

The pod shows Init:0/1 instead of a crash loop, which is far clearer for whoever looks at it next. As soon as the database Service had a ready endpoint, the init container finished and the main container started.

The process finishes with exit code 0

Code
$ kubectl get pod api-exit0
NAME        READY   STATUS             RESTARTS      AGE
api-exit0   0/1     CrashLoopBackOff   2 (15s ago)   30s
$ kubectl logs api-exit0 --previous
migrations done

Nothing is broken except the object type. A migration, seeder or one-off script belongs in a Job (or a CronJob), which expects its container to finish. For a long-running server that exits 0, check that the process stays in the foreground: php-fpm -F, nginx -g 'daemon off;', and no & at the end of the start script.

If you have seen the same pattern in plain Docker, it is the same idea with different tooling; my notes on why Docker containers keep restarting cover the restart-policy side.

Common mistakes I still catch in review

  • Deleting the pod and calling it fixed. The ReplicaSet recreates it from the same spec.
  • Reading kubectl logs without --previous and concluding "there are no logs".
  • Raising the memory limit for an exit code 1, or changing code for an OOMKill. Check the exit code first.
  • Pointing liveness probes at an endpoint that checks the database, Redis and a third-party API.
  • No startupProbe on an app that takes longer to boot than initialDelaySeconds + periodSeconds × failureThreshold.
  • Putting a migration or seeding script in a Deployment instead of a Job.
  • Fixing the live Deployment with kubectl edit and forgetting to update the manifest in Git, so the next deploy brings the crash back.

Questions I get asked

How long does Kubernetes wait between restarts?

By default the delay starts at 10 seconds and doubles after each failed restart, up to five minutes. It resets after the container has run for ten minutes without a problem. Newer Kubernetes releases have feature gates that shorten this, so check your version's documentation if the timing on your cluster looks different.

Can I force an immediate restart?

kubectl rollout restart deploy/<name> or deleting the pod skips the wait. It is useful once you have fixed the cause, for example after creating a missing Secret. Before that, it only gives you another crash.

Why are the logs empty even with --previous?

Usually because the process never got to write anything: an OOMKill (137), a missing entrypoint (128), or an app that logs to a file inside the container instead of stdout. Check the exit code and the events.

Does exit code 137 always mean out of memory?

No, 137 means the process got SIGKILL. If Reason says OOMKilled, it was memory. If the events show a failed liveness probe, the kubelet killed it after the grace period.


About the author. Muhammad Wilayat Shah is a senior full-stack engineer and team lead based in Islamabad, with 13+ years shipping ERP and SaaS products with Laravel, Node.js, React and Next.js on Linux, Docker and Kubernetes. He wrote this after watching the same CrashLoopBackOff get "fixed" by deleting pods one too many times. More at wilayats.com/about.

Last reviewed: 25 November 2026

ShareLinkedInX
Back to blog

Keep reading

Fix Nginx 413 Request Entity Too Large Error

11/25/2026

Fix Nginx 413 Request Entity Too Large Error

Nginx 413 Request Entity Too Large means a body size limit was hit. Find which layer sent it, then fix client_max_body_size, PHP upload limits, Express and Kubernetes ingress.

Newsletter

Occasional notes on Laravel, React, and shipping production systems — no spam.

Comments

Join the discussion. Replies load on demand, up to 5 levels.

0 threads

Loading editor…
Reviewed before publishing

No comments yet

Be the first to start the conversation.