Lesson 3 of 11 · 8 min · Core objects
Services: stable access to changing Pods
What you will learn
- Why you need Services
- Defining a Service: selector, ports,
portvstargetPort - How Services find Pods (labels, EndpointSlices) and balance load
- Reaching a Service by name from another Pod
- Debugging "the Service doesn't work"
In simple words
Pods come and go, and each new one gets a new address, so nothing can rely on a Pod's address. A Service is a fixed front desk with one name and one address that never change. It passes each request to whichever healthy Pods are behind it at that moment.
Words to know
- Service: a stable name and address for a group of Pods.
- Selector: how a Service chooses its Pods: by their labels.
- Endpoint: the address of one Pod that is currently behind the Service.
- Port and targetPort: the door callers use on the Service, and the door on the Pod that it forwards to.
The problem
Pods come and go, and each new Pod has a new IP. A web front end cannot keep a list of back-end Pod IPs. A Service is a stable front: a fixed virtual IP (ClusterIP) and a DNS name that forwards to whichever healthy Pods currently match its selector.
client Pod ──▶ Service "web" 10.43.79.167:80 ──▶ Pod A 10.42.0.8:80
(stable) ├─▶ Pod B 10.42.0.9:80
└─▶ Pod C 10.42.0.10:80
Creating a Service
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80
selector: Pods with the labelapp: webreceive the traffic. It is the link between Service and Pods; there is no other reference between them.port: the port the Service listens on.targetPort: the port on the Pods (a number, or the name of acontainerPort). They are often different:port: 80,targetPort: 8080for an app that listens on 8080.type: defaults toClusterIP(internal only). Other types in the networking lessons.
Faster: kubectl expose deployment web --port=80 --target-port=80. What we got:
NAME TYPE CLUSTER-IP PORT(S)
web ClusterIP 10.43.79.167 80/TCP
How it works
- The control plane keeps a list of endpoints for each Service: the IPs of Pods that match the selector and are Ready (readiness probes!). It is stored as EndpointSlice objects (the older
Endpointsobject is deprecated, whichkubectlwarns about). Our Service had three endpoints for three Pods. - kube-proxy on every node programs rules (iptables or IPVS) so that a connection to the ClusterIP is redirected to one of the endpoints. The ClusterIP is not a real interface: it exists only as rules.
- Load balancing is per connection, roughly random, not per request and not aware of load. A client that keeps one long-lived connection (HTTP keep-alive, gRPC) sticks to one Pod. For per-request balancing use an L7 proxy, service mesh or client-side balancing.
Proof of balancing: we made each Pod return its own name, then called the Service six times from another Pod:
for i in 1 2 3 4 5 6; do kubectl exec client -- wget -qO- http://web/id.txt; done | sort | uniq -c
1 pod web-57b5dddfb4-kfkzp
1 pod web-57b5dddfb4-ptcxt
4 pod web-57b5dddfb4-xhj6g
All three Pods were used, though not evenly: random choice, not round robin. (Do not use this to test strict distribution.)
Reaching a Service
From any Pod in the cluster, use the Service name; cluster DNS resolves it (details in the DNS lesson):
- same namespace:
http://web - other namespace:
http://web.other-namespaceor the full nameweb.other-namespace.svc.cluster.local
Kubernetes also injects environment variables like WEB_SERVICE_HOST into Pods created after the Service; prefer DNS.
Debugging a Service: no endpoints
The most common failure is a selector that matches no Pods. We changed the selector to a label nobody has:
kubectl patch svc web -p '{"spec":{"selector":{"app":"nope"}}}'
kubectl get endpoints web
NAME ENDPOINTS
web <none>
Traffic to such a Service gets "connection refused" or hangs. The checklist:
kubectl get endpointslices -l kubernetes.io/service-name=web(orkubectl get endpoints web): are there addresses?- If none:
kubectl get pods --show-labelsand compare withkubectl get svc web -o yaml→selector. Typos and label drift are the usual cause. - If Pods match but are not listed: they are not Ready (failing readiness probe, still starting).
kubectl get pods,kubectl describe pod. - If endpoints exist: is
targetPortthe port the container really listens on? Test from inside:kubectl exec client -- wget -qO- http://<pod-ip>:<targetPort>. - Is a NetworkPolicy blocking it? (network policy lesson)
- Is the app listening on
127.0.0.1only? It must bind0.0.0.0(Docker lesson).
Headless and special Services
- Headless Service (
clusterIP: None): no virtual IP and no load balancing; DNS returns the individual Pod IPs. Used by StatefulSets so thatdb-0.dbaddresses a specific Pod, and by clients that do their own balancing. - ExternalName: a DNS alias to an outside name (
externalName: example.com), so in-cluster code can use a stable internal name for an external dependency. - A Service without a selector can be paired with hand-made endpoints to point at external IPs.
Sticky sessions and ports
sessionAffinity: ClientIP keeps a client on the same Pod (coarse, based on IP). Name your ports (name: http) and refer to the names from the Service's targetPort, so the Pod can change numbers without changing the Service. A Service can expose several ports; each needs a name.
Common mistakes
- Selector typos → no endpoints.
port/targetPortconfusion.- Expecting per-request load balancing from long-lived connections.
- Using Pod IPs in configuration instead of Service names.
- Readiness probes missing, so traffic reaches Pods that are not ready.
- Exposing everything with LoadBalancer Services (one cloud load balancer, and cost, per Service) instead of an Ingress (later).
Try it yourself
- Create the Deployment and Service above. Call the Service from a temporary Pod (
kubectl run tmp --rm -it --image=busybox -- wget -qO- http://web). - Make each Pod return its name and measure the distribution over 30 calls.
- Break the selector and diagnose the empty endpoints with the checklist, then fix it.
- Create a headless Service for the same Pods and compare what
nslookupreturns.
Check yourself
What problem does a Service solve?
Pod IPs change as Pods are replaced. A Service gives a stable virtual IP and DNS name and forwards to the current set of matching, ready Pods.
What is the difference between port and targetPort?
port is where the Service listens; targetPort is the port on the backend Pods to which traffic is forwarded.
A Service has no endpoints. What are the two most likely causes?
The selector matches no Pods (label mismatch), or the matching Pods are not Ready.
How does Kubernetes load balance across Pods of a Service?
kube-proxy rules choose a backend per new connection (roughly random). Requests on one long-lived connection all go to the same Pod.
Interview questions
How do Pods in one Deployment talk to another Deployment?
Through the second Deployment's Service, using its DNS name (http://orders in the same namespace, orders.shop.svc.cluster.local across namespaces).
What is a headless Service for?
DNS-based discovery of individual Pod IPs without a virtual IP: used with StatefulSets for stable per-Pod names and by clients that implement their own load balancing.
Why might traffic still reach an unhealthy Pod?
Without a readiness probe, a Pod is considered Ready as soon as its container starts, so it receives traffic before it can serve it. Define readiness probes.
Summary
- A Service is a stable ClusterIP and DNS name in front of the Pods selected by labels.
- Endpoints list the ready Pod IPs; kube-proxy rules forward connections to them (random per connection).
- Debug: endpoints → selector vs labels → readiness →
targetPort→ NetworkPolicy → bind address. - Headless and ExternalName Services cover discovery and aliases.
Going deeper
- Service: types, ports, selectors and traffic policies.
- EndpointSlices: how backends are tracked.
- Debug Services: the official troubleshooting walkthrough.
See how much of this you can explain
A free mock interview asks you new questions on Kubernetes for DevOps Engineer and scores every answer, with what to fix.