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
webinto an IP, and the full Service DNS name - Why busybox's
nslookupcan 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)
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.
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:
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:
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/ #;exitends it and--rmdeletes the pod.
/ # 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 thekube-dnsService:
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
searchline: a short name is tried with each of these added. The first is the pod's own namespace, sowebbecomesweb.net-lab.svc.cluster.local. ndots:5: any name with fewer than five dots goes through the search list first.
/ # 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
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:
k run dns4 --image=busybox:1.37 --restart=Never -- nslookup web.net-lab
sleep 5
k logs dns4
k delete pod dns4
- No
-itand no--rm: the pod runs, finishes and keeps its output.k logsthen prints all of it. For one-shot tests that's more reliable than-it, which can miss output printed before kubectl attaches.
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:
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>
| From | Name | Works? |
|---|---|---|
| Same namespace | web | Yes |
| Another namespace | web | No: it becomes web.OTHER-NS.svc.cluster.local |
| Another namespace | web.net-lab | Yes for applications; no with busybox nslookup |
| Anywhere | web.net-lab.svc.cluster.local | Always |
Testing DNS on the exam: use the full name with nslookup. A short-name nslookup failing doesn't prove DNS is broken.
Service types
| Type | Gets | Reachable from |
|---|---|---|
| ClusterIP | A cluster-internal IP | Inside the cluster |
| NodePort | That, plus the same port on every node, 30000 to 32767 | Anything that can reach a node |
| LoadBalancer | Both, plus an external IP from a provider | The outside world, once a provider assigns one |
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)
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:
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 therun=NAMElabelk runadds 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.
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:
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 withexec, unlikerun --rm.
Deny everything in
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 noingress:rules: no incoming traffic allowed at all.
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
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-alljust blocks everything nothing allows.
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:
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:
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
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:
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
namespaceSelectorandpodSelectorin the same item: pods labeledk8s-app: kube-dnsin the namespacekube-system. Every namespace gets thekubernetes.io/metadata.namelabel automatically.- UDP and TCP 53: DNS mostly uses UDP, and TCP for large answers.
networkpolicy.networking.k8s.io/frontend-egress configured
<!DOCTYPE html>
| Error | Means | Look at |
|---|---|---|
Couldn't connect, instantly (Lesson 4, ticket 10) | Reached the pod; nothing listens on that port | Ports, targetPort |
download timed out | Traffic silently dropped | NetworkPolicies, firewalls |
bad address 'web' | The name couldn't become an IP | DNS, 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
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
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
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
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
podSelectorpicks the pods being protected; selectors insidefromortopick the other side. namespaceSelectorandpodSelectorin 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 logson a plain pod for one-shot commands.
Clean up
(cp1)
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 addresspoints 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
| To | Use |
|---|---|
| Make a ClusterIP Service | k expose deploy NAME -n NS --port=80 |
| Make a NodePort Service | k expose deploy NAME -n NS --name=NAME-np --type=NodePort --port=80 |
| See endpoints | k get endpointslices -n NS, or k describe svc NAME -n NS |
| A Service's DNS name | SERVICE.NAMESPACE.svc.cluster.local |
| Test DNS | k run t --image=busybox:1.37 --restart=Never -- nslookup FULL-NAME, then k logs t |
| Test a request from a pod | k exec -n NS POD -- wget -qO- -T 3 http://NAME |
| CoreDNS's IP | k get svc -n kube-system kube-dns |
| List policies | k get networkpolicy -n NS |
| Select a namespace by name | namespaceSelector: 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
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?
A LoadBalancer Service on a kubeadm cluster running on your own machines shows EXTERNAL-IP pending forever. What's missing?
After a NetworkPolicy that allows only role=frontend pods into app=web, curl to a node's NodePort from your laptop times out. Why?
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?
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?
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-dnsService. - 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
- Services (opens in a new tab), including every type.
- DNS for Services and Pods (opens in a new tab).
- Network Policies (opens in a new tab), the page to copy from on the exam.
- Debugging DNS resolution (opens in a new tab).