Sub topic 14
Inspecting and Debugging Pods with kubectl
This chapter assumes you can read a Pod manifest, know the Pod phases and container states, and have kubectl configured against a cluster. It uses a Pod named web as the example. It covers a repeatable way to find out why a Pod is not working.
The debugging loop
Work from the outside in. Check the status first, then the events, then the logs. Only then open a shell.
The core commands
kubectl apply -f pod.yaml
kubectl get pod web -o wide
kubectl describe pod web
kubectl logs web -c nginx
kubectl logs web -c nginx --previous
kubectl exec -it web -- sh
kubectl debug -it web --image=busybox:1.36 --target=nginx
kubectl delete pod webget pod -o wideshows the STATUS, the restart count, the Pod IP, and the node.describe podshows container states, the last termination reason and exit code, and the Events list at the bottom. Events are where scheduling and image-pull errors appear.logs -c <name>reads one container's output. You need-cwhen the Pod has more than one container.logs --previousreads the output of the last terminated instance. Use it when a container keeps restarting, because the current instance may have no output yet.execruns a command inside a running container.debugadds an ephemeral container to a running Pod. Use it when the image has no shell.--targetlets it share the process namespace of the named container.
Common failures
| STATUS | Typical cause | Where to look |
|---|---|---|
| Pending | No node fits the requests, or a volume cannot bind | Events in `describe` |
| ImagePullBackOff, ErrImagePull | Wrong image name or tag, or missing registry credentials | Events in `describe` |
| CrashLoopBackOff | The process exits repeatedly and the kubelet delays each restart | `logs --previous`, exit code |
| CreateContainerConfigError | A referenced ConfigMap or Secret key is missing | Events in `describe` |
| OOMKilled (last state reason) | The container exceeded its memory limit | `describe`, Last State |
| Running but not Ready | The readiness probe is failing | Events and probe settings |
Exit codes help. Code 137 usually means the process was killed with SIGKILL, often by the out-of-memory killer. Code 1 usually means the application itself failed.
Worked example
Suppose you change the image to nginx:1.99 and apply it. The Pod shows ImagePullBackOff. Running kubectl describe pod web shows an event such as Failed to pull image "nginx:1.99", and kubectl logs prints nothing because the container never started. The fix is a valid tag. Most fields of a running Pod cannot be edited, but the image is one of the exceptions, so you can run kubectl set image pod/web nginx=nginx:1.27.
Exercise
A Pod shows CrashLoopBackOff, and kubectl logs web prints nothing. Which command do you run next, and why?
Show answer
Run kubectl logs web --previous (adding -c <container> if there is more than one). The current container has just restarted and may have written no output, while the previous instance holds the output from before it crashed. Then check kubectl describe pod web for the exit code and last termination reason.