Skip to Content

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 , founding engineer running Kubernetes in production. Updated .

frontendhttp://webService: web10.96.0.12, fixedselectorapp=webweb-9qmztweb-4xk2p ✕web-zc8hn newweb-tw7rcdropped
The chapter in one picturePods come and go. The Service name and IP stay the same.

By the end you can

0 of 5

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.

web-svc.yaml
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:

Toggle a pod's label or readiness and watch the endpoint list change.

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

TypeReachable fromUse it for
ClusterIPInside the cluster onlyAlmost every Service: APIs, databases, internal tools
NodePortEvery node's IP on a port from 30000–32767Labs, and bare metal with your own load balancer in front
LoadBalancerA cloud load balancer's public or internal addressThe one entry point of a cluster, usually your Ingress controller
Headless (clusterIP: None)DNS returns the pod IPs directlyStatefulSets 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

  1. Client podwget http://web
  2. CoreDNSkube-system
  3. kube-proxy ruleson every node
  4. 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. 1

    frontend pod

    curl http://web/ from namespace default

  2. 2

    CoreDNS

    web → web.default.svc.cluster.local → 10.96.0.12

  3. 3

    Service web

    ClusterIP 10.96.0.12, port 80 → targetPort 8080, selector app=web

  4. 4

    EndpointSlice

    Ready endpoints: 10.244.1.7:8080, 10.244.2.4:8080

  5. 5

    kube-proxy rules

    On the client node: 10.96.0.12:80 → one ready endpoint, picked at random

  6. 6

    web pod

    nginx listening on 8080 answers

$ curl http://web/   # press Send request

Three 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: LoadBalancer Service 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: PreferClose keeps 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. Local keeps 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

Live lab · no install

Expose and debug a Service

Hover a Service in the map to see which pods it sends traffic to.

kind-learn
  1. Tasks0/7
  2. 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=80
  3. See the pods behind it

    Hover the Service in the map to highlight its endpoints.

    kubectl get endpointslices -l kubernetes.io/service-name=web
  4. Call 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"
  5. 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=web

    No endpoints: every request to web now fails. The map shows the Service with 0 endpoints.

  6. Compare labels with the selector, then fix it
    kubectl get pods --show-labels
    kubectl patch svc web -p '{"spec":{"selector":{"app":"web"}}}'
  7. 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 --overwrite
  8. Reach 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
  9. Lab complete. Keep experimenting, or press Reset to start again.
↑↓ history · Tab complete · Ctrl+C stop
Cluster map live
  • 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.

Terminal
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   3s
expose
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:

Terminal
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:

Terminal
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
Terminal
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

  1. 1. How does a Service decide which pods get traffic?
  2. 2. A Service exists, but requests to it time out. kubectl get endpointslices shows no endpoints. Most likely cause?
  3. 3. From a pod in namespace shop, how do you reach the Service api in namespace payments?
  4. 4. You have 12 public HTTP services on EKS. What is the cheaper design?

Further reading