Services and EndpointSlices
Give a changing set of Pods a stable Service address and inspect the backends Kubernetes can route to.
What you will understand
- Follow a Service selector from matching labels to EndpointSlice entries.
- Trace a connection from a ClusterIP and Service port to the application port.
- Verify Service connectivity from a client Pod inside the cluster.
Builds on: Probes and graceful recovery
How a Service finds replaceable backends
A catalog frontend needs to call a catalog API. The API runs in a Pod today, and its replacement could have a different IP tomorrow. A Service gives clients a stable name and a way to reach the current backends. A Service of type ClusterIP gets a virtual IP for use inside the cluster. A client connects to that IP and the Service port. The network implementation forwards the connection to an eligible backend.
For a Service with a selector, Kubernetes looks for Pods in the same namespace whose labels match. The EndpointSlice controller records matching backend addresses, ports, and conditions in objects called EndpointSlices. You can think of the Service as the address clients use and the slices as the list the network implementation uses to learn where traffic can go. Toggle a backend’s readiness in the first scene and compare the selected Pods with the eligible endpoints.
Which Pods can be Service backends?
The Service selector finds label matches. EndpointSlices publish their addresses and readiness, and ordinary forwarding uses eligible endpoints.
Match app=service-demo
- Service app=service-demoPod A: matching, readyselector match
- Service app=service-demoPod B: matching, unreadyselector match
- Service app=service-demoPod C: other labelnot selected
The Service selector app=service-demo matches A and B. A healthy Pod with another label is outside this backend set.
Record readiness per address
- EndpointSlice controllerA ready=true; B ready=falsemaintain backend records
- Service identityA ready=true; B ready=falseassociated Service
The EndpointSlice controller maintains addresses, target ports, and readiness conditions. An unready address can remain represented with ready false.
Update addresses as Pods change
- Replacement Pod DUpdated ready backend IPsnew address/condition
- Same Service ClusterIPUpdated ready backend IPscurrent backend set
When a replacement Pod gets a new IP and becomes ready, the endpoint records change. The Service frontend remains stable.
A normal selector-based Service is shown with publishNotReadyAddresses omitted.
Try the mechanism with your own inputs
- Service to web-aeligible
- Service to web-beligible
The selector app=web and readiness together produce web-a, web-b. A container can remain running while unready. This models a Service with publishNotReadyAddresses=false and no terminating endpoints; readiness alone does not describe every Service routing policy.
spec:
selector:
app: web
# publishNotReadyAddresses defaults to false.
# Selected non-terminating ready endpoints: web-a, web-bExplore another case
Connect a Service port to the app’s listener
Save the example as service-demo.yaml and apply it in a disposable namespace. The Deployment starts nginx on port 80 and gives the Pod the label app: service-demo. The Service selects that label and accepts TCP connections on port 8080. Its targetPort: http refers to the port named http in each selected Pod. That name resolves to 80 in this example. Setting containerPort documents and names a port. The process in the container still has to listen there.
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-demo
spec:
replicas: 1
selector:
matchLabels:
app: service-demo
template:
metadata:
labels:
app: service-demo
spec:
containers:
- name: web
image: nginx:1.27.5
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
---
apiVersion: v1
kind: Service
metadata:
name: service-demo
spec:
type: ClusterIP
selector:
app: service-demo
ports:
- name: http
protocol: TCP
port: 8080 # Client-facing Service port.
targetPort: http # Resolves to the Pod's named port 80.kubectl apply -f service-demo.yaml
kubectl get service service-demo
kubectl get pods -l app=service-demo --show-labels
kubectl get endpointslices -l kubernetes.io/service-name=service-demo -o yamlCheck the Service address and compare the Pod IP with the addresses in its slices. Look for conditions.ready beside the backend. Send a request in the forwarding scene. Follow it from the client to the ready Pod and watch the response return. On a cluster using kube-proxy, it updates rules used to handle Service traffic. Some network implementations handle that job differently.
Open Inspect the request to follow one iptables example. Compare the destination before and after forwarding, then check what source address the client sees on the reply.
Where does a request to a Service go?
Send an HTTP request to port 8080 and follow the connection to nginx on port 80. The EndpointSlice below shows the available addresses.
10.244.1.7:80Ready · eligible10.244.2.9:80Not ready · skipped
The client already knows the Service’s virtual IP. The node has forwarding rules for its ready endpoint.
Text walkthrough
- The Service has a ready backend
The client already knows the Service’s virtual IP. The node has forwarding rules for its ready endpoint.
- Client connects to the Service address
The client targets 10.96.0.42:8080. The node’s forwarding rules select an eligible backend.
- The connection is forwarded to port 80
The node directs the connection to 10.244.1.7:80. The client sends GET / over that connection.
- nginx receives GET /
The ready Pod handles the request on its own IP and port.
- The response is on its way back
nginx sends HTTP 200. The reply follows the established connection back to the client.
- The client receives HTTP 200
One request reached a ready backend through the Service’s stable address.
IPv4 ClusterIP with one ready and one unready endpoint. DNS resolution, TCP handshakes and the details of address translation are omitted. The EndpointSlice supplies endpoint data to the networking implementation.
Inspect the request
A ClusterIP connection in a cluster using kube-proxy in iptables mode. Start with how the node gets its forwarding rules, then follow one HTTP request.
From EndpointSlice controllerTo API server
Publish eligible endpoints
The controller records the addresses and readiness of the Service’s matching Pods. An unready Pod can still appear in an EndpointSlice.
- Before
- No address published yet
- After
- 10.244.1.7:80 is ready
The controller records the addresses and readiness of the Service’s matching Pods. An unready Pod can still appear in an EndpointSlice.
Illustrative addresses and playback timing. The client already knows the Service IP. TCP handshake and other packet fields are omitted.
Reveal prediction answer
DNS can still return the Service IP. With no matching Pods, forwarding has no eligible backend, so the HTTP request fails.
Compare the Service selector, EndpointSlice readiness, and HTTP from the original client before and after the repair.
Verify this in the Service labExplore another case
Test from a Pod inside the cluster
Run the sample client in the same namespace. The short name service-demo resolves there, and the request goes to port 8080. The client exits after fetching the page, so the wait looks for Succeeded. Read its logs to see the nginx response, then remove the client Pod.
kubectl run service-client --image=busybox:1.36.1 --restart=Never \
--command -- sh -c 'wget -qO- http://service-demo:8080/'
# This finite client can finish before a Ready condition is observed.
kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/service-client --timeout=90s
kubectl logs service-client
kubectl delete pod service-clientRepeat after checking the selector and endpoint addresses in the Service networking lab. If you change a label to break the match, observe which fields change before restoring it.
Read endpoint conditions
An EndpointSlice entry can still show a Pod address while ready is false. Inspect conditions as well as the address. Options such as publishing unready addresses and routing behavior during termination affect special cases. When debugging a typical Service, first confirm matching Pods, ready backends, and agreement between the target port and the application listener.
Check your understanding
The Service exists and DNS returns an IP, but its selector says app: catalog while the Pods say app: catalogue. Where will the connection go?
The labels do not match, so this Service has no eligible backends from those Pods. Its name can still resolve to the ClusterIP. Compare the selector with the Pod labels and correct the intended match before debugging the application itself.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise