Prompt
How do I troubleshoot repeated container restarts in Kubernetes?
Latest observation
Repeated container restarts in Kubernetes usually mean the process in the container is crashing, failing liveness/readiness probes, being OOM-killed, or getting terminated by the platform. Here’s a practical troubleshooting flow.
1) Identify which pod and container are restarting
kubectl get pods -A
kubectl get pod <pod-name> -n <namespace> -o wide
Check restart count:
kubectl describe pod <pod-name> -n <namespace>
Look for:
Restart CountLast StateExit CodeReason- probe failures
OOMKilled
2) Inspect logs from the current and previous container
Current logs:
kubectl logs <pod-name> -n <namespace> -c <container-name>
Previous crashed instance:
kubectl logs <pod-name> -n <namespace> -c <container-name> --previous
If the app starts and crashes quickly, --previous is often the most useful.
3) Check pod events
kubectl describe pod <pod-name> -n <namespace>
At the bottom, look at Events for clues such as:
Back-off restarting failed containerLiveness probe failedReadiness probe failedOOMKilled- image pull issues
- mount/volume errors
4) Determine the exit reason
Common ones:
Exit Code 1: app error or misconfigurationExit Code 137: often OOMKilled or SIGKILLExit Code 143: SIGTERM, often graceful shutdownOOMKilled: memory limit too low or memory leakCrashLoopBackOff: container repeatedly exits and Kubernetes is backing off
You can inspect:
kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.status.containerStatuses[*].lastState.terminated}'
5) Check liveness/readiness probes
Bad probe configuration is a very common cause of restarts.
Inspect deployment:
kubectl get deployment <deploy-name> -n <namespace> -o yaml
Look for:
livenessProbereadinessProbestartupProbe
Common issues:
- probe hits the wrong path/port
- app takes longer to start than probe timeout
- probe depends on downstream services that aren’t ready
- no
startupProbefor slow-starting apps
If startup is slow, consider adding a startupProbe and relaxing liveness thresholds.
6) Check resource limits and node pressure
If the container is killed for memory:
kubectl describe pod <pod-name> -n <namespace>
Look for OOMKilled.
Review resource requests/limits:
kubectl get deployment <deploy-name> -n <namespace> -o yaml | grep -A5 -B5 resources
Check node status:
kubectl describe node <node-name>
Look for:
- MemoryPressure
- DiskPressure
- PIDPressure
- evictions
7) Verify configuration and secrets
Crash loops often come from missing or bad config.
Check:
- environment variables
- ConfigMaps
- Secrets
- file mounts
- command/args overrides
Examples:
kubectl get configmap -n <namespace>
kubectl get secret -n <namespace>
kubectl describe pod <pod-name> -n <namespace>
A typo in an env var name or a bad secret value can cause startup failure.
8) Check for dependency failures
If the app depends on DBs, caches, or APIs, it may exit if they’re unavailable.
Look for logs indicating:
- connection refused
- authentication failure
- DNS resolution failure
- timeout
Test connectivity from inside the pod:
kubectl exec -it <pod-name> -n <namespace> -- sh
Then try:
nslookup <service>curl <endpoint>nc -vz <host> <port>
9) Compare with deployment history
See if a recent rollout introduced the issue:
kubectl rollout history deployment/<deploy-name> -n <namespace>
kubectl rollout status deployment/<deploy-name> -n <namespace>
You can roll back if needed:
kubectl rollout undo deployment/<deploy-name> -n <namespace>
10) If it only happens on certain nodes, inspect the node
Sometimes the pod is fine but a specific node has issues.
Check where the pod lands:
kubectl get pod <pod-name> -n <namespace> -o wide
Then inspect:
kubectl describe node <node-name>
kubectl get events -A --sort-by=.lastTimestamp
Look for:
- node reboot or drain
- kubelet errors
- disk full
- network issues
11) Use an ephemeral/debug container if the app won’t stay up
If the image includes shell tools or you can use a debug container:
kubectl debug -it pod/<pod-name> -n <namespace> --image=busybox --target=<container-name>
This helps inspect:
- filesystem
- config files
- networking
- DNS
- process behavior
12) Common root causes checklist
- Application exception on startup
- Missing env var / secret / config file
- Incorrect command or args
- Bad probe configuration
- Out of memory / memory leak
- Dependency not ready
- File permission or volume mount issue
- Image mismatch or incompatible binary
- Node resource pressure
- Crash due to signal or fatal error
Quick triage commands
kubectl describe pod <pod> -n <ns>
kubectl logs <pod> -n <ns> --previous
kubectl get pod <pod> -n <ns> -o wide
kubectl get events -n <ns> --sort-by=.lastTimestamp
kubectl describe node <node>
If you want, I can also give you:
- a step-by-step decision tree for CrashLoopBackOff, or
- a scripted checklist you can run during incident response.
Brands