CKA prep, by exam domain · Lesson 8 of 12 · about 50 minutes

Services, DNS and Network Policies

How pods find and reach each other: ClusterIP, NodePort and LoadBalancer Services, DNS inside the cluster (and the test tool that lies about it), then NetworkPolicies that block everything except what you allow, including the egress rule that quietly breaks DNS.

Exam domain: Services and networking · 20% of the exam

By the end you'll know

  • What ClusterIP, NodePort and LoadBalancer each add, and why a LoadBalancer stays pending on your own machines
  • How a pod turns web into an IP, and the full Service DNS name
  • Why busybox's nslookup can fail on a name that works, and how to test DNS reliably
  • How to block all traffic to an app, then allow just one client
  • Why a NetworkPolicy also blocks NodePort traffic from outside
  • The egress rule that breaks DNS, and how to allow it
  • How to tell connection refused, a timeout and a bad address apart

Before you start

  • You need: the cluster from Lesson 2, all nodes Ready, with Calico as the network plugin. Calico enforces NetworkPolicies; Flannel would accept them and ignore them.
  • The output below is from real runs on November 27 and 28, 2026, in New York, on an Apple silicon Mac.

Services and networking is 20% of the exam, and this lesson covers most of it: how traffic finds an app, and how to stop it. Lesson 9 adds the front door, Ingress and the Gateway API.

Setup

(cp1)

BASH
k create ns net-lab
k create deploy web -n net-lab --image=nginx:alpine --replicas=2
k expose deploy web -n net-lab --port=80
k get svc,endpoints -n net-lab
  • k expose deploy web --port=80: creates a Service for the Deployment, with its pods' labels as the selector. The default type is ClusterIP.
  • k get svc,endpoints: two types in one command, comma-separated with no spaces.
OUTPUT
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/web   ClusterIP   10.111.154.149   <none>        80/TCP    20s

NAME            ENDPOINTS                             AGE
endpoints/web   10.244.189.113:80,10.244.235.176:80   20s

Endpoints objects are being replaced by EndpointSlices. Same information, split into slices of up to 100 addresses, so a Service with thousands of pods isn't one huge object:

OUTPUT
vagrant@cp1:~$ k get endpointslices -n net-lab
NAME        ADDRESSTYPE   PORTS   ENDPOINTS                       AGE
web-g5q8s   IPv4          80      10.244.189.113,10.244.235.176   6m22s

k describe svc still shows an Endpoints: line, and Lesson 4's checks still work.

DNS: how a pod finds a Service

From a temporary pod in the same namespace:

BASH
k run dns -n net-lab --rm -it --image=busybox:1.37 --restart=Never -- sh
  • -- sh: a shell instead of a one-off command, so you can run several. The prompt becomes / #; exit ends it and --rm deletes the pod.
OUTPUT
/ # cat /etc/resolv.conf
search net-lab.svc.cluster.local svc.cluster.local cluster.local localdomain
nameserver 10.96.0.10
options ndots:5
  • nameserver 10.96.0.10: every pod asks CoreDNS, at the IP of the kube-dns Service:
OUTPUT
vagrant@cp1:~$ k get svc -n kube-system kube-dns
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   3d18h
  • The search line: a short name is tried with each of these added. The first is the pod's own namespace, so web becomes web.net-lab.svc.cluster.local.
  • ndots:5: any name with fewer than five dots goes through the search list first.
OUTPUT
/ # nslookup web
Server:		10.96.0.10
Address:	10.96.0.10:53

Name:	web.net-lab.svc.cluster.local
Address: 10.111.154.149

** server can't find web.svc.cluster.local: NXDOMAIN
** server can't find web.cluster.local: NXDOMAIN
** server can't find web.localdomain: NXDOMAIN
...
/ # nslookup web.net-lab.svc.cluster.local
...
Name:	web.net-lab.svc.cluster.local
Address: 10.111.154.149
/ # wget -qO- http://web | head -4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

The first answer is the real one: the Service's ClusterIP. The NXDOMAIN ("no such name") lines after it are busybox's nslookup trying the other search domains anyway. They're harmless. The full name answers cleanly.

