Sub topic 1
How a Service selects Pods
Prerequisites
This chapter assumes you can run basic kubectl commands (get, describe, logs, exec). It also assumes you know what Pods, Deployments, and namespaces are, and that you can read a YAML manifest. You do not need to know anything about DNS or kube-proxy yet. Later chapters cover them.
The working model
A Service does not hold a list of Pods that you maintain by hand. It holds a label selector. A label is a key-value pair attached to an object, such as app: web. A selector is a query that matches objects by their labels.
Kubernetes keeps watching the Pods in the Service's namespace. Every Pod whose labels match the selector, and which is ready, becomes a backend for the Service. If a Pod is added, deleted, or relabeled, the backend list changes without any edit to the Service.
This is why most "the Service doesn't work" problems start with labels. If the selector matches nothing, the Service still exists and still gets an IP address. Requests to it fail because there is nowhere to send them.
A working example
The Deployment below labels its Pods in spec.template.metadata.labels. The Service selects those labels.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 8080
targetPort: 80Three label sets are involved, and they have different jobs:
| Location | Field | Purpose |
|---|---|---|
| Deployment | `spec.selector.matchLabels` | Which Pods the Deployment owns |
| Pod template | `spec.template.metadata.labels` | Labels stamped onto each Pod |
| Service | `spec.selector` | Which Pods receive Service traffic |
The Deployment selector must match the Pod template labels, or the API server rejects the Deployment. The Service selector is not checked against anything. A typo in it is accepted without any error.
Checking the match
To see what the Service selects, compare its selector with the labels on the Pods:
kubectl get service web -o jsonpath='{.spec.selector}'
kubectl get pods --show-labels
kubectl get pods -l app=webIf the last command returns no Pods, the selector matches nothing. All the labels in a selector must match (they are combined with AND). A selector of app: web, tier: frontend does not match a Pod that only has app: web.
Common errors
- Typo in a key or value.
app: webborApp: web. Label keys and values are case-sensitive. - Wrong namespace. A Service only selects Pods in its own namespace. A matching Pod in another namespace is ignored.
- Labels on the wrong object. Labels set in the Deployment's
metadata.labelsdo not reach the Pods. Onlyspec.template.metadata.labelsdoes. - Extra selector keys. A leftover key such as
version: v1excludes Pods that were redeployed without it.
Try it
Apply the manifest, then break the selector on purpose. Run kubectl patch service web -p '{"spec":{"selector":{"app":"webb"}}}'. Then run kubectl get pods -l app=webb. The empty result confirms the cause. Patch it back to app: web to fix it.
Exercise
A Service has the selector app: web, tier: frontend. Your Pods are labeled only app: web and are Running. Will the Service send traffic to them? How would you confirm it?
Show answer
No. A selector matches only Pods that carry every listed label, and these Pods lack tier: frontend. To confirm, run kubectl get pods -l app=web,tier=frontend. It returns nothing, while kubectl get pods -l app=web returns the Pods. Fix it by adding the missing label to the Pod template or removing it from the Service selector.