Kubernetes Field Guide · Chapter 5 of 16
Services and DNS: stable addresses for moving pods
Pods get a new IP every time they are replaced. A Service gives a set of pods one stable name and address, and spreads traffic across the ones that are ready. Learn the three types, cluster DNS, and what each type costs in the cloud.
- 35 min
- Intermediate
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
Why pods need Services
Every pod gets its own IP, but pods are disposable: a rollout, a crash or a drained node replaces them with new ones on new IPs. Nothing should ever hold on to a pod IP.
A Service is a stable virtual IP and DNS name in front of a changing set of pods. Clients talk to web; Kubernetes keeps track of which pods are behind it right now.
Selectors and endpoints
A Service does not know about Deployments. It has a label selector, and a controller keeps an EndpointSlice listing the IPs of every pod that matches the selector and is ready. kube-proxy on each node turns that list into routing rules.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- name: http
port: 80
targetPort: 80- Every ready pod with app=web receives traffic, whoever created it.
- The port clients use on the Service.
- The port on the container. They often differ, for example port 80 to targetPort 8080.
Change labels and readiness, send traffic and switch the type:
Interactive
Service web · selector app=web
endpoints:
Path
Cost:
Relabelling a pod is a real debugging trick: remove a misbehaving pod from the Service by changing its label, and its ReplicaSet starts a replacement while you investigate the original.
ClusterIP, NodePort and LoadBalancer
| Type | Reachable from | Use it for |
|---|---|---|
| ClusterIP | Inside the cluster only | Almost every Service: APIs, databases, internal tools |
| NodePort | Every node's IP on a port from 30000–32767 | Labs, and bare metal with your own load balancer in front |
| LoadBalancer | A cloud load balancer's public or internal address | The one entry point of a cluster, usually your Ingress controller |
Headless (clusterIP: None) | DNS returns the pod IPs directly | StatefulSets and clients that pick their own pod, such as database drivers |
Each type builds on the previous one: a LoadBalancer Service is also a NodePort and a ClusterIP Service.
Cluster DNS
CoreDNS gives every Service a name. From a pod in the same namespace, web works. From another namespace use web.default, and the full name is web.default.svc.cluster.local.
Follow one request from a pod to the Service named web:
Step through it
A request to http://web, hop by hop
- Client podwget http://web
- CoreDNSkube-system
- kube-proxy ruleson every node
- A web podone of the ready ones
Step 0 of 4
Press Play, or step through with Next.
Debug a Service, hop by hop
"I cannot reach my service" is one of the most common Kubernetes problems, and it always fails at one specific hop. Work along the path in order (DNS, Service, endpoints, pod) and the first broken hop is your bug. Switch on a bug and send a request.
Interactive · Trace a request
Switch on a bug, send a request, see where it dies.
- 1
frontend pod
curl http://web/ from namespace default
- 2
CoreDNS
web → web.default.svc.cluster.local → 10.96.0.12
- 3
Service web
ClusterIP 10.96.0.12, port 80 → targetPort 8080, selector app=web
- 4
EndpointSlice
Ready endpoints: 10.244.1.7:8080, 10.244.2.4:8080
- 5
kube-proxy rules
On the client node: 10.96.0.12:80 → one ready endpoint, picked at random
- 6
web pod
nginx listening on 8080 answers
$ curl http://web/ # press Send requestThree of the four bugs give the same error, Connection refused, which is why guessing does not work. Check the endpoints first: if the list is empty, the problem is labels or readiness; if it is full, look at ports.
In production: cost and traffic policy
- One load balancer, not one per Service. Every
type: LoadBalancerService creates a separate cloud load balancer, billed by the hour and by traffic. Put one Ingress controller or Gateway behind a single load balancer and keep everything else ClusterIP. - Cross-zone traffic is billed. On AWS and most clouds, traffic between availability zones costs money. By default a Service spreads requests across all zones. Setting
trafficDistribution: PreferClosekeeps traffic in the caller's zone when there are healthy pods there. - Client IPs. With the default
externalTrafficPolicy: Cluster, traffic can hop to another node and the app sees a node IP.Localkeeps the real client IP and skips the hop, but only nodes running a pod receive traffic. - Internal load balancers. Clouds support annotations that make a LoadBalancer private to your network, which is what you want for anything that is not public.
Hands-on: expose and debug a Service
The lab is docked at the bottom of your screen.
Live lab · no install
Expose and debug a Service
Hover a Service in the map to see which pods it sends traffic to.
- Tasks0/7
Expose the web Deployment
The Service gets one stable IP and DNS name in front of four moving pods.
kubectl expose deployment web --port=80 --target-port=80See the pods behind it
Hover the Service in the map to highlight its endpoints.
kubectl get endpointslices -l kubernetes.io/service-name=webCall it by name from inside the cluster
kubectl run tmp --rm -it --image=busybox:1.37 --restart=Never -- sh -c "nslookup web; wget -qO- http://web | head -n 1"Break the selector
The most common Service bug: a selector that matches nothing.
kubectl patch svc web -p '{"spec":{"selector":{"app":"wbe"}}}' kubectl get endpointslices -l kubernetes.io/service-name=webNo endpoints: every request to web now fails. The map shows the Service with 0 endpoints.
Compare labels with the selector, then fix it
kubectl get pods --show-labels kubectl patch svc web -p '{"spec":{"selector":{"app":"web"}}}'Take one pod out of the Service by relabelling it
The ReplicaSet notices it lost a pod and makes a new one. The relabelled pod keeps running, out of rotation: a classic way to debug a live pod.
kubectl label $(kubectl get pods -l app=web -o name | head -n 1) app=debug --overwriteReach web from another namespace
Short names only work in the same namespace. Add the namespace to the name.
kubectl create namespace shop kubectl run tmp -n shop --rm -it --image=busybox:1.37 --restart=Never -- wget -qO- http://web.default- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
Use the web Deployment from the last chapter.
kubectl expose deployment web --port=80 --target-port=80
kubectl get svc web
kubectl get endpointslices -l kubernetes.io/service-name=web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web ClusterIP 10.96.142.17 <none> 80/TCP 3s
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-8qfzl IPv4 80 10.244.0.21,10.244.0.22,10.244.0.23 + 1 more 3sexpose- Creates a Service using the Deployment selector. Same result as applying the YAML above.
endpointslices- The ready pod IPs behind the Service.
Call it by name from a throwaway pod:
kubectl run tmp --rm -it --image=busybox:1.37 --restart=Never -- sh -c "nslookup web; wget -qO- http://web | head -n 4"
--rm -it- Interactive, and deleted when it exits.
nslookup web- Shows the Service name resolving to its ClusterIP.
Now break it the most common way, with a selector that matches nothing:
kubectl patch svc web -p '{"spec":{"selector":{"app":"wbe"}}}'
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl get pods --show-labels | head -n 3
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-8qfzl IPv4 <unset> <unset> 2m<unset>- No endpoints: every request to web now fails.
--show-labels- Compare the real labels with the selector.
Mini mission
Fix it, then reach it from another namespace
Fix the selector. Then create a namespace shop and fetch the nginx page from a pod running there.
Reveal one safe solution
kubectl patch svc web -p '{"spec":{"selector":{"app":"web"}}}'
kubectl create namespace shop
kubectl run tmp -n shop --rm -it --image=busybox:1.37 --restart=Never -- wget -qO- http://web.default
Inside shop, the short name web would look for a Service in shop and fail. web.default names the namespace.
Check yourself
Quiz
0 of 4 answered