Kubernetes Error: CrashLoopBackOff
Fix the Kubernetes CrashLoopBackOff error when a container repeatedly starts, crashes on initialization, and enters an exponential backoff delay.
Symptoms
kubectl get podsshows pod statusCrashLoopBackOfforError.- The
RESTARTScount increments rapidly (e.g. 5, 10, 25 restarts). - Pod fails to transition into the
RunningorReadystate.
Why It Happens
CrashLoopBackOff means the application process inside the container started, encountered a fatal runtime exception, and exited with a non-zero exit code. Kubernetes attempts to restart the container, applying exponential backoff delay (10s, 20s, 40s, up to 5 minutes) to protect cluster nodes.
Common causes:
- Missing or Invalid Environment Variables: Application requires a database connection string or secret key that was not supplied in the PodSpec.
- Database Connection Failure: The backend database or Redis service is unreachable or not yet ready.
- Application Fatal Exception: Node.js/Python uncaught syntax or import error during startup.
- Failing Liveness Probe: Liveness probe endpoint returns HTTP 500 or times out during initialization.
- Entrypoint Execution Failure: File permission errors on startup script or missing executable binary.
How to Diagnose
```bash
kubectl logs <pod-name> --previous
```Check `Last State -> Terminated -> Exit Code` and `Reason`.Solutions & Fixes
1. Fix Missing Environment Variables
Verify all required environment variables are defined in the Deployment manifest:
spec:
containers:
- name: api
image: my-app:1.0.0
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: app-secrets
key: DATABASE_URL2. Add Init Container to Wait for Database Readiness
Prevent the main application from starting before the database is listening:
spec:
initContainers:
- name: wait-for-postgres
image: busybox:1.36
command: ['sh', '-c', 'until nc -z -w 2 postgres-service 5432; do echo waiting for postgres; sleep 2; done;']
containers:
- name: api
image: my-app:1.0.03. Adjust Liveness Probe Initial Delay
If your app takes 15 seconds to boot, increase initialDelaySeconds:
livenessProbe:
httpGet:
path: /api/healthz
port: 3000
initialDelaySeconds: 20
periodSeconds: 10Related Topics
Prisma Error P1001: Can't reach database server
Troubleshoot and resolve Prisma connection errors when the database server is offline, credentials are wrong, or network firewalls block traffic.
Kubernetes Error: ImagePullBackOff / ErrImagePull
Fix ImagePullBackOff and ErrImagePull errors caused by non-existent container images, invalid tags, private registry authorization failures, or rate limits.