Sub topic 14
Debugging Selectors That Match Nothing
This chapter assumes you know Pods, Deployments, and Services, can read YAML, and have used basic kubectl commands. It also assumes you know how equality-based and set-based selectors work.
A selector that matches nothing rarely produces an error. A Service with no matching Pods is valid, and so is a kubectl get -l that returns an empty list. You see the symptom later, as timeouts or connection refusals. Debugging means comparing what the selector asks for with the labels the objects actually carry.
The core method
Work from the selector outward:
- Read the selector on the object that should find Pods.
- List the Pods you expect it to find, with their labels.
- Compare key by key, including spelling and case.
- Check the result the selector produced, such as Service endpoints.
The sequence below does this for a Service named web.
kubectl get service web -o jsonpath='{.spec.selector}'
kubectl get endpoints web
kubectl get pods -l app=web --show-labelsThe first command prints the selector, for example {"app":"web"}. The second shows the endpoints the control plane built from it. If the ENDPOINTS column says <none>, no Ready Pod matched. The third command tests the same selector against Pods directly. If it returns nothing, the labels are wrong. If it returns Pods, the problem is elsewhere (see below).
Common causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Endpoints empty, `-l` returns nothing | Typo or case difference in a key or value | Correct the label or the selector |
| Endpoints empty, `-l` returns Pods | Pods not Ready | Check readiness probes with `kubectl describe pod` |
| Selector works in one namespace only | Service and Pods are in different namespaces | Selectors never cross namespaces |
| Deployment rejected | `spec.selector` does not match `template.metadata.labels` | Make the template labels satisfy the selector |
| Traffic stops after a label edit | Someone removed or changed a label on a Pod | Restore the label with `kubectl label --overwrite` |
Keys are case-sensitive, so App=web and app=web are different labels. A value of "true" is a string, so write it quoted in YAML.
Reading the Deployment selector
A Deployment's selector must be satisfied by its Pod template labels. The matchLabels and matchExpressions entries are combined with AND.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
matchExpressions:
- key: tier
operator: In
values: [frontend]
template:
metadata:
labels:
app: web
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.27If the template omitted tier, the API server would reject the Deployment. After creation, the selector of a Deployment is immutable in apps/v1, so you must recreate the Deployment to change it.
Label edits that detach Pods
Changing a label on a running Pod can take it out of a Service or a controller's reach. Removing the label a ReplicaSet selects on makes the ReplicaSet stop counting that Pod, so it creates a replacement. The edited Pod keeps running, orphaned. This is a handy way to pull one Pod out for inspection, but it also explains Pods that "disappear" from a Deployment.
kubectl get pods --show-labels
kubectl label pod web-1 app-
kubectl get pods -l app=webOther places that match nothing
A nodeSelector that no node satisfies leaves the Pod Pending. kubectl describe pod shows a scheduling event saying no nodes matched the node selector. Compare it with kubectl get nodes --show-labels.
Exercise
A Service api has selector app: api. kubectl get endpoints api shows <none>, but kubectl get pods -l app=api lists three Pods with status Running and 0/1 Ready. What is the likely cause, and what do you check next?
Show answer
The selector matches, so labels are not the problem. The Pods are not Ready, and Services only publish Ready Pods as endpoints. Run kubectl describe pod on one of them and look at the readiness probe and its events.
Exercise
You create a Deployment whose selector is app: shop but whose template labels are app: store. What happens?
Show answer
The API server rejects the Deployment, because the selector must match the Pod template labels. Change either the selector or the template label so they agree.