Kubernetes troubleshooting and debugging: a systematic flow from get to kubectl debug
A practical playbook for investigating broken pods: get, describe, events, logs, exec, and debug, with CrashLoopBackOff, ImagePullBackOff, OOMKilled, Pending, probe failures, and Services without endpoints reproduced for real.
Kubernetes$ kubectl events --for pod/crash▍Everyone has seen a pod stuck in CrashLoopBackOff and started guessing: it’s the image, it’s the network, it’s the cluster. In practice, almost every workload problem in Kubernetes can be solved with six commands used in the right order. In this guide, we reproduce the most common failures on a kind cluster running Kubernetes 1.37 and show the real output of each one, so you recognize the pattern when it shows up in production.
The flow: from general to specific
The idea is to narrow the problem down at each step, without skipping any:
| Step | Command | Question it answers |
|---|---|---|
| 1 | kubectl get pods -o wide |
What is the state? On which node? How many restarts? |
| 2 | kubectl describe pod <pod> |
What was the container’s last state, exit code, and reason? |
| 3 | kubectl events --for pod/<pod> |
What did the scheduler, kubelet, and controllers say? |
| 4 | kubectl logs <pod> / --previous |
What did the application write before it died? |
| 5 | kubectl exec -it <pod> -- sh |
From the inside, is the environment what I think it is? |
| 6 | kubectl debug |
What if the image has no shell or the container won’t even start? |
kubectl events --for is more direct than filtering kubectl get events: it returns only the object’s events, sorted by time. If you are not comfortable with the basic commands yet, read the essential kubectl commands guide first.
The lab: six failures on purpose
We created a namespace with pods that are broken on purpose (the manifests are in the following sections). The first get already separates the cases:
$ kubectl -n d-debug get pods
NAME READY STATUS RESTARTS AGE
crash 0/1 CrashLoopBackOff 5 (68s ago) 4m5s
imagem-errada 0/1 ImagePullBackOff 0 4m5s
oom 0/1 CrashLoopBackOff 5 (66s ago) 4m5s
pendente 0/1 Pending 0 4m5s
probe-falha 0/1 CrashLoopBackOff 6 (10s ago) 4m5s
web-d489c9cb5-l2llf 1/1 Running 0 4m2s
web-d489c9cb5-nsg7p 1/1 Running 0 4m2s
(The pod names are the Portuguese ones from the lab: imagem-errada is “wrong image”, pendente is “pending”, and probe-falha is “probe failure”.) Notice that oom and probe-falha also show up as CrashLoopBackOff. That status only says the kubelet is waiting to restart the container. The cause is one level below.
CrashLoopBackOff: the application exits with an error
apiVersion: v1
kind: Pod
metadata:
name: crash
spec:
containers:
- name: app
image: busybox:1.37
command: ["sh", "-c", "echo 'starting...'; echo 'error: DATABASE_URL not set' >&2; exit 1"]
describe shows the exit code, and the events confirm the restart loop:
$ kubectl -n d-debug describe pod crash
State: Terminated
Reason: Error
Exit Code: 1
Last State: Terminated
Reason: Error
Exit Code: 1
Restart Count: 3
$ kubectl -n d-debug events --for pod/crash
LAST SEEN TYPE REASON OBJECT MESSAGE
31s (x4 over 70s) Normal Started Pod/crash Container started
30s (x3 over 69s) Warning BackOff Pod/crash Back-off restarting failed container app in pod crash_d-debug(...)
$ kubectl -n d-debug logs crash
starting...
error: DATABASE_URL not set
One detail confuses a lot of people: --previous asks for the logs of the previous run. If the current container is running (as when a probe kills the process), --previous is what you want. If the current container has already terminated and is in back-off, plain logs already shows the last run, and --previous may fail with unable to retrieve container logs, because the kubelet keeps only the most recent dead container. Exit code 1 points to the application; 137 points to SIGKILL (memory or probe); 126 and 127 usually mean a wrong command or permission.
ImagePullBackOff: a tag that does not exist
With image: nginx:does-not-exist, the kubelet tries, fails, and waits longer and longer between attempts:
$ kubectl -n d-debug events --for pod/imagem-errada
REASON MESSAGE
Pulling Pulling image "nginx:does-not-exist"
Failed Failed to pull image "nginx:does-not-exist": rpc error: code = NotFound desc = failed to pull and unpack image "docker.io/library/nginx:does-not-exist": failed to resolve reference "docker.io/library/nginx:does-not-exist": docker.io/library/nginx:does-not-exist: not found
Failed Error: ErrImagePull
BackOff Back-off pulling image "nginx:does-not-exist"
Failed Error: ImagePullBackOff
Read the message to the end: not found means a wrong tag or name; unauthorized or 403 means credentials (imagePullSecrets); a timeout means networking or DNS from the node to the registry.
OOMKilled: a memory limit that is too small
command: ["sh", "-c", "tail /dev/zero"]
resources:
limits:
memory: "16Mi"
$ kubectl -n d-debug describe pod oom
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Restart Count: 4
The kernel killed the process when it went over the cgroup limit. Before simply raising the limit, find out whether it is legitimate usage or a leak. The scheduling and resources guide explains how to size requests and limits.
Pending: the scheduler found no node
A pod requesting cpu: "64" never leaves Pending, and there are no logs at all, because no container was created. The answer is in the scheduler’s events:
$ kubectl -n d-debug events --for pod/pendente
TYPE REASON MESSAGE
Warning FailedScheduling 0/3 nodes are available: 1 node(s) had untolerated taint(s), 2 Insufficient cpu. preemption: 0/3 nodes are available: 3 Preemption is not helpful for scheduling.
The message goes node by node: the control plane has a taint and the two workers lack CPU. Other common causes are an impossible nodeSelector or affinity and a PVC with no available volume.
Failing probe: the process is alive, but Kubernetes disagrees
readinessProbe:
httpGet: {path: /healthz, port: 80}
livenessProbe:
httpGet: {path: /, port: 8080}
initialDelaySeconds: 5
failureThreshold: 3
$ kubectl -n d-debug events --for pod/probe-falha
Warning Unhealthy Readiness probe failed: HTTP probe failed with statuscode: 404
Warning Unhealthy Liveness probe failed: Get "http://10.244.1.6:8080/": dial tcp 10.244.1.6:8080: connect: connection refused
Normal Killing Container web failed liveness probe, will be restarted
$ kubectl -n d-debug describe pod probe-falha
Last State: Terminated
Reason: Completed
Exit Code: 0
nginx shut down cleanly (exit 0) because the kubelet told it to stop. A wrong readiness probe takes the pod out of the Service; a wrong liveness probe restarts it in a loop. Here, kubectl logs probe-falha --previous works and shows nginx exiting normally.
Service without endpoints: a selector that does not match
The Deployment creates pods with app: web, but the Service looks for app: frontend:
$ kubectl -n d-debug describe svc web | grep -E 'Selector|Endpoints'
Selector: app=frontend
Endpoints:
$ kubectl -n d-debug get endpointslices -l kubernetes.io/service-name=web
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-pctsb IPv4 <unset> <unset> 2m51s
$ kubectl -n d-debug run curl1 --image=curlimages/curl --restart=Never --command -- curl -sS -m 3 http://web
$ kubectl -n d-debug logs curl1
curl: (7) Failed to connect to web:80 after 0 ms: Could not connect to server
Compare the Service’s selector with kubectl get pods --show-labels. After fixing the selector, the EndpointSlice lists the IPs of both pods and curl gets a 200. To understand the full traffic path, see the introduction to Services.
exec and debug: getting inside the pod
When the image has a shell, kubectl exec -it <pod> -- sh does the job. Distroless images have no shell, and a crashing container does not accept exec. That is what kubectl debug is for.
Ephemeral container with –target
The ephemeral container joins the already running pod and, with --target, shares the process namespace of the target container:
# interactive
kubectl -n d-debug debug -it web-d489c9cb5-l2llf --image=busybox:1.37 --target=nginx
# non-interactive version used in the lab
kubectl -n d-debug debug web-d489c9cb5-l2llf --image=busybox:1.37 --target=nginx \
-- sh -c 'ps; wget -qO- -S http://localhost 2>&1 | head -1; ls /proc/1/root/etc/nginx/'
kubectl -n d-debug logs web-d489c9cb5-l2llf -c debugger-dfnbh
The output shows the nginx processes and the target’s filesystem, reachable through /proc/1/root:
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
34 101 0:00 nginx: worker process
44 root 0:00 sh -c ps; wget -qO- -S http://localhost 2>&1 | head -1; ls /proc/1/root/etc/nginx/
HTTP/1.1 200 OK
conf.d
mime.types
nginx.conf
An ephemeral container cannot be removed once added; it stays in the spec until the pod is recreated. The default --profile in kubectl 1.37 is general; the other options are baseline, restricted, netadmin, and sysadmin.
Copying the pod for containers that won’t start
For the crash pod, it makes more sense to create a copy with a different command and investigate at your own pace:
kubectl -n d-debug debug crash --copy-to=crash-debug --container=app -- sleep 600
kubectl -n d-debug exec crash-debug -- sh -c 'env | grep DATABASE_URL'
In the lab, the grep came back empty, which confirms the missing variable. Delete the copy when you are done.
When the problem is the node
If several pods on the same node fail together, go up one level:
kubectl describe node blog-lab-worker | sed -n '/Conditions:/,/Addresses:/p'
kubectl -n d-debug debug node/blog-lab-worker --image=busybox:1.37 --profile=sysadmin \
-- chroot /host sh -c 'systemctl is-active kubelet containerd; df -h /; journalctl -u kubelet -n 20'
We tested kubectl debug node/... on kind and it works: the pod starts on the node with the host’s root mounted at /host. Careful: it is created in the current namespace, so pass -n and delete the node-debugger-... pod at the end. What to check on the node:
- Conditions:
Ready,MemoryPressure,DiskPressure, andPIDPressure. - Services: kubelet and the container runtime active, with
journalctl -u kubelet. - Disk: space and inodes; a full disk triggers evictions.
- Containers:
crictl psandcrictl podsto see what the runtime sees. - Allocated resources: the
Allocated resourcessection ofdescribe node.
Without metrics-server, kubectl top answers error: Metrics API not available. Deeper performance investigations (the USE method, CPU throttling, conntrack) are in the deep infrastructure troubleshooting guide.
Checklist
- Start with
get -o wideand only then move down todescribeand events - Use
kubectl events --for pod/<name>instead of scanning every event - Read the exit code and
ReasonofLast Statebefore looking at logs - Use
logs --previouswhen the current container has already restarted and is running - Compare the Service selector with the pod labels and check the EndpointSlice
- Use
kubectl debug --targetfor images without a shell and--copy-tofor crashing containers - Remove debug pods and copies when you are done
Debugging Kubernetes is not guesswork: the cluster has already recorded what happened, and the job is to read it in the right order.