Sub topic 2
What a Service is and how clients reach it
This chapter assumes you can use kubectl get, describe, logs and exec. It also assumes you know Pods, Deployments, namespaces and YAML manifests, and that you understand IP addresses, ports and DNS A records. By the end, you will be able to trace one request from a client Pod to a backend Pod.
Why Pods need a Service
A Pod gets its own IP address, but that address is temporary. When a Deployment replaces a Pod, the new Pod gets a new IP. Clients can't track a changing list of addresses.
A Service solves this. It gives a group of Pods one stable virtual IP, called the ClusterIP, and one stable DNS name. It picks its backends with a label selector. Any Pod in the same namespace whose labels match is a candidate backend.
A working example
This Deployment and Service pair works as written. The Service selector matches the Pod labels. The targetPort matches the port nginx listens on.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 8080
targetPort: 80The Service has two port fields. port is where clients connect on the ClusterIP. targetPort is where the traffic arrives on the Pod. Here, clients use 8080 and nginx receives the traffic on 80.
How a request travels
- The client asks cluster DNS for
web. DNS returns the ClusterIP. - The client opens a TCP connection to the ClusterIP on port 8080.
- Rules that kube-proxy installed on the client's node rewrite the destination to the IP of one ready backend Pod, on port 80.
- The Pod answers, and the reply returns along the same path.
No process listens on the ClusterIP, and no network interface holds it. It exists only as a match in those node rules. Later chapters cover DNS and kube-proxy in detail.
Where failures live
Each step of the request path can fail in a different way. The symptom usually points to the layer.
| Symptom from a client Pod | Likely layer |
|---|---|
| The name does not resolve | Cluster DNS or the client's `resolv.conf` |
| The name resolves, but connections time out | No ready endpoints, a NetworkPolicy, or a node-level problem |
| The name resolves, but connections are refused | `targetPort` does not match the port the container listens on |
First checks
Start with two commands. The first shows the ClusterIP and the selector. The second shows which Pod IPs the Service currently has.
kubectl get service web -o wide
kubectl get endpointslices -l kubernetes.io/service-name=webIf the endpoint list is empty, the Service has no backends. DNS and ports don't matter until that is fixed. The next chapters explain how to find out why.
Exercise
A client Pod runs wget -qO- http://web:8080 and gets "connection refused". nslookup web returns the ClusterIP. The Service has port: 8080 and targetPort: 80, and nginx listens on 80. Which layers does this evidence rule out, and which would you check next?
Show answer
Name resolution works, so DNS is ruled out. The ports match as configured (8080 to 80, with nginx on 80), so a plain port mismatch is unlikely. Next, check that the Service has endpoints with kubectl get endpointslices -l kubernetes.io/service-name=web. Then check that the selector matches running, ready Pods with kubectl get pods -l app=web -o wide. Confirm the container is actually listening on 80.