Sub topic 13
Debugging workloads
This chapter assumes you have a running kind or minikube cluster, can apply a manifest, and know what a Pod and a Deployment are. It walks through a repeatable order for finding out why a workload is failing.
A working model
A workload fails in one of three places. The pod may never start, because it can't be scheduled or its image can't be pulled. The container may start and then crash. Or the container may run but give wrong answers. Each stage leaves evidence in a different place, so you check them in order.
The standard sequence
kubectl get pods
kubectl describe pod <pod-name>
kubectl logs deployment/web
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl exec -it <pod-name> -- shkubectl get podsshows the STATUS and RESTARTS columns. This tells you which stage you are in.kubectl describe podprints the pod's configuration and, at the bottom, its Events. Scheduling failures, image pull errors and failed probes appear there.kubectl logsprints what the container wrote to standard output. Givendeployment/web, kubectl picks one pod of that Deployment, so it shows only one replica.kubectl get eventslists recent cluster events. Sorting by creation time puts the newest last.kubectl exec -itopens a shell inside a running container, so you can check files, environment variables and network access.
Common statuses
| Status | Usual cause | Where to look |
|---|---|---|
| Pending | No node has enough resources, or a volume is missing | `describe`, Events section |
| ImagePullBackOff, ErrImagePull | Wrong image name or tag, or a local image the cluster can't see | `describe`, Events section |
| CrashLoopBackOff | The container starts and exits repeatedly | `logs`, then `logs --previous` |
| Running, 0/1 Ready | The readiness probe is failing | `describe`, then `logs` |
On a local cluster, ImagePullBackOff is often caused by an image built on your machine that was never loaded into the nodes. See the previous chapter on loading images.
Crashed containers
When a container restarts, kubectl logs shows the new instance, which may have no output yet. Add --previous to read the logs of the last terminated instance:
kubectl logs <pod-name> --previousIn describe, the Last State block shows the exit code and reason. Exit code 137 usually means the container was killed, often for exceeding its memory limit (reason OOMKilled).
Service problems
If pods are healthy but the Service doesn't respond, check whether the Service selects any pods:
kubectl get endpoints webAn empty ENDPOINTS column means the Service's selector doesn't match any ready pod's labels. Compare app: web in the Service with the labels in the Deployment's pod template.
Exercise
A pod shows CrashLoopBackOff and kubectl logs <pod-name> prints nothing. What do you run next, and why?
Show answer
Run kubectl logs <pod-name> --previous. The current container has just restarted and has not written output yet, while the previous instance holds the output from the crash. Also check kubectl describe pod <pod-name> for the exit code and reason in Last State.
Exercise
The Service web exists and all pods are Running and Ready, but requests fail. Which command checks whether the Service is connected to the pods, and what does an empty result mean?
Show answer
kubectl get endpoints web. An empty ENDPOINTS column means the Service selector doesn't match the pods' labels, so you should compare the selector with the labels in the pod template.