Services, DNS, Ingress and Network Policies

Topics Covered

Services and EndpointSlices

Creating Services fast

The three port numbers

Service types

EndpointSlices: where the Service actually points

Debugging a Service with no endpoints

Services without selectors

Changing an existing Service safely

What the interviewer or grader is checking

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.

bash
1kubectl -n shop expose deployment web --port=80 --target-port=8080 --name=web
2kubectl -n shop expose deployment web --port=80 --target-port=8080 \
3  --type=NodePort --name=web-np
4kubectl -n shop expose pod debug --port=9000          # selector from the pod's labels

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.

bash
kubectl -n shop create service clusterip api --tcp=80:8080 --dry-run=client -o yaml

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.

yaml
1apiVersion: v1
2kind: Service
3metadata:
4  name: web-np
5  namespace: shop
6spec:
7  type: NodePort
8  selector:
9    app: web
10  ports:
11  - name: http
12    port: 80           # the Service's own port, used with the ClusterIP or name
13    targetPort: 8080   # the container port the traffic is sent to
14    nodePort: 30080    # the port opened on every node (NodePort and LoadBalancer only)
15    protocol: TCP

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.

A Service gives a stable address and forwards port to targetPort on any ready pod that matches its selector.

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 stays pending forever, 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.

bash
kubectl -n shop get endpointslices -l kubernetes.io/service-name=web
kubectl -n shop describe endpointslice web-abc12
kubectl -n shop get endpoints web        # older API, still works, now deprecated

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.

EndpointSlices list pod addresses, and readiness decides which ones receive traffic.

Debugging a Service with no endpoints

"The Service exists but nothing answers" is the most common networking task. Work through it in this order.

bash
1kubectl -n shop get svc web -o wide                    # note the SELECTOR column
2kubectl -n shop get pods -l app=web -o wide            # do any pods match it?
3kubectl -n shop get endpointslices -l kubernetes.io/service-name=web
4kubectl -n shop describe pod web-7d4b9-xk2p            # readiness probe failures?
5kubectl -n shop run t --rm -it --image=busybox:1.36 --restart=Never -- \
6  wget -qO- -T 2 http://web:80

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.

yaml
1apiVersion: v1
2kind: Service
3metadata:
4  name: legacy-db
5  namespace: shop
6spec:
7  ports:
8  - port: 5432
9---
10apiVersion: discovery.k8s.io/v1
11kind: EndpointSlice
12metadata:
13  name: legacy-db-1
14  namespace: shop
15  labels:
16    kubernetes.io/service-name: legacy-db
17addressType: IPv4
18ports:
19- port: 5432
20endpoints:
21- addresses: ["10.20.30.40"]

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:

bash
1kubectl -n shop set selector svc web 'app=web,tier=frontend'
2kubectl -n shop patch svc web -p '{"spec":{"type":"NodePort"}}'
3kubectl -n shop patch svc web --type=json \
4  -p '[{"op":"replace","path":"/spec/ports/0/targetPort","value":8080}]'

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.

Verify from inside the cluster

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.