Services and Load Balancing
Three notes-web pods, three changing IPs, and clients that should not care about any of them. A Service is the one stable front door that hides the churn and spreads the traffic.
The rule
A Service is a stable name that spreads traffic across the pods its selector finds.
A Service has:
- a selector — which pods it sends traffic to, by label;
- a stable cluster IP that never changes while the Service exists;
- a DNS name such as
notes-web(inside the same namespace) ornotes-web.notes.svc.cluster.local(from anywhere); - a
portthat clients use and atargetPorton the pods.
apiVersion: v1
kind: Service
metadata:
name: notes-web
namespace: notes
spec:
selector:
app: notes-web
ports:
- port: 80
targetPort: 8080Clients call http://notes-web:80. Kubernetes forwards each connection to port 8080 on one of the matching pods. The current list of pod addresses is kept in EndpointSlices, and kube-proxy on each node programs the rules that do the forwarding.
Only ready pods receive traffic. If a pod's readiness probe fails, it is removed from the list until it recovers. This is what makes rolling updates smooth.
Spreading is per connection, not per request. A client that keeps one long-lived connection open may stay with the same pod.
The shortcut
When a Service "doesn't work", check the three matches, left to right: name → selector → port.
Name
Is the client calling
notes-webin the right namespace? From another namespace it needsnotes-web.notes.
The most common cause of an empty Service is a selector that does not match the pod labels exactly — one character is enough.
Drills
Answer fast — trust the rule.
A Service has port: 80 and targetPort: 8080. Which port do clients call?
How does a Service find its pods?
A pod's readiness probe is failing. What happens to Service traffic?
From a pod in namespace tools, which name reaches the notes-web Service?
Remember
- A Service is a stable name and IP that spreads traffic across the ready pods its selector finds.
- Debug with the three matches: name → selector → port.
- Trap: a one-character selector mismatch leaves the Service with no endpoints.
