Troubleshooting
Drill until fluent: hands-on rounds test this directly. Phase 4 of the Learning Path.
Docs: Debug Pods Β· Debug Services Β· Debug running Pods Β· Learnk8s visual guide
Overviewβ
Debugging Kubernetes is a walk down the six-step chain behind every kubectl apply. kubectl get pods tells you which step broke; the STATUS column is the index into this page. The method is always the same: read the status, run the one command that explains that status, fix the cause, and confirm with the same command. Guessing, or restarting things to see what happens, wastes the most time in the exam and in interviews.
Figure 13-1: Troubleshooting decision tree. Amber marks the one command to run first for each status.
Key conceptsβ
Status β first command β usual causesβ
| Status | Stage that failed | First command | Usual causes |
|---|---|---|---|
| No Pods at all | Admission | kubectl describe rs (or describe job) | ResourceQuota exceeded, Pod Security violation, bad ServiceAccount |
Pending | Scheduling | kubectl describe pod β Events | Requests too big, taints, nodeSelector/affinity, PVC not bound |
ContainerCreating (stuck) | Volume attach, network setup | kubectl describe pod β Events | Missing ConfigMap/Secret volume, Multi-Attach error, CNI failure |
ErrImagePull / ImagePullBackOff | Image pull | kubectl describe pod β Events | Tag typo, private registry without imagePullSecrets, rate limits |
CreateContainerConfigError | Container config | kubectl describe pod β Events | Missing ConfigMap/Secret or key, runAsNonRoot with a root image |
RunContainerError | Container start | kubectl describe pod | Command not found in image, bad mount path |
CrashLoopBackOff | Running | kubectl logs --previous | App error at startup, missing config, liveness probe killing it |
OOMKilled (in Last State) | Running | kubectl describe pod | Memory limit too low, leak |
Running, 0/1 Ready | Readiness | kubectl describe pod β probe events | Wrong probe path/port, dependency down, too slow |
Running, unreachable | Service routing | kubectl describe svc β Endpoints | Selector/label mismatch, wrong targetPort, NetworkPolicy |
Terminating (stuck) | Deletion | kubectl get pod -o yaml β finalizers | Finalizer controller gone, node unreachable |
Exit codesβ
| Code | Meaning | Usual cause |
|---|---|---|
| 0 | Clean exit | A long-running app exiting means it has nothing to do: wrong command, or a Job in a Deployment |
| 1 | App error | Read the logs |
| 126 | Command not executable | Permissions, wrong architecture binary |
| 127 | Command not found | Typo in command, or the binary isn't in the image |
| 137 | SIGKILL (128+9) | OOMKilled, or killed after the grace period |
| 139 | SIGSEGV (128+11) | Crash in native code |
| 143 | SIGTERM (128+15) | Normal shutdown during a rollout or scale-down |
Timeout vs refusedβ
| Symptom from a client Pod | Likely cause |
|---|---|
bad address / could not resolve host | Name doesn't exist: typo, or the Service is in another namespace |
Connection refused | Service has no endpoints (kube-proxy rejects), or wrong targetPort |
| Timeout | NetworkPolicy drop (a blocked DNS lookup looks like this too), or the app accepted but hangs |
β See Services & Ingress and Network Policy.
kubectl essentialsβ
The core loop:
kubectl get pods -o wide # STATUS, RESTARTS, node, IP
kubectl describe pod <pod> # Events at the bottom: read them first
kubectl logs <pod> --previous # output of the crashed container
kubectl get events --sort-by=.lastTimestamp --field-selector type=Warning
kubectl get pod <pod> -o yaml | less # the full spec and status as the API sees it
When no Pods appear:
kubectl describe deployment <name> # Conditions: ReplicaFailure
kubectl describe rs -l app=<name> # FailedCreate events: quota, Pod Security
Get inside:
kubectl exec -it <pod> -- sh # if the image has a shell
kubectl debug -it <pod> --image=busybox:1.36 --target=<container> # ephemeral container, shares process namespace
kubectl debug <pod> -it --copy-to=dbg --container=<container> -- sh # copy of the Pod with a shell instead of the app
kubectl debug node/<node> -it --image=busybox:1.36 # shell on the node, host fs at /host
kubectl port-forward pod/<pod> 8080:8080 # bypass Service and Ingress
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 3 http://<svc>
π§ͺ Labβ
Requires: Standard lab setup Β· Config & Secrets Β· Services & Ingress Β· How the tiers work.
Three bugs, one manifest.
Goal
Save this as broken.yaml and apply it to namespace lab4-1. Make wget -qO- http://web from a Pod in the namespace return the nginx welcome page. Fix every problem using only get, describe, logs and edit/set/patch, and write down the status that led you to each fix.
apiVersion: v1
kind: ConfigMap
metadata:
name: web-conf
data:
MODE: prod
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels: {app: web}
template:
metadata:
labels: {app: web}
spec:
containers:
- name: nginx
image: nginx:1.27-alpinee
env:
- name: MODE
valueFrom:
configMapKeyRef: {name: web-config, key: MODE}
ports: [{containerPort: 80}]
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector: {app: webapp}
ports:
- port: 80
targetPort: 80
Verify
kubectl -n lab4-1 get pods # 2/2 Running, 1/1 Ready each
kubectl -n lab4-1 run tmp --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 3 http://web | head -4
π‘ Hints
- Start with
kubectl get podsand look up the STATUS in the status table. It tells you the first command to run. - Two of the bugs show up as Pod statuses, one after the other. The third only shows up when you call the Service.
- Fix at the owner:
kubectl set imageandkubectl patch deployment, not the Pods. - For the Service, compare
describe svcβSelectorwithget pods --show-labels.
π’ Guided
-
Create the lab namespace.
kubectl create namespace lab4-1 -
Apply the broken manifest.
kubectl -n lab4-1 apply -f broken.yaml -
Read the status: ErrImagePull / ImagePullBackOff.
kubectl -n lab4-1 get pods -
Read the first Pod's events: Failed to pull image
nginx:1.27-alpinee: not found (tag typo).kubectl -n lab4-1 describe pod -l app=web | grep -A3 Events -m1 -
Fix the tag on the Deployment.
kubectl -n lab4-1 set image deployment/web nginx=nginx:1.27-alpine -
Read the status again: CreateContainerConfigError.
kubectl -n lab4-1 get pods -
Find the message: configmap "web-config" not found (the ConfigMap is called web-conf).
kubectl -n lab4-1 describe pod -l app=web | grep -i configmap -
Point the env var at the right ConfigMap.
kubectl -n lab4-1 patch deployment web --type=json \-p '[{"op":"replace","path":"/spec/template/spec/containers/0/env/0/valueFrom/configMapKeyRef/name","value":"web-conf"}]' -
Wait for the fixed Pods.
kubectl -n lab4-1 rollout status deployment/web -
Call the Service: Connection refused.
kubectl -n lab4-1 run tmp --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 3 http://web -
See why:
app=webapp, Endpoints:<none>.kubectl -n lab4-1 describe svc web | grep -E "Selector|Endpoints" -
Fix the selector; Verify now succeeds.
kubectl -n lab4-1 patch svc web -p '{"spec":{"selector":{"app":"web"}}}' -
Run the β Check below, then delete everything the lab created.
kubectl delete namespace lab4-1
β Check
t() { [ "$2" = "$3" ] && echo "PASS $1" || echo "FAIL $1: got '$2', want '$3'"; }
kubectl -n lab4-1 run chk --image=busybox:1.36 --restart=Never -- sleep 600 2>/dev/null; kubectl -n lab4-1 wait --for=condition=Ready pod/chk >/dev/null
t "2 Pods Ready" "$(kubectl -n lab4-1 get deploy web -o jsonpath='{.status.readyReplicas}')" "2"
t "web answers" "$(kubectl -n lab4-1 exec chk -- wget -qO- -T 3 http://web | grep -o '<title>.*</title>')" "<title>Welcome to nginx!</title>"
β More drills in the same format: Troubleshooting Drills.
Gotchasβ
- Read Events before logs. Most failures before
Runningnever produce app logs at all. - No Pods means look one level up. Admission errors land on the ReplicaSet or Job, not on a Pod that doesn't exist.
CrashLoopBackOffwaits grow to 5 minutes. After a fix,kubectl delete podskips the wait.--previousor nothing. The current container's log starts empty after each restart.- Events expire after about an hour. For overnight failures, use the log system.
- Distroless images have no shell.
kubectl exec -- shfails. Usekubectl debug --target. - Fix the owner. Editing a Pod owned by a Deployment is undone. Fix the Deployment.
Scenario questionsβ
Q1 β
A Deployment shows 0/3 ready and kubectl get pods shows nothing. Where are the Pods?
Model answer
- Clarify: new Deployment, or did this start after a namespace change (a quota or Pod Security label)?
- Observe:
kubectl describe deploymentβReplicaFailure.kubectl describe rsβFailedCreate: exceeded quotaorviolates PodSecurity. - Hypothesise: admission rejects every Pod the ReplicaSet tries to create. Nothing exists to show.
- Fix: add requests the quota needs (or raise the quota), or fix the securityContext.
- Prevent: teach the team to go one level up when Pods are missing. Alert on
FailedCreateevents.
Q2 β
β
A Pod is Pending with 0/3 nodes are available: 1 node(s) had untolerated taint {dedicated: gpu}, 2 Insufficient memory. Explain and fix.
Model answer
- Clarify: is this Pod meant for the GPU node? How much memory does it request?
- Observe: the message covers every node: one is tainted for GPU workloads, two lack free memory (by requests, not usage).
- Hypothesise: a regular Pod with a large memory request and no GPU toleration. Nowhere is legal.
- Fix: right-size the memory request, or add capacity. Only add a toleration if it really belongs on the GPU node.
- Prevent: check requests against
kubectl describe nodeallocatable; let the cluster autoscaler add nodes for legitimate demand.
Q3 β
β
A container crashes instantly and kubectl logs --previous is empty. What next?
Model answer
- Clarify: new image or new command/args?
- Observe:
kubectl describe podβ Last State exit code. 127 is command not found, 126 not executable, 0 means the process simply finished. - Hypothesise: the process dies before writing anything: wrong entrypoint, a missing binary, or a script that exits.
- Fix: start a copy with a shell and inspect it:
kubectl debug <pod> -it --copy-to=dbg --container=<name> -- sh, then run the command by hand. - Prevent: smoke-test images in CI by running their real entrypoint.
Q4 β β β Users report the app is "slow sometimes". Nothing is crashing. How do you approach it?
Model answer
- Clarify: which endpoints, how slow, since when, all users or some? Is there a pattern: time of day, after deploys?
- Observe:
kubectl top podsagainst requests and limits, restart counts, readiness flapping in events, HPA activity (kubectl describe hpa), and whether slow requests come from particular Pods or nodes. - Hypothesise: CPU throttling from limits; one bad node; Pods flapping in and out of endpoints; cold Pods after HPA scale-out without warm-up; a slow dependency.
- Fix: follow the evidence. Throttling β remove or raise CPU limits (β Resources & Scaling); cold starts β readiness that waits for warm-up; one bad node β cordon and drain.
- Prevent: per-Pod latency metrics and tracing.
kubectlshows the present; intermittent problems need history.
Summaryβ
- STATUS picks the command. Pending and pull errors β describe; crashes β logs
--previous; unreachable β describe svc. - Events explain everything before
Running. Logs explain everything after. - Missing Pods mean admission. Describe the ReplicaSet.
- Exit code 137 is a kill, usually OOM. 127 is a missing command.
- Walk the chain in order. Admission, scheduling, pull, start, readiness, Service. Stop at the first broken link.
Next in path β Troubleshooting Drills