Sub topic 15
Services: Stable Access to Changing Pods
This chapter assumes you know what a Pod is, how labels and selectors work, and how a Deployment keeps a set of replicas running. It also assumes basic familiarity with IP addresses and ports.
The problem: Pod IPs do not last
Every Pod gets its own IP address. That address lasts only as long as the Pod does. When a Deployment replaces a Pod during a rollout or after a node failure, the new Pod gets a different IP. A client that hard-codes a Pod IP breaks as soon as the Pod is replaced.
A Service solves this. It is an API object that gives a group of Pods one stable virtual IP and port, plus a DNS name inside the cluster.
Defining a Service
A Service picks its backends with a label selector. This one targets the Pods created by the web Deployment, which carry the label app: web.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80selectorchooses the Pods that receive traffic.portis the port clients use when they call the Service.targetPortis the port the container listens on. It can differ fromport.
If you do not set type, the Service is of type ClusterIP. It is reachable only from inside the cluster.
How traffic reaches a Pod
The Service itself does not run as a process. Its virtual IP is a rule, not a network interface on a machine. Three components cooperate to make it work.
- The control plane watches Pods that match the selector and records the ready ones as endpoints of the Service.
- On every node,
kube-proxywatches Services and their endpoints. It programs the node's packet-forwarding rules (iptables or IPVS, depending on configuration). - When a Pod sends traffic to the Service IP, those rules rewrite the destination to the IP of one of the endpoints.
Only Pods that pass their readiness checks are used as endpoints. A Pod that is starting up or failing its checks is removed from rotation, and it returns once it is ready again.
Finding a Service by name
Cluster DNS gives each Service a name. From a Pod in the same namespace, a client can call http://web. From another namespace, it uses web.<namespace>, or the full form web.<namespace>.svc.cluster.local. Clients should use this name and never the virtual IP.
Service types
| Type | Reachable from | Typical use |
|---|---|---|
| ClusterIP | Inside the cluster | Internal communication between workloads |
| NodePort | Outside, on a port of every node | Simple external access, testing |
| LoadBalancer | Outside, through a cloud load balancer | Production external access on supported platforms |
Try it
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl get service web
kubectl get endpoints webThe endpoints list shows the IPs of the ready Pods. Delete one Pod with kubectl delete pod <pod-name> and run kubectl get endpoints web again. The Deployment starts a replacement, and the endpoint list changes while the Service IP stays the same.
Common errors
- Empty endpoints. The selector does not match the Pod labels, or no Pod is ready. Compare
spec.selectorin the Service with the labels in the Pod template. - Connection refused.
targetPortdoes not match the port the container actually listens on. - Name not resolving. The client is in a different namespace and used the short name.
Exercise
A Service named api has selector: {app: api}, but kubectl get endpoints api shows no addresses. The Pods are Running. Name two likely causes.
Show answer
The Pods' labels do not match the selector (for example, they are labeled app: backend), or the Pods are Running but not Ready because their readiness checks are failing. Either case leaves the endpoint list empty.