The Service name format: SERVICE.NAMESPACE.svc.cluster.local.

From another namespace

OUTPUT
vagrant@cp1:~$ k run dns2 --rm -it --image=busybox:1.37 --restart=Never -- nslookup web
...
** server can't find web.default.svc.cluster.local: NXDOMAIN
...
pod default/dns2 terminated (Error)

From default, web becomes web.default.svc.cluster.local, which doesn't exist. So try web.net-lab:

BASH
k run dns4 --image=busybox:1.37 --restart=Never -- nslookup web.net-lab
sleep 5
k logs dns4
k delete pod dns4
  • No -it and no --rm: the pod runs, finishes and keeps its output. k logs then prints all of it. For one-shot tests that's more reliable than -it, which can miss output printed before kubectl attaches.
OUTPUT
Server:		10.96.0.10
Address:	10.96.0.10:53

** server can't find web.net-lab: NXDOMAIN

** server can't find web.net-lab: NXDOMAIN

Failed. But the name isn't the problem. Busybox's nslookup only uses the search list for names without a dot, so web.net-lab was sent as-is and isn't a real name. Real applications look names up through the system resolver, which follows ndots:5:

OUTPUT
vagrant@cp1:~$ k run dns5 --image=busybox:1.37 --restart=Never -- wget -qO- -T 5 http://web.net-lab
vagrant@cp1:~$ sleep 5
vagrant@cp1:~$ k logs dns5 | head -4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
FromNameWorks?
Same namespacewebYes
Another namespacewebNo: it becomes web.OTHER-NS.svc.cluster.local
Another namespaceweb.net-labYes for applications; no with busybox nslookup
Anywhereweb.net-lab.svc.cluster.localAlways

Testing DNS on the exam: use the full name with nslookup. A short-name nslookup failing doesn't prove DNS is broken.

Service types

TypeGetsReachable from
ClusterIPA cluster-internal IPInside the cluster
NodePortThat, plus the same port on every node, 30000 to 32767Anything that can reach a node
LoadBalancerBoth, plus an external IP from a providerThe outside world, once a provider assigns one
OUTPUT
vagrant@cp1:~$ k expose deploy web -n net-lab --name=web-np --type=NodePort --port=80
service/web-np exposed
vagrant@cp1:~$ k expose deploy web -n net-lab --name=web-lb --type=LoadBalancer --port=80
service/web-lb exposed
vagrant@cp1:~$ k get svc -n net-lab
NAME     TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
web      ClusterIP      10.111.154.149   <none>        80/TCP         15m
web-lb   LoadBalancer   10.97.241.78     <pending>     80:31136/TCP   0s
web-np   NodePort       10.104.18.80     <none>        80:32044/TCP   29s
  • 80:32044/TCP: Service port 80, node port 32044, picked by Kubernetes.
  • EXTERNAL-IP <pending> stays that way. A LoadBalancer asks something outside Kubernetes for an IP: on AWS, Azure or Google Cloud, the cloud's controller creates one. Your machines have no such controller. On bare metal, a tool like MetalLB fills that role.
  • The LoadBalancer still got a node port, 31136. A LoadBalancer is a NodePort plus an outside load balancer that forwards to it.

(Your machine)

OUTPUT
MKB@KwamiB cka-practice$ curl -I --max-time 5 http://192.168.34.155:32044
HTTP/1.1 200 OK
Server: nginx/1.31.10
...
MKB@KwamiB cka-practice$ curl -I --max-time 5 http://192.168.34.154:32044
HTTP/1.1 200 OK
Server: nginx/1.31.10

cp1 answered too, with no web pod on it. kube-proxy runs on every node, and forwards a node port on any node to a ready pod anywhere in the cluster.

NetworkPolicies: block, then allow

By default every pod can reach every pod, across all namespaces. A NetworkPolicy restricts that, for the pods it selects. Two clients first, one labeled as the frontend:

