Sub topic 7
How Services Find Pods
This chapter assumes you know what Pods, Deployments, and Services are, can read YAML manifests, and can run basic kubectl commands. It covers how a Service uses a label selector to decide which Pods receive traffic, and how to check that the match worked.
The working model
A Service has a stable virtual IP and DNS name. It does not hold a list of Pods. Instead, its spec.selector is a set of label key-value pairs. Kubernetes continuously finds every Pod in the same namespace whose labels contain all of those pairs. It then publishes the ready Pods' IP addresses as endpoints, and traffic sent to the Service is spread across them.
The dotted line shows the Pod that is not selected. Its labels do not contain app=web, so it never becomes an endpoint.
A Service and its Pods
The Deployment below creates Pods labeled app: web. The Service selects that label.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.27
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Note these details:
- The Service selector is a plain map. It supports only equality, and every pair must match (logical AND). It has no
matchExpressionsand noinornotinoperators. - Extra labels on a Pod, such as
tier: frontend, do not prevent a match. The Pod only needs to have at least the labels the selector lists. - The Service selects Pods, not Deployments. It works with Pods created by any means, including ones from several Deployments that share a label.
- Matching is limited to the Service's own namespace.
Labels change membership live
Because matching is continuous, relabeling a Pod changes where traffic goes without restarting anything. Removing the label app from one Pod takes it out of the Service. The owning ReplicaSet may then create a replacement, because that Pod no longer matches the Deployment's selector either. Pods can be pulled out of rotation this way for debugging.
Debugging a Service with no endpoints
When requests to a Service fail, compare what the Service asks for with what the Pods have.
kubectl get service web -o jsonpath='{.spec.selector}'
kubectl get endpoints web
kubectl get pods -l app=web --show-labels- The first command prints the selector, for example
{"app":"web"}. - The second lists the endpoint addresses.
<none>means no ready Pod matched. - The third runs the same selector against Pods. If it returns nothing, the labels differ from the selector.
Common causes:
| Symptom | Likely cause |
|---|---|
| Endpoints show `<none>`, no Pods listed by the selector | Typo or different value, such as `app: webb` or `App: web` (keys and values are case-sensitive) |
| Pods match, but endpoints are still empty | Pods are not Ready, so a failing readiness probe keeps them out |
| Endpoints exist, but connections fail | `targetPort` does not match the port the container listens on |
| Pods are running in another namespace | The Service only selects within its own namespace |
Exercise
A Service has selector: {app: web, tier: frontend}. A Pod has the labels app: web and tier: backend. Is the Pod selected, and how would you confirm it with kubectl?
Show answer
No. A Service selector requires every listed pair to match, and tier differs. Confirm with kubectl get pods -l app=web,tier=frontend --show-labels (the Pod will not appear) and kubectl get endpoints <service> (its address will be absent).