Lesson 4 of 8 in Kubernetes
Services: stable addresses and DNS names for Pods
A Service gives a changing set of Pods one stable virtual IP and DNS name. It selects Pods by label and spreads traffic across the ones that are ready.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Deployments: replicas, rollouts and rollbacks, Networking for DevOps
The problem this solves
Pods come and go, and each new Pod gets a new IP address. A client that remembers a Pod's IP will break the first time that Pod is replaced.
A Service solves this with a name and an address that stay the same. Kubernetes watches which Pods match the Service's labels and are ready, and keeps the list up to date. Clients inside the cluster use the Service name, and cluster DNS turns it into the Service address.
How it works
- Network
- Runs code
A client looks up the Service name in cluster DNS, calls the Service address, and the Service forwards each request to one of the matching Pods.
- Client Pod to Cluster DNS: 1 look up web
- Client Pod to Service: 2 call the address
- Service to Pod A: 3 forward
- Service to Pod B: 3 or forward
- Network
- Control
- Runs code
Kubernetes lists the Pods whose labels match the Service selector in an EndpointSlice, and skips Pods whose labels do not match.
- Service to EndpointSlice: 1 selector is matched
- EndpointSlice to Pod app=web: 2 label matches
- EndpointSlice to Pod app=wbe: 3 no match
A Service of the default type ClusterIP is reachable only inside the cluster. Its address is virtual: the network rules on each node redirect it to a real Pod IP. A Service named web in namespace default gets the DNS name web.default.svc.cluster.local. Inside the same namespace, plain web works.
The link between a Service and its Pods is only a label selector. Nothing else connects them. If the selector matches no ready Pods, the Service still exists, but it forwards to nothing.
Other types build on ClusterIP. NodePort also opens a port on every node. LoadBalancer asks the cloud provider for an external load balancer, which does not exist on a plain local cluster. For local access, kubectl port-forward is the simplest.
Try it yourself
Run these in a throwaway folder. You will create a Deployment, put a Service in front of it, call it by name, then break the selector on purpose.
- 1Create Pods and a Service
cat > svc.yaml <<'EOF' apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 2 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.27 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: web spec: selector: app: web ports: - port: 80 targetPort: 80 EOF kubectl apply -f svc.yaml kubectl rollout status deployment/web - 2See which Pods the Service found
kubectl get endpointslices -l kubernetes.io/service-name=web
The ENDPOINTS column lists two Pod IPs. Compare them with
kubectl get pods -l app=web -o wide. - 3Call the Service by name from inside the cluster
kubectl run tmp --rm -it --restart=Never --image=busybox:1.36 -- wget -qO- http://web
This starts a throwaway Pod, fetches the nginx welcome page through the Service name, and deletes the Pod.
- 4Reach it from your laptop
kubectl port-forward service/web 8080:80
In a second terminal run
curl localhost:8080, then press Ctrl+C. - 5Break the selector
kubectl patch service web -p '{"spec":{"selector":{"app":"wbe"}}}' kubectl get endpointslices -l kubernetes.io/service-name=webThe ENDPOINTS column is now empty. Repeat the
wgetstep and it will fail, although every Pod is Running. - 6Fix it and clean up
kubectl patch service web -p '{"spec":{"selector":{"app":"web"}}}' kubectl delete -f svc.yaml rm svc.yaml
What breaks, and where to look
The Pods are Running, but requests to the Service hang or are refused, and the EndpointSlice has no addresses.
- WHY IT HAPPENS
- The Service selector does not match the Pod labels, often because of a typo or because only the Deployment's label was changed. Or the Pods are running but not Ready.
- CHECK FIRST
- Run
kubectl get endpointslices -l kubernetes.io/service-name=<service-name>, thenkubectl get svc <service-name> -o yamlfor the selector andkubectl get pods --show-labelsto compare.
The Service has endpoints, but connections are refused or time out.
- WHY IT HAPPENS
- The Service
targetPortis not the port the app actually listens on inside the container.portis what clients use,targetPortis where the container listens. - CHECK FIRST
- Run
kubectl describe svc <service-name>to see the target port, thenkubectl logs <pod-name>to see which port the app opened.
wget: bad address 'db'from a Pod, although the Service exists.- WHY IT HAPPENS
- The Service is in a different namespace. A bare name resolves only within the caller's own namespace.
- CHECK FIRST
- Run
kubectl get svc -Ato find its namespace, then use<service>.<namespace>as the host name.
When not to use this
- A ClusterIP Service does not give you a public address or HTTP routing by host and path. For that you need a load balancer or the routing objects in the next concept.
- Services balance connections, not requests. A long-lived connection such as gRPC stays on one Pod, so you may need client-side balancing or a service mesh.
Check yourself
How does a Service know which Pods to send traffic to?
It matches its selector against Pod labels, and only Pods that are ready are listed in its EndpointSlice.
What is the DNS name of Service api in namespace shop?
api.shop.svc.cluster.local. From a Pod in the same namespace, api is enough.
The Pods are Running but the Service has no endpoints. What do you check first?
Compare the Service selector with the Pod labels, and confirm the Pods are Ready, not only Running.
Keep going
Lesson 73 of 92 in the DevOps path