0%
Kubernetes CKA and CKAD Exam Prep: Hands On Task Drills and Mock Exams
How the Exams Work
Administrator (CKA) Skills
Mock Exams
Services, DNS, Ingress and Network Policies
Networking tasks are spread across both exams. The CKAD expects you to expose applications with Services and Ingress and to restrict traffic with NetworkPolicies. The CKA adds the cluster side: how DNS works, what the CNI plugin does and how to repair a broken CoreDNS. This lesson covers all of it as task patterns. You already know from the containers course why Services exist. Here the focus is producing the right object in one command, knowing which field controls which number, and proving that traffic actually flows.
Creating Services fast
kubectl expose reads the selector from the object you expose, so it is the quickest correct way to create a Service for a Deployment.
kubectl create service also exists, but it builds a selector from the Service name (app=NAME), which is only right if your pods happen to carry that label. Use it when the task gives you the selector explicitly and you are going to edit the output, otherwise prefer expose.
The generated YAML has selector: app: api. Check that it matches the pod labels with kubectl -n shop get pods --show-labels before you apply it.
The three port numbers
A Service manifest has up to three port fields, and the exam likes to swap them.
port is what clients inside the cluster use: http://web-np.shop:80. targetPort is where the container listens; it defaults to the same value as port when omitted, which is a common source of silent failures when the app listens on 8080. nodePort must be in the cluster's node port range, 30000 to 32767 by default, and is assigned randomly if you leave it out. A task that says "expose on node port 30080" needs the field set explicitly, which means editing the generated YAML or patching the Service afterwards.
targetPort can also be a name. If the container declares ports: - name: http containerPort: 8080, the Service can use targetPort: http. That lets different versions of the pod use different port numbers behind one Service, and it is a frequent pattern in Helm charts.
Service types
ClusterIP(the default): a virtual IP reachable only inside the cluster.NodePort: a ClusterIP plus a port on every node's IP address.LoadBalancer: a NodePort plus an external load balancer provisioned by a cloud controller or an add on such as MetalLB. On a bare kubeadm exam cluster without one, the external IP stayspendingforever, which is expected and not something to fix unless the task says so.ExternalName: no proxying at all; DNS returns a CNAME to an external host name. It has no selector and no endpoints.
A headless Service sets clusterIP: None. It gets no virtual IP, and DNS returns the individual pod IPs instead. StatefulSets use headless Services to give each pod a stable DNS name, which the next section covers.
Two traffic fields come up occasionally. sessionAffinity: ClientIP sends repeat requests from the same client IP to the same pod. externalTrafficPolicy: Local on NodePort and LoadBalancer Services keeps external traffic on the node it arrived at, which preserves the client's source IP but drops traffic on nodes that have no local pod. The default, Cluster, spreads traffic across all pods but replaces the source IP with the node's.
EndpointSlices: where the Service actually points
A Service on its own is just an IP and a selector. The EndpointSlice controller watches pods that match the selector and writes their IPs and ports into EndpointSlice objects. kube-proxy, or the CNI's replacement for it, reads those slices and programs the routing on each node.
Each endpoint in a slice has conditions: ready, serving and terminating. Only ready endpoints receive new traffic. A pod that matches the selector but fails its readiness probe stays in the slice marked not ready, which is why a Service can have pods behind it and still return connection refused. The older Endpoints API is deprecated in recent releases in favour of EndpointSlices; kubectl get endpoints still works and prints a warning, and it remains a fast way to see ready addresses.
Debugging a Service with no endpoints
"The Service exists but nothing answers" is the most common networking task. Work through it in this order.
If the selector matches no pods, the cause is a label typo in the Service or the pod template. Fix whichever the task allows, usually the Service with kubectl edit svc or kubectl set selector svc web app=web. If pods match but are not ready, read the readiness probe events. If endpoints are ready but the request still fails, compare targetPort with the port the container actually listens on; kubectl exec into the pod and run netstat -tlnp or ss -tlnp if the image has them, or read the container's ports and the application's configuration.
Services without selectors
Occasionally a task asks for a Service that points to something outside the cluster, such as a database on a fixed IP. Create the Service without a selector, then create an EndpointSlice yourself with the label kubernetes.io/service-name set to the Service name.
Pods can now connect to legacy-db.shop:5432, and the traffic goes to 10.20.30.40. If the external system has a DNS name rather than an IP, an ExternalName Service is simpler, though it does not remap ports.
Changing an existing Service safely
Tasks often hand you a Service that is almost right. Three commands cover most edits without opening an editor:
set selector replaces the whole selector, so include every label you want to keep. Changing type from ClusterIP to NodePort keeps the ClusterIP and adds a random node port; changing back removes the node port. The ClusterIP itself is immutable, so a task that asks for a specific ClusterIP means deleting and recreating the Service with spec.clusterIP set. After any edit, recheck the EndpointSlices: a selector change can empty them in an instant, and that is easy to miss when you only look at the Service.
What the interviewer or grader is checking
For a Service task the checks are mechanical: the Service exists with the right name, type and ports in the right namespace, its selector matches the intended pods, and its EndpointSlices contain ready addresses. A Service with every field correct but zero endpoints is usually scored as a failure, because the task almost always implies that traffic must reach the application. Spend the extra ten seconds on kubectl get endpointslices before you move on, and test one request from a throwaway pod.
Always test a Service from a throwaway pod: kubectl run t --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 2 http://SERVICE:PORT. Testing from your terminal host only proves the node port, not the in cluster path the task usually asks about.