Sub topic 13
Debugging a Pending pod
This chapter assumes you can apply manifests with kubectl, know what labels and selectors are, and have read the earlier chapters on nodeSelector and node affinity. It shows how to find out why a pod that uses these rules stays in the Pending state.
What Pending means here
A pod is Pending when the scheduler has not yet assigned it to a node. The scheduler filters out nodes that cannot run the pod. If every node is filtered out, the pod waits and the scheduler records the reason as an event. Required rules (nodeSelector and requiredDuringSchedulingIgnoredDuringExecution) filter nodes out. Preferred rules only affect scoring, so they never cause Pending by themselves.
Step 1: Read the events
Start with the pod's events.
kubectl describe pod nginx-ssd
kubectl get nodes --show-labels
kubectl get nodes -l disktype=ssdThe Events section at the bottom of describe shows a FailedScheduling message. For a pod with nodeSelector: {disktype: ssd} and no matching node, it looks like this:
Warning FailedScheduling default-scheduler 0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selector.The message counts nodes per reason. A node can fail for only one reported reason, so the counts add up to the total. The exact wording varies between Kubernetes versions, so look for the words "node affinity/selector".
Step 2: Compare rules with labels
The third command in the block above, kubectl get nodes -l disktype=ssd, is the quickest test. If it returns no nodes, no node can satisfy the rule. If it returns nodes, the selector is not the problem, or another rule is also filtering those nodes out.
Common causes:
- Typo in key or value.
disktype: SSDdoes not matchdisktype=ssd. Matching is case sensitive and exact. - The label was never applied. Run
kubectl label nodes worker-1 disktype=ssd, then check withkubectl get nodes -L disktype. The-Lflag shows the label as a column. - Wrong operator.
NotInandDoesNotExistmatch nodes that lack the key.GtandLtneed a label whose value is an integer. A value such as"high"never matches. - Terms and expressions combined wrongly. Expressions in one term are ANDed. Separate terms are ORed. If you meant "either condition" but wrote two expressions in one term, no node may satisfy both.
nodeSelectorand affinity together. Both must be satisfied. A node that fits the affinity can still fail thenodeSelector.- Labels on the wrong object. Node rules read node labels. Labeling the pod or the namespace has no effect.
Step 3: Fix and confirm
You have two choices. Change the node, or change the pod's rule. Labeling a node takes effect right away, and the scheduler retries Pending pods on its own.
kubectl label nodes worker-1 disktype=ssd
kubectl get pod nginx-ssd -o wideIf you edit the rule, remember that most fields of a running pod cannot be changed. Delete and recreate a bare pod. For a Deployment, edit the pod template and let the rollout create new pods.
Quick reference
| Symptom in events | Likely cause | First action |
|---|---|---|
| `didn't match Pod's node affinity/selector` | No node has the required label | `kubectl get nodes --show-labels` |
| Selector matches nodes, still Pending | Another rule, taint, or resource limit | Read every count in the event |
| Pod runs on an unexpected node | Rule is preferred, not required | Move it to the required rule |
| Pod stays on a node after you remove the label | `IgnoredDuringExecution` | Delete the pod so it reschedules |
Exercise
A pod with nodeSelector: {disktype: ssd} is Pending. kubectl get nodes -l disktype=ssd returns nothing. Which two commands and which event do you use to confirm the cause and fix it?
Show answer
Run kubectl describe pod <name> and look in Events for a FailedScheduling message saying the nodes did not match the Pod's node affinity/selector. Then run kubectl label nodes <node> disktype=ssd to label a suitable node (or correct the pod's selector). The scheduler retries on its own, and kubectl get pod <name> -o wide shows the assigned node.
Exercise
A pod has one required term containing two expressions: zone In [zone-a] and disktype In [ssd]. Your nodes in zone-a have no disktype label, and the nodes in zone-b have disktype=ssd. Why is the pod Pending, and how would you change the rule so either kind of node is accepted?
Show answer
Expressions inside one term are ANDed, so a node needs both zone-a and disktype=ssd, and none does. To accept either, put each expression in its own entry under nodeSelectorTerms, because separate terms are ORed.