Services and EndpointSlices · CKA
Create a stable access point that keeps working as Pods are replaced.
Learn with progress and quizzesFirst, understand the concepts.
Selector
A Service targets Pods matching its selector. If Pod labels do not match, those Pods will not be selected as backends.
Port and targetPort
port is the port exposed by the Service; targetPort is the backend port. EndpointSlices let you inspect the actual backend addresses.
Why it matters
Clients use a stable Service address, but selected Pods provide responses. Read the frontend, backend set, and listening port separately.
Work through the reasoning
Identify the backend set
Compare every Service selector condition with Pod labels. Match EndpointSlice addresses and ready conditions against Pod IPs from get pods -o wide.
Distinguish the ports
With port 80 and targetPort 8080, clients contact Service port 80 and traffic reaches Pod port 8080. Declaring containerPort does not make the application listen there.
Learn by doing.
You need kubectl and access to your own practice Kubernetes cluster. Each lab is independent. Check your current context first, then run the commands in your own terminal.
Expose a web Deployment as a ClusterIP Service, then compare its selector with its EndpointSlices.
kubectl create namespace study-svc --dry-run=client -o yaml | kubectl apply -f -
kubectl create deployment web --image=nginx:stable -n study-svc --dry-run=client -o yaml | kubectl apply -f -
kubectl expose deployment web --port=80 --target-port=80 -n study-svc --dry-run=client -o yaml | kubectl apply -f -
kubectl get service web -n study-svc -o yaml
kubectl get pods -n study-svc --show-labels
kubectl get endpointslices -n study-svc -l kubernetes.io/service-name=webHow to check your result
Once the Pod is Ready, check that the EndpointSlice address matches the Pod IP.
Apply what you learned
The application listens on 8080 but targetPort is 80. Why can requests fail even with a correct selector?
Reveal the reasoning after trying
Backend selection is correct but traffic reaches the wrong port. Keep the client-facing Service port if appropriate and align targetPort with the application’s 8080. Verify through the original request path afterward.
Ready to check your understanding?
A Service exists but has no backends. What should you compare first?
- Service name and Node name
- Service selector and Pod labels
- Image tag and DNS name
- CPU and disk size
Reveal the reasoning after trying
Service selector and Pod labels
The selector must match Pod labels to select backends. Then check Pod readiness and targetPort.