BASH
k run frontend -n net-lab --image=busybox:1.37 --labels=role=frontend -- sleep 3600
k run other -n net-lab --image=busybox:1.37 -- sleep 3600
k get pods -n net-lab --show-labels
  • --labels=role=frontend: a label a policy can select. It replaces the run=NAME label k run adds otherwise.

My first try had --image=busybix:1.37. Kubernetes accepted it (pod/frontend created), and only the node, trying to pull it, found out: ErrImagePull. Running the command again gave AlreadyExists; a pod's image can't be changed by running it again. k delete pod frontend -n net-lab $now and a fresh k run fixed it.

OUTPUT
NAME                   READY   STATUS              RESTARTS   AGE   LABELS
frontend               0/1     ContainerCreating   0          0s    role=frontend
other                  0/1     ContainerCreating   0          0s    run=other
web-6bd469df5c-2nsx7   1/1     Running             0          19m   app=web,pod-template-hash=6bd469df5c
web-6bd469df5c-lfqkv   1/1     Running             0          19m   app=web,pod-template-hash=6bd469df5c

Both clients can reach web before any policy:

OUTPUT
vagrant@cp1:~$ k exec -n net-lab frontend -- wget -qO- -T 3 http://web | head -1
<!DOCTYPE html>
vagrant@cp1:~$ k exec -n net-lab other -- wget -qO- -T 3 http://web | head -1
<!DOCTYPE html>
  • k exec POD --: runs a command inside a running pod. Everything after -- belongs to the pod.
  • wget -qO- -T 3: fetch quietly, print to the screen, give up after 3 seconds.
  • | head -1: runs on cp1, keeping the first line. Fine with exec, unlike run --rm.

Deny everything in

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-deny-all
  namespace: net-lab
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Ingress
  • podSelector: the pods this policy protects.
  • policyTypes: [Ingress] with no ingress: rules: no incoming traffic allowed at all.
OUTPUT
vagrant@cp1:~$ k apply -f deny.yaml
networkpolicy.networking.k8s.io/web-deny-all created
vagrant@cp1:~$ k exec -n net-lab frontend -- wget -qO- -T 3 http://web | head -1
wget: download timed out
command terminated with exit code 1
vagrant@cp1:~$ k exec -n net-lab other -- wget -qO- -T 3 http://web | head -1
wget: download timed out
command terminated with exit code 1

A timeout, not a refusal: Calico drops the packets silently, so the client hears nothing until it gives up.

Allow the frontend

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-allow-frontend
  namespace: net-lab
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 80
  • from: - podSelector: role: frontend: only from pods with that label, in this namespace.
  • ports: only TCP 80.
  • Policies add up. Traffic any policy allows gets through; web-deny-all just blocks everything nothing allows.
OUTPUT
vagrant@cp1:~$ k get networkpolicy -n net-lab
NAME                 POD-SELECTOR   AGE
web-allow-frontend   app=web        0s
web-deny-all         app=web        43s
vagrant@cp1:~$ k exec -n net-lab frontend -- wget -qO- -T 3 http://web | head -1
<!DOCTYPE html>
vagrant@cp1:~$ k exec -n net-lab other -- wget -qO- -T 3 http://web | head -1
wget: download timed out
command terminated with exit code 1

And from your Mac, through the NodePort that answered a few minutes earlier:

OUTPUT
MKB@KwamiB cka-practice$ curl -I --max-time 5 http://192.168.34.155:32044
curl: (28) Connection timed out after 5002 milliseconds

A NetworkPolicy protects the pod however traffic arrives: directly, through a ClusterIP, or through a NodePort from outside. Your Mac isn't a role=frontend pod.

Egress, and the DNS trap

Policies can also control what a pod sends out. Allow frontend to reach only web, on port 80:

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: frontend-egress
  namespace: net-lab
spec:
  podSelector:
    matchLabels:
      role: frontend
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: web
    ports:
    - protocol: TCP
      port: 80
