Sub topic 8
Services: stable access to Pods
This chapter assumes you can read a Deployment manifest, know how labels and selectors match objects, and have kubectl pointed at a cluster. Pods are replaced often, and each new Pod gets a new IP address. A Service gives clients one stable name and virtual IP, and it forwards traffic to whichever Pods currently match.
The working model
A Service is a Kubernetes object with a selector and a list of ports. The control plane watches for Pods whose labels match the selector and that report ready. It records their addresses in EndpointSlice objects. On each node, kube-proxy (or the cluster's network plugin) uses those records to route traffic sent to the Service's virtual IP to one of the ready Pods.
Cluster DNS also gives the Service a name. From the demo namespace, web resolves to the Service. From other namespaces, use web.demo or web.demo.svc.cluster.local.
A ClusterIP Service
This manifest routes port 80 on the Service to port 80 in any Pod in demo labeled app: web.
apiVersion: v1
kind: Service
metadata:
name: web
namespace: demo
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80selectormust match the Pod template labels in the Deployment, not the Deployment's own labels.portis the port clients use on the Service.targetPortis the port on the Pod. It can be a number or the name of a container port.typedefaults toClusterIP, which is reachable only from inside the cluster.
Service types
| Type | Reachable from | Typical use |
|---|---|---|
| `ClusterIP` | Inside the cluster | Internal communication between workloads |
| `NodePort` | Each node's IP on a high port | Simple external access, testing |
| `LoadBalancer` | A cloud load balancer's address | Public or external-facing services |
Each type builds on the previous one. A LoadBalancer Service also has a cluster IP and node ports.
Check the connection
Apply the Deployment and the Service, then inspect the result.
kubectl apply -f deployment.yaml -f service.yaml
kubectl get service web -n demo
kubectl get endpointslices -n demo -l kubernetes.io/service-name=web
kubectl port-forward service/web 8080:80 -n demoIn a second terminal, run curl http://localhost:8080. You should see the nginx welcome page.
Common errors
- Selector matches nothing. The Service exists but has no endpoints, and connections fail. A label typo is the usual cause. Compare
kubectl get pods -n demo --show-labelswith the selector. - Pods are not ready. Pods that fail their readiness probe are removed from the endpoints. The Service stays up but sends no traffic to them.
- Wrong
targetPort. The Service looks healthy, but connections are refused or time out because nothing listens on that port in the container. - Wrong namespace. A Service selects Pods only in its own namespace. A short name like
webresolves only for clients in the same namespace.
Exercise
A Service named web in demo shows no endpoints. The Deployment's Pods are Running and carry the label app: web-server, and the Service selector is app: web. What is wrong, and what are two ways to fix it?
Show answer
The selector does not match the Pod labels. Either change the Service selector to app: web-server, or change the Pod template labels (and the Deployment selector, which is immutable, so this needs a recreated Deployment) to app: web. Changing the Service selector is simpler. Then re-apply and confirm with kubectl get endpointslices -n demo.