Expose an app with a Service and fix one with no endpoints
In this task you'll give pods a stable address, and fix a Service that sends traffic nowhere.
Pods come and go with new IP addresses. A Service gives a set of pods one stable name and IP. It finds its pods with a label selector, and the list of pods it found is its EndpointSlice. ClusterIP (the default) works inside the cluster, NodePort opens a port on every node, and LoadBalancer asks the cloud for an external address.
The most common Service fault is a selector that matches nothing, so the Service has no endpoints and connections fail. Next is a targetPort that doesn't match the port the container listens on. Both are quick to see: compare kubectl get endpointslices with the pod labels and container ports.
Example. Kestwick Hotels renamed the label on its rates pods from app: rates to app: rates-api in a release, but not the Service. The Service had no endpoints, and the booking site showed errors until the selector was updated.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 30 minutes.
Where to look
- Terminal
- kubectl
Steps
- Create a deployment
webfrom the nginx image with 2 replicas, and expose it:kubectl expose deployment web --port 80. - Check the Service has two endpoints:
kubectl get endpointslices -l kubernetes.io/service-name=web. - Test it from inside the cluster:
kubectl run t --rm -it --image=busybox:1.36 -- wget -qO- http://web. - Break it:
kubectl patch service web -p '{"spec":{"selector":{"app":"wrong"}}}', then check the endpoints again and repeat the test.The list is empty and the request times out. - Fix the selector back to
app: web, then try a wrongtargetPort(8080) and see the same symptom with endpoints present. - Change the Service type to NodePort and reach it with
kubectl port-forward service/web 8080:80from your computer.
Commands
kubectl create deployment web --image=nginx:1.27 --replicas=2
kubectl expose deployment web --port 80
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl run t --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 3 http://web
# Compare what the Service selects with the pods' labels
kubectl get service web -o jsonpath='{.spec.selector}'; kubectl get pods --show-labels
Watch out for
- Cost
- Free on kind. In a cloud, every LoadBalancer Service creates a paid public IP and load balancer rule.
- Security
- Expose only what must be reached from outside; prefer one shared entry point (an ingress or gateway) over many LoadBalancer Services.
- Common mistakes
- Matching on a label the pods don't have, mixing up port and targetPort, and testing from a pod in another namespace with the short name.
Clean up
kubectl delete deployment web and kubectl delete service web.
Words to know
- Service
- A stable name and virtual IP in front of a changing set of pods.
- Selector
- The labels a Service uses to find its pods.
- EndpointSlice
- The list of pod addresses a Service currently sends traffic to.
- targetPort
- The port on the pod that the Service forwards to.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands OnKeep going
- Kubernetes roadmap: the skills behind the CKA and CKADExplained
- Kubernetes networking: pods, Services, DNS, Ingress and Gateway APIExplained
- Place pods with affinity, taints and tolerationsPrevious in Kubernetes skills
- Route web traffic with the Gateway APINext in Kubernetes skills
- Containers and KubernetesAll pages in this topic