OUTPUT
vagrant@cp1:~$ k exec -n net-lab frontend -- wget -qO- -T 3 http://web | head -1
wget: bad address 'web'
command terminated with exit code 1
vagrant@cp1:~$ k exec -n net-lab frontend -- wget -qO- -T 3 http://10.111.154.149 | head -1
<!DOCTYPE html>

By IP it works, by name it doesn't. Using a name means asking CoreDNS first, on UDP port 53, and the policy allows only TCP 80 to web. The DNS question was dropped, so wget never learned an IP. Add a rule for DNS:

YAML
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: web
    ports:
    - protocol: TCP
      port: 80
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
  • namespaceSelector and podSelector in the same item: pods labeled k8s-app: kube-dns in the namespace kube-system. Every namespace gets the kubernetes.io/metadata.name label automatically.
  • UDP and TCP 53: DNS mostly uses UDP, and TCP for large answers.
OUTPUT
networkpolicy.networking.k8s.io/frontend-egress configured
<!DOCTYPE html>
ErrorMeansLook at
Couldn't connect, instantly (Lesson 4, ticket 10)Reached the pod; nothing listens on that portPorts, targetPort
download timed outTraffic silently droppedNetworkPolicies, firewalls
bad address 'web'The name couldn't become an IPDNS, or an egress policy blocking it

How the drills work

Each drill is a task to do from a blank terminal. Try it first, without looking. Then open "Show the fast way" to compare your commands, and read the notes under them: they cover the part people usually get wrong. Repeat a drill a few days later until you can do it cold, because the exam leaves little time to look things up.

The drills

Expose Deployment web on a NodePort, check it has endpoints, and reach it from outside the cluster.

Show the fast way
BASH
k expose deploy web -n NS --name=web-np --type=NodePort --port=80
k get svc web-np -n NS                 # PORT(S) 80:3XXXX/TCP
k get endpointslices -n NS
curl -I --max-time 5 http://NODE-IP:3XXXX

Any node's IP works, whether or not a pod runs there. No endpoints means the selector matches no ready pod (Lesson 4). A task may ask for a specific node port: add it with k edit svc under nodePort:, in the range 30000 to 32767.

From inside a pod, look up a Service in another namespace by its full DNS name, and find which IP pods use for DNS.

Show the fast way
BASH
k run dnstest --image=busybox:1.37 --restart=Never -- nslookup SVC.NS.svc.cluster.local
sleep 5
k logs dnstest
k delete pod dnstest
k get svc -n kube-system kube-dns

Use the full name with nslookup: busybox's version skips the search list for names with a dot. A plain pod plus k logs keeps every line of output. The nameserver in a pod's /etc/resolv.conf is the kube-dns Service's ClusterIP.

Block all incoming traffic to pods labeled app=api in namespace NS, then allow only pods labeled role=frontend to reach them on port 8080. Prove both.

Show the fast way
YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-allow-frontend
  namespace: NS
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 8080

One policy does both: selecting the pods with policyTypes: [Ingress] already blocks everything not allowed by a rule. Prove it from a role=frontend pod (works) and an unlabeled one (times out). Copy the YAML from the docs ("Network Policies") and change the labels.

Allow pods labeled role=frontend to send traffic only to app=web on port 80, without breaking DNS.

Show the fast way
YAML
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: web
    ports:
    - protocol: TCP
      port: 80
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53

Forgetting DNS is the classic mistake: names fail with bad address while IPs still work. Test by name, not by IP.

On exam day

  • Get the selectors right: the top podSelector picks the pods being protected; selectors inside from or to pick the other side.
  • namespaceSelector and podSelector in one item mean "and"; as two separate list items (each with its own -), they mean "or". One dash changes the meaning.
  • Egress policies need DNS unless the task says otherwise.
  • Test with a timeout (wget -T 3, curl --max-time 5), so a blocked request fails fast.
  • Use full names for DNS tests, and k logs on a plain pod for one-shot commands.

Clean up

(cp1)

BASH
k delete ns net-lab
rm deny.yaml allow.yaml egress.yaml

Deleting the namespace removes the Deployment, the three Services, the client pods and the policies.

