Day 17: NetworkPolicies
Goal: You block your application’s traffic by default, open only the paths it needs, and read existing policies fast enough to set the right Pod labels. · Duration: approx. 75 min · Prerequisite: Day 16
Briefing
CKAD: Services and Networking (20 %) – “Demonstrate basic understanding of NetworkPolicies”.
- Without a policy, every Pod may talk to every other Pod – across namespaces, too.
- Once a policy selects a Pod, only what some policy allows is
permitted for it in that direction (
Ingress,Egress). Policies add up; there is no “deny”. - They are enforced by the CNI plugin, here Calico v3.33.1.
tag17-other/reporter ──┐
tag17/client ──────────┴─> tag17/notes :8080 ──> tag17/db :5432
(all: DNS ──> kube-system/kube-dns :53)
Drill 1: The test field
mkdir -p ~/ckad/tag17 && cd ~/ckad/tag17
k create ns tag17
k create ns tag17-other
k config set-context --current --namespace=tag17
cat > db.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
spec:
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: postgres
image: docker.io/library/postgres:18
env:
- name: POSTGRES_USER
value: notes
- name: POSTGRES_PASSWORD
value: lab-db-pw-123
ports:
- containerPort: 5432
readinessProbe:
exec:
command: ["pg_isready", "-U", "notes"]
EOF
k apply -f db.yaml
k expose deployment db --port=5432
k rollout status deployment dbThe readiness probe makes sure that rollout status only
returns once Postgres accepts connections – otherwise notes-api aborts
at startup with “database unreachable”.
cat > notes.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: notes
spec:
replicas: 2
selector:
matchLabels:
app: notes
template:
metadata:
labels:
app: notes
spec:
containers:
- name: notes-api
image: 172.16.0.126:30500/notes-api:1.0
env:
- name: NOTES_API_KEY
value: lab-key-123
- name: NOTES_DB_URL
value: postgres://notes:lab-db-pw-123@db:5432/notes?sslmode=disable
ports:
- containerPort: 8080
name: http
EOF
k apply -f notes.yaml
k expose deployment notes --port=80 --target-port=8080
k rollout status deployment notes
k run client --image=busybox:1.37 -- sleep 3600
k run outsider -n tag17-other --image=busybox:1.37 -- sleep 3600
k get podsExpected (abridged):
deployment "notes" successfully rolled out
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 8s
db-7bb897448b-2zhfz 1/1 Running 0 18s
notes-fd744685f-74qrc 1/1 Running 0 3s
notes-fd744685f-qscvr 1/1 Running 0 3s
The API key and password are pure lab values. Now test the paths –
always with a timeout (-T2 for wget, -w2 for
nc), otherwise you wait forever when traffic is blocked:
k exec client -- wget -qO- -T2 http://notes/healthz
k exec client -- nc -zv -w2 db 5432
k exec -n tag17-other outsider -- wget -qO- -T2 http://notes.tag17/healthz
k exec -n tag17-other outsider -- nc -zv -w2 db.tag17 5432Expected:
ok
db (10.97.47.147:5432) open
ok
db.tag17 (10.97.47.147:5432) open
Everything is open – even the foreign namespace gets straight to the
database. /healthz only returns ok if
notes-api reaches the database.
Save your progress? Sign up for free
Drill 2: Default deny for ingress
cat > deny-all.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes:
- Ingress
EOF
k apply -f deny-all.yaml
k get netpol
k exec client -- wget -qO- -T2 http://notes/healthz
k exec client -- nc -zv -w2 db 5432
k exec deploy/notes -- nc -zv -w2 db 5432Expected:
networkpolicy.networking.k8s.io/default-deny created
NAME POD-SELECTOR AGE
default-deny <none> 0s
wget: download timed out
command terminated with exit code 1
nc: db (10.97.47.147:5432): Connection timed out
command terminated with exit code 1
nc: db (10.97.47.147:5432): Operation timed out
command terminated with exit code 1
podSelector: {} selects all Pods in the namespace, and
there is no ingress list – so nothing is allowed. With
Calico, blocked means packets are dropped, hence a timeout and not
“connection refused”.
Warning: Existing connections stay open. In the lab, notes-api’s
/healthzstill reportedokfor about a minute – until the connection pool closed the old database connection. After that:503 Service Unavailable.
Save your progress? Sign up for free
Drill 3: Allowing selectively – podSelector and ports
Only Pods with the label access=notes should reach
notes-api. First attempt with the Service port:
cat > notes-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: notes-ingress
spec:
podSelector:
matchLabels:
app: notes
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
access: notes
ports:
- port: 80
EOF
k apply -f notes-ingress.yaml
k label pod client access=notes
k exec client -- wget -qO- -T2 http://notes/versionExpected:
networkpolicy.networking.k8s.io/notes-ingress created
pod/client labeled
wget: download timed out
command terminated with exit code 1
The policy acts at the Pod, after the Service translation. So what
counts is the container port 8080 (or its name
http), not port 80 of the Service:
sed -i 's/port: 80$/port: 8080/' notes-ingress.yaml
k apply -f notes-ingress.yaml
k exec client -- wget -qO- -T2 http://notes/version
k label pod client access-
k exec client -- wget -qO- -T2 http://notes/version
k label pod client access=notesExpected:
networkpolicy.networking.k8s.io/notes-ingress configured
1.0
pod/client unlabeled
wget: download timed out
command terminated with exit code 1
pod/client labeled
The label decides – not the Pod name. Now the database: only notes may connect.
cat > db-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-ingress
spec:
podSelector:
matchLabels:
app: db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: notes
ports:
- port: 5432
EOF
k apply -f db-ingress.yaml
k exec deploy/notes -- nc -zv -w2 db 5432
k exec client -- nc -zv -w2 db 5432
k exec client -- wget -qO- -T5 http://notes/healthzExpected:
networkpolicy.networking.k8s.io/db-ingress created
db (10.97.47.147:5432) open
nc: db (10.97.47.147:5432): Connection timed out
command terminated with exit code 1
ok
For /healthz, use -T5 here: notes-api waits
up to 2 s for the database, so with -T2 wget may give up
first.
Save your progress? Sign up for free
Drill 4: namespaceSelector – AND or OR
Every namespace automatically carries the label
kubernetes.io/metadata.name=<name>:
k get ns tag17-other --show-labels
k run partner -n tag17-other --image=busybox:1.37 -l access=notes -- sleep 3600
k exec -n tag17-other partner -- wget -qO- -T2 http://notes.tag17/versionExpected:
NAME STATUS AGE LABELS
tag17-other Active 4m35s kubernetes.io/metadata.name=tag17-other
pod/partner created
wget: download timed out
command terminated with exit code 1
partner does have access=notes, but a
podSelector on its own only applies in the policy’s
own namespace. Variant A – two entries in
from:
cat > notes-or.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: notes-from-other
spec:
podSelector:
matchLabels:
app: notes
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tag17-other
- podSelector:
matchLabels:
access: notes
ports:
- port: 8080
EOF
k apply -f notes-or.yaml
k exec -n tag17-other partner -- wget -qO- -T2 http://notes.tag17/version
k exec -n tag17-other outsider -- wget -qO- -T2 http://notes.tag17/versionExpected:
networkpolicy.networking.k8s.io/notes-from-other created
1.0
1.0
Even outsider without a label gets through: two list
entries mean OR – “any Pod from tag17-other” or “a Pod
with access=notes from tag17”. Variant B – a single entry,
so one - less:
sed 's/^ - podSelector:/ podSelector:/' notes-or.yaml > notes-and.yaml
diff notes-or.yaml notes-and.yaml
k apply -f notes-and.yaml
k exec -n tag17-other partner -- wget -qO- -T2 http://notes.tag17/version
k exec -n tag17-other outsider -- wget -qO- -T2 http://notes.tag17/version
k describe netpol notes-from-other | sed -n '/Allowing ingress/,/Not affecting/p'Expected:
16c16
< - podSelector:
---
> podSelector:
networkpolicy.networking.k8s.io/notes-from-other configured
1.0
wget: download timed out
command terminated with exit code 1
Allowing ingress traffic:
To Port: 8080/TCP
From:
NamespaceSelector: kubernetes.io/metadata.name=tag17-other
PodSelector: access=notes
Not affecting egress traffic
One entry with both selectors means AND: Pods with
access=notes in tag17-other. k describe gives
it away: variant A shows two From: blocks, variant B shows
one.
Save your progress? Sign up for free
Drill 5: Default deny for egress – with a DNS exception
cat > deny-egress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
spec:
podSelector: {}
policyTypes:
- Egress
EOF
k apply -f deny-egress.yaml
k exec client -- wget -qO- -T2 http://notes/version
k exec client -- nslookup -timeout=2 notesExpected:
networkpolicy.networking.k8s.io/default-deny-egress created
wget: bad address 'notes'
command terminated with exit code 1
;; connection timed out; no servers could be reached
command terminated with exit code 1
bad address instead of timed out: even the
DNS query to CoreDNS is egress and therefore blocked. The exception for
all Pods in the namespace, UDP and TCP 53:
cat > allow-dns.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- port: 53
protocol: UDP
- port: 53
protocol: TCP
EOF
k apply -f allow-dns.yaml
k exec client -- nslookup notes.tag17.svc.cluster.local
k exec client -- wget -qO- -T2 http://notes/versionExpected:
networkpolicy.networking.k8s.io/allow-dns created
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: notes.tag17.svc.cluster.local
Address: 10.102.130.4
wget: download timed out
command terminated with exit code 1
The name resolves, but the connection to notes stays blocked – it
needs its own egress rule. A short form without to that you
often see in tasks (Drill 8): just ports with 53 allows DNS
to any destination.
Save your progress? Sign up for free
Drill 6: Egress for the application
Inside a namespace with default deny in both directions, every connection needs two permissions: egress at the sender, ingress at the receiver.
cat > egress-apps.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-egress
spec:
podSelector:
matchLabels:
run: client
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: notes
ports:
- port: 8080
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: notes-egress
spec:
podSelector:
matchLabels:
app: notes
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: db
ports:
- port: 5432
EOF
k apply -f egress-apps.yaml
k exec client -- wget -qO- -T2 http://notes/version
k exec client -- wget -qO- -T5 http://notes/healthz
k exec deploy/notes -- nc -zv -w2 db 5432
k exec client -- nc -zv -w2 db 5432Expected:
networkpolicy.networking.k8s.io/client-egress created
networkpolicy.networking.k8s.io/notes-egress created
1.0
ok
db (10.97.47.147:5432) open
nc: db (10.97.47.147:5432): Connection timed out
command terminated with exit code 1
run: client is the label that k run sets
automatically. Egress rules also name the container port (8080), not the
Service port.
Save your progress? Sign up for free
Drill 7: ipBlock for destinations outside the cluster
For addresses without Pod labels – nodes, external databases, the
corporate network – there is ipBlock, optionally with
except:
k exec client -- nc -zv -w2 172.16.0.126 6443
cat > client-ipblock.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-to-nodes
spec:
podSelector:
matchLabels:
run: client
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 172.16.0.0/24
except:
- 172.16.0.228/32
EOF
k apply -f client-ipblock.yaml
k exec client -- nc -zv -w2 172.16.0.126 6443
k exec client -- nc -zv -w2 172.16.0.83 10250
k exec client -- nc -zv -w2 172.16.0.228 10250Expected:
nc: 172.16.0.126 (172.16.0.126:6443): Connection timed out
command terminated with exit code 1
networkpolicy.networking.k8s.io/client-to-nodes created
172.16.0.126 (172.16.0.126:6443) open
172.16.0.83 (172.16.0.83:10250) open
nc: 172.16.0.228 (172.16.0.228:10250): Connection timed out
command terminated with exit code 1
cp1 and worker2 are reachable, worker1 (except) is not.
An ipBlock must not appear together with
podSelector or namespaceSelector in the same
entry. Pod IPs change – for Pods, always use selectors.
Save your progress? Sign up for free
Drill 8: Exam task – reading policies, setting labels
A typical task: “Pod reporter in
tag17-other should reach notes in tag17. Do
not change any NetworkPolicy.” The policies in tag17-other
already exist:
cat > other-policies.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: tag17-other
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
egress:
- ports:
- port: 53
protocol: UDP
- port: 53
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: to-notes
namespace: tag17-other
spec:
podSelector:
matchLabels:
team: notes-users
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tag17
podSelector:
matchLabels:
app: notes
ports:
- port: 8080
EOF
k apply -f other-policies.yaml
k run reporter -n tag17-other --image=busybox:1.37 -- sleep 3600
k exec -n tag17-other reporter -- wget -qO- -T2 http://notes.tag17/versionExpected: wget: download timed out. Now
read – starting with the sender’s namespace:
k get netpol -n tag17-other
k describe netpol -n tag17-other to-notes | sed -n '/^Spec/,$p'Expected:
NAME POD-SELECTOR AGE
default-deny <none> 5s
to-notes team=notes-users 5s
Spec:
PodSelector: team=notes-users
Not affecting ingress traffic
Allowing egress traffic:
To Port: 8080/TCP
To:
NamespaceSelector: kubernetes.io/metadata.name=tag17
PodSelector: app=notes
Policy Types: Egress
k label pod -n tag17-other reporter team=notes-users
k exec -n tag17-other reporter -- wget -qO- -T2 http://notes.tag17/versionExpected: again
wget: download timed out – egress is open, but the receiver
does not let it in. So read there as well:
k get netpol -n tag17 -o wide | grep app=notes
k describe netpol -n tag17 notes-from-other | grep -A4 'Allowing ingress'
k label pod -n tag17-other reporter access=notes
k exec -n tag17-other reporter -- wget -qO- -T2 http://notes.tag17/version
k exec -n tag17-other reporter -- nc -zv -w2 db.tag17 5432Expected (abridged):
notes-egress app=notes 3m30s
notes-from-other app=notes 4m29s
notes-ingress app=notes 5m28s
Allowing ingress traffic:
To Port: 8080/TCP
From:
NamespaceSelector: kubernetes.io/metadata.name=tag17-other
PodSelector: access=notes
pod/reporter labeled
1.0
nc: db.tag17 (10.97.47.147:5432): Connection timed out
command terminated with exit code 1
Two labels, two namespaces – and the database still stays closed. Open exactly as much as required.
Save your progress? Sign up for free
Exercise: Speed Drills
- Write a policy
deny-allfortag17that forbids ingress and egress for all Pods, and check it only with--dry-run=server. Target time: 60 s - Which label does a Pod in
tag17need to reachdb? Target time: 20 s - Allow Pods with
monitoring=truefrom all namespaces to access notes on port 8080 (only--dry-run=server). Target time: 90 s - Replace
client-to-nodes:clientmay only reach the NodePorts 30000–32767 in the network 172.16.0.0/24. Target time: 90 s
Solution
k apply --dry-run=server -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
EOF
k get netpol db-ingress -o jsonpath='{.spec.ingress[0].from[0].podSelector}'; echo
k apply --dry-run=server -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: notes-monitoring
spec:
podSelector:
matchLabels:
app: notes
ingress:
- from:
- namespaceSelector: {}
podSelector:
matchLabels:
monitoring: "true"
ports:
- port: 8080
EOF
k delete netpol client-to-nodes
k apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-nodeports
spec:
podSelector:
matchLabels:
run: client
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 172.16.0.0/24
ports:
- port: 30000
endPort: 32767
EOF
k exec client -- wget -qO- -T2 http://172.16.0.126:30500/v2/_catalog
k exec client -- nc -zv -w2 172.16.0.126 6443Expected:
networkpolicy.networking.k8s.io/deny-all created (server dry run)
{"matchLabels":{"app":"notes"}}
networkpolicy.networking.k8s.io/notes-monitoring created (server dry run)
networkpolicy.networking.k8s.io "client-to-nodes" deleted from tag17 namespace
networkpolicy.networking.k8s.io/client-nodeports created
{"repositories":["notes-api"]}
nc: 172.16.0.126 (172.16.0.126:6443): Connection timed out
command terminated with exit code 1
On 2: app=notes –
k describe netpol db-ingress shows it just as well. On 3:
namespaceSelector: {} means “all namespaces”; without
policyTypes the API server sets Ingress
because an ingress list is present. On 4:
endPort only works with a numeric port.
Save your progress? Sign up for free
Checkpoint
k get netpol -o custom-columns=NAME:.metadata.name,TYPES:.spec.policyTypesExpected:
NAME TYPES
allow-dns [Egress]
client-egress [Egress]
client-nodeports [Egress]
db-ingress [Ingress]
default-deny [Ingress]
default-deny-egress [Egress]
notes-egress [Egress]
notes-from-other [Ingress]
notes-ingress [Ingress]
k exec client -- wget -qO- -T5 http://notes/healthzExpected: ok – client reaches notes,
notes reaches db.
k exec client -- nc -zv -w2 db 5432Expected: Connection timed out – client
cannot get to the database directly.
k get pod -n tag17-other reporter --show-labelsExpected:
access=notes,run=reporter,team=notes-users
Clean up (after the challenge):
k config set-context --current --namespace=default
k delete ns tag17 tag17-otherChallenge
The Deployment exporter runs in tag17-other
(k create deployment exporter --image=busybox:1.37 -- sleep 3600).
Its Pods should reach notes in tag17 – permanently, even
after every restart. The policies stay unchanged.
Hint: k label deployment labels only
the Deployment, not its Pods.
Solution
k create deployment exporter -n tag17-other --image=busybox:1.37 -- sleep 3600
k label deployment -n tag17-other exporter team=notes-users access=notes
k exec -n tag17-other deploy/exporter -- wget -qO- -T2 http://notes.tag17/version
k patch deployment -n tag17-other exporter -p '{"spec":{"template":{"metadata":
{"labels":{"team":"notes-users","access":"notes"}}}}}'
k rollout status -n tag17-other deployment exporter
k exec -n tag17-other deploy/exporter -- wget -qO- -T2 http://notes.tag17/versionExpected (abridged):
deployment.apps/exporter created
deployment.apps/exporter labeled
wget: download timed out
command terminated with exit code 1
deployment.apps/exporter patched
deployment "exporter" successfully rolled out
1.0
The labels belong in the Pod template
(spec.template.metadata.labels); the change triggers a
rollout. k edit deployment works just as well. Afterwards,
clean up as in the checkpoint.
Save your progress? Sign up for free
Exam Tip
- First
k get netpol -A -o wide, thenk describe netpol: read selectors and ports, then set labels – only change policies if the task allows it. - AND/OR trap:
namespaceSelectorandpodSelectorin the same list entry = AND, as two entries (-) = OR. Ports are container ports. - With egress blocks, keep DNS (UDP and TCP 53) in mind. Docs: “Network Policies” under Concepts → Services, Load Balancing, and Networking.
Enterprise Note
- Default deny per namespace belongs in the namespace template (Helm, Kustomize, GitOps), not in individual teams’ handiwork.
- Cluster-wide rules (always allow DNS, let monitoring in) are handled
by CNI-specific objects such as Calico
GlobalNetworkPolicy; the standard NetworkPolicy only knows namespaces. - NetworkPolicies work at L3/L4. HTTP paths, mTLS and identities are handled by a service mesh (Istio, Linkerd) or Cilium.
- Test policies like code: check a connection matrix in CI, analyze Calico flow logs for dropped packets.