Sub topic 3
Why Services exist
This chapter assumes you can write a Deployment, apply YAML with kubectl, and read basic TCP/IP terms such as IP address, port and DNS name. It explains the problem Services solve and shows the smallest working example.
The problem: Pod IPs do not last
Every Pod gets its own IP address, and any Pod can reach any other Pod on that address. That is the Kubernetes network model. It does not make a Pod IP a good address to depend on.
- A Pod that is replaced gets a new IP. Deployments replace Pods during rollouts, node failures and scale changes.
- A Deployment runs several replicas, so a client would need to know every IP and pick one.
- Clients cannot tell which Pods are ready to serve traffic.
A client that hard-codes 10.244.1.17 breaks the first time that Pod is rescheduled.
What a Service gives you
A Service is an API object that gives a changing set of Pods one stable name and one stable virtual IP. Clients talk to the Service. Kubernetes keeps track of which Pods currently sit behind it.
A Service has two main parts:
- A selector, which is a set of labels. Pods whose labels match become the Service's backends.
- One or more ports, which say what port clients connect to and where the traffic goes on the Pod.
The Service does not own the Pods. It finds them by label, so the Deployment can add and remove Pods freely.
A worked example
First, a Deployment whose Pods carry the label app: web:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: httpThen a Service that selects those Pods:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- name: http
port: 80
targetPort: httpThe selector matches the Pod label app: web. Clients connect to port: 80 on the Service. Traffic goes to the container port named http, which is 80 in this Deployment.
Apply both files and check the result:
kubectl get service web
kubectl get endpointslices -l kubernetes.io/service-name=webThe first command shows the Service and its stable CLUSTER-IP. The second lists the Pod addresses currently behind it. Delete a Pod and the Service address stays the same, while the list of Pod addresses updates.
Common mistakes
- A selector that does not match the Pod labels. The Service is created, but it has no backends.
- Using the Service selector as if it were the Deployment selector. They are separate fields and must each match the Pod template labels.
- Treating the Service as a Pod owner. Deleting a Service does not delete Pods.
Exercise
A Deployment's Pods are labeled app: web. Your Service has the selector app: webapp. The Service exists and has a ClusterIP. What happens when a client connects to it, and how do you confirm the cause?
Show answer
The connection fails, because no Pods match app: webapp, so the Service has no endpoints. Confirm it by running kubectl get endpointslices -l kubernetes.io/service-name=<service name> and seeing no addresses, then compare kubectl get pods --show-labels with the Service selector. Fix the selector to app: web.