Kubernetes · Chapter 7 of 28
Services
By the end of this chapter you can give a set of pods one stable name and address with a Service, choose its port and targetPort, read which pods it sends to from its EndpointSlices, call it by DNS name from inside the cluster, and find out why a Service answers nothing. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running apps
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
The storefront calls the catalog for every page: 2 storefront pods, 2 catalog pods, about 300 requests a second on an ordinary evening and four times that on Dhanteras. Today the storefront's page answers 502, because it has nowhere to send those calls:
$ kubectl exec storefront-84744fd4c9-gfps8 -- curl -s localhost:8080/
{"error":"catalog is unreachable","detail":"Could not resolve host: catalog"}
What you know so far gives each pod an IP, and -o wide shows it. But which one should the storefront call? There are two catalog pods, soon more, and their IPs change every time a pod is replaced: chapter 5's Deployment made new pods with new IPs on every rollout, scale and crash. Writing pod IPs into the storefront's config would break on the next rollout and send every request to one pod. Tushar needs one name that always means "a ready catalog pod", and Ammar needs to see which pods are behind it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
catalog-svc.yaml: a Service for the catalog
storefront calls http://catalog on port 80, but there is no Service named catalog yet. Make it.
1. Open catalog-svc.yaml. Write a selector that picks the catalog pods: app=catalog.
2. Add one port: port 80, with targetPort 8080 (or the port's name, http).
3. Run kubectl apply -f catalog-svc.yaml.
4. Send a request from a storefront pod to its own page, http://localhost:8080/, with lab curl --from.
To pass: a Service named catalog in ch07 selects only catalog pods and listens on port 80. The Check sends its own requests from a storefront pod: http://catalog/products and the storefront page must answer 200, and the page must list the products.
- Task 2
Fix the rider-tracker Service
The Service rider-tracker sends traffic to a port its pods do not listen on. Fix it.
1. Send a request from a storefront pod: lab curl --from ch07/<pod> http://rider-tracker/riders. Read the error.
2. Compare the Service's targetPort with the port the rider-tracker pods listen on.
3. Fix the targetPort with kubectl edit, kubectl patch, or a fixed copy that you apply.
To pass: a request from a storefront pod to http://rider-tracker/riders answers 200. The Service keeps its name, port 80 and its selector app=rider-tracker. Do not delete it.
- Task 3
endpoints.txt: which pods notifier sends to
Three notifier pods run, but the Service notifier sends traffic to only some of them. Find which.
1. Run kubectl get endpointslices, or kubectl describe svc notifier.
2. The slice lists IPs. Match each IP to a pod name, with kubectl get pods -o wide.
3. In endpoints.txt, below the notes, write the pod names, one on each line.
To pass: you ran kubectl get endpointslices or kubectl describe on the Service, and the command worked. endpoints.txt has exactly the names of the ready pods that notifier sends to now: no IPs, no other pods, and none left out.
- Task 4 · interview question
Interview: when a pod behind a Service dies
Ammar will ask: "A pod behind the catalog Service dies. What happens to the requests?"
1. Write two or three sentences in your own words in answer.txt, below the notes.
2. Say what happens to the Service's endpoints (the EndpointSlice) when the pod dies.
3. Say where the requests go, and what the ReplicaSet does.
To pass: answer.txt says what happens to the endpoints, and that requests go to the other ready pods.
What you will learn
- ClusterIP Services: a selector, port, targetPort and named ports.
- EndpointSlices: the ready and not ready addresses behind a Service.
- Cluster DNS: hello, hello.ch07, hello.ch07.svc.cluster.local, and /etc/resolv.conf.
- Calling from inside: kubectl exec ... curl and lab curl --from.
- kube-proxy, Service env vars, and a Service whose selector matches nothing.
More in Running apps
Build "Services" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.