In real life

  • Start each namespace with a default-deny policy, then allow each flow you need. Anything new is blocked until someone decides it should talk.
  • Real clusters get LoadBalancer IPs from the cloud, or from MetalLB or a similar tool on bare metal. NodePorts are rarely used directly by users.
  • Policy-aware network plugins differ in extras. Calico and Cilium add their own policy types, such as cluster-wide policies, on top of the standard NetworkPolicy.
  • DNS is the first thing to check when an app "can't reach" something by name. A bad address points straight at it.

Spot the problem

Work out what's wrong before you read the explanation.

"We blocked everything except the frontend, and now the frontend can't reach anything by name." A team adds an egress policy to their frontend allowing only TCP 443 to their API pods. Every request by name now fails with bad address. Why? The policy blocks DNS. Allow UDP and TCP 53 to the kube-dns pods in kube-system.

"The policy says frontend only, but the load balancer health checks fail." After a NetworkPolicy allowing only role=frontend, an external load balancer that reaches the app through a NodePort marks it down. Why? NetworkPolicies apply however traffic arrives, NodePort included. The load balancer's traffic needs its own rule, such as an ipBlock for its addresses.

"DNS is broken: nslookup can't find the Service." From another namespace, nslookup web.shop in a busybox pod returns NXDOMAIN, but the app calling http://web.shop works. Is DNS broken? No: busybox's nslookup skips the search list for names with a dot. nslookup web.shop.svc.cluster.local succeeds.

Keep this

ToUse
Make a ClusterIP Servicek expose deploy NAME -n NS --port=80
Make a NodePort Servicek expose deploy NAME -n NS --name=NAME-np --type=NodePort --port=80
See endpointsk get endpointslices -n NS, or k describe svc NAME -n NS
A Service's DNS nameSERVICE.NAMESPACE.svc.cluster.local
Test DNSk run t --image=busybox:1.37 --restart=Never -- nslookup FULL-NAME, then k logs t
Test a request from a podk exec -n NS POD -- wget -qO- -T 3 http://NAME
CoreDNS's IPk get svc -n kube-system kube-dns
List policiesk get networkpolicy -n NS
Select a namespace by namenamespaceSelector: matchLabels: kubernetes.io/metadata.name: NS

Where you are now

You've seen all three Service types, including why a LoadBalancer stays pending without a provider. You know how a pod turns a name into an IP, and how to test DNS without being fooled by the test tool. You've locked an app down to one client, watched the policy block a NodePort from outside, and fixed the egress rule that breaks DNS. Lesson 9 adds the front door: Ingress and the Gateway API.

Check yourself

  1. A pod in namespace shop calls http://api, and the api Service lives in namespace backend. The request fails with bad address. What's the fix in the calling code?

  2. A LoadBalancer Service on a kubeadm cluster running on your own machines shows EXTERNAL-IP pending forever. What's missing?

  3. After a NetworkPolicy that allows only role=frontend pods into app=web, curl to a node's NodePort from your laptop times out. Why?

  4. A request from a pod times out after a policy is applied. A different request fails instantly with connection refused. Which one points at the NetworkPolicy?

  5. An egress policy allows a pod only TCP 80 to app=web. Requests by IP work; by name they fail with bad address. What do you add?

  6. In a busybox pod in namespace default, nslookup web.shop returns NXDOMAIN, but wget http://web.shop works. Why?

Key terms

  • Service: a stable IP and name in front of a changing set of pods.
  • ClusterIP / NodePort / LoadBalancer: a Service reachable inside the cluster, on every node's port, or through an external IP from a provider.
  • EndpointSlice: the list of pod IPs and ports a Service sends traffic to, replacing Endpoints.
  • kube-proxy: runs on every node and forwards Service traffic to pods.
  • CoreDNS: the cluster's DNS server, reached at the kube-dns Service.
  • Search domains and ndots: how a short name gets the rest of its DNS name added.
  • NetworkPolicy: rules for which traffic may reach (ingress) or leave (egress) selected pods.
  • Default deny: a policy that selects pods and allows nothing, so only explicitly allowed traffic gets through.

Go deeper