Day 9: NetworkPolicies
Goal: You block Pod traffic by default and open only the paths that are needed – inbound and outbound. · Duration: approx. 60 min · Prerequisite: Day 7
Briefing
CKA: “Define and enforce Network Policies” (part of the 20 %).
- Without a policy, every Pod may talk to every other Pod.
- As soon as a policy selects a Pod, only what a policy allows is
permitted for that Pod in the named direction (
Ingress,Egress). Policies are additive. - They are enforced by the CNI plugin – Calico here. Without a suitable CNI, NetworkPolicies have no effect.
other/outsider (role=frontend) --+
tag09/frontend (role=frontend) --+--> tag09/api :8080
tag09/intruder ------------------+
Drill 1: The test field
k create ns tag09
k create ns other
k config set-context --current --namespace=tag09
k create deployment api --image=registry.k8s.io/e2e-test-images/agnhost:2.53 \
-- /agnhost netexec --http-port=8080
k expose deployment api --port=80 --target-port=8080
k run frontend --image=busybox:1.37 -l role=frontend -- sleep 3600
k run intruder --image=busybox:1.37 -- sleep 3600
k run outsider -n other --image=busybox:1.37 -l role=frontend -- sleep 3600A short test per Pod – no helper script, just three commands:
k exec frontend -- wget -qO- -T2 http://api.tag09/hostname; echo
k exec intruder -- wget -qO- -T2 http://api.tag09/hostname; echo
k exec -n other outsider -- wget -qO- -T2 http://api.tag09/hostname; echoExpected: three times a Pod name like
api-86b79696fd-qwcrs – everything is open.
Save your progress? Sign up for free
Drill 2: Default deny
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes:
- Ingress
EOFRepeat the three test commands from Drill 1.
Expected: three times
wget: download timed out
command terminated with exit code 1
podSelector: {} selects all Pods in the namespace, and
there is no ingress rule – so nothing is allowed.
Save your progress? Sign up for free
Drill 3: Only the frontend may reach the API
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-from-frontend
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- port: 8080
EOFRepeat the test commands.
Expected:
| Pod | Result |
|---|---|
frontend |
Pod name |
intruder |
timed out |
outsider (other) |
timed out |
Two details: the port in the policy is the container
port 8080, not the Service port 80. And
podSelector in from only applies within the
policy’s own namespace – that is why
outsider stays out even though it carries the same
label.
Save your progress? Sign up for free
Drill 4: The AND/OR trap
Now outsider from other should get in.
Variant A – two entries in from:
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-from-other
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: other
- podSelector:
matchLabels:
role: frontend
EOF
k run intruder -n other --image=busybox:1.37 -- sleep 3600
k wait -n other --for=condition=Ready pod/intruder
k exec -n other outsider -- wget -qO- -T2 http://api.tag09/hostname; echo
k exec -n other intruder -- wget -qO- -T2 http://api.tag09/hostname; echoExpected: both get a Pod name. Two list entries mean
OR: any Pod from other or
any frontend Pod from tag09.
Variant B – one entry with both selectors (no - before
podSelector):
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-from-other
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: other
podSelector:
matchLabels:
role: frontend
EOF
k exec -n other outsider -- wget -qO- -T2 http://api.tag09/hostname; echo
k exec -n other intruder -- wget -qO- -T2 http://api.tag09/hostname; echoExpected: outsider gets a Pod name,
intruder times out. One entry with both selectors means
AND. A single dash makes the difference – the most
common trap in the exam.
Kubernetes sets the label kubernetes.io/metadata.name on
every namespace automatically.
Save your progress? Sign up for free
Drill 5: Block egress – and forget DNS
The frontend should only talk to the API:
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-egress
spec:
podSelector:
matchLabels:
role: frontend
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: api
ports:
- port: 8080
EOF
k exec frontend -- wget -qO- -T3 http://api.tag09/hostname; echoExpected:
wget: bad address 'api.tag09'
command terminated with exit code 1
Even though the path to the API is allowed: the Pod may no longer ask
the DNS server and cannot find api.tag09.
Save your progress? Sign up for free
Drill 6: Allow DNS
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-egress
spec:
podSelector:
matchLabels:
role: frontend
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: api
ports:
- port: 8080
- 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 exec frontend -- wget -qO- -T3 http://api.tag09/hostname; echo
k exec frontend -- wget -qO- -T3 http://example.comExpected:
api-86b79696fd-qwcrs
wget: download timed out
command terminated with exit code 1
The API works, the internet doesn’t – exactly as intended.
Save your progress? Sign up for free
Drill 7: Reading policies
k get networkpolicy
k describe networkpolicy frontend-egress | sed -n '/Spec/,$p'Expected (shortened):
NAME POD-SELECTOR AGE
api-from-frontend app=api 68s
api-from-other app=api 51s
default-deny <none> 75s
frontend-egress role=frontend 34s
Spec:
PodSelector: role=frontend
Not affecting ingress traffic
Allowing egress traffic:
To Port: 8080/TCP
To:
PodSelector: app=api
----------
To Port: 53/UDP
To Port: 53/TCP
To:
NamespaceSelector: kubernetes.io/metadata.name=kube-system
PodSelector: k8s-app=kube-dns
Policy Types: Egress
describe shows AND and OR unambiguously: entries under
one To: apply together, ---- separates
alternatives. Use it to read other people’s policies before you change
them.
Save your progress? Sign up for free
Exercise: Speed Drills
- Which policies select the Pod
frontend? Target time: 30 s - Create a policy
deny-allthat blocks all inbound and outbound traffic in the namespaceother. Target time: 60 s
Solution
k get netpol -o custom-columns=NAME:.metadata.name,SELECTOR:.spec.podSelector
cat <<'EOF' | k apply -n other -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
EOFExpected (excerpt):
NAME SELECTOR
api-from-frontend map[matchLabels:map[app:api]]
...
default-deny map[]
frontend-egress map[matchLabels:map[role:frontend]]
networkpolicy.networking.k8s.io/deny-all created
frontend is selected by default-deny (empty
selector = all) and frontend-egress.
Save your progress? Sign up for free
Checkpoint
k exec intruder -- wget -qO- -T2 http://api.tag09/hostnameExpected: wget: download timed out
k exec frontend -- wget -qO- -T2 http://api.tag09/hostname; echoExpected: a Pod name starting with
api-.
k exec -n other outsider -- wget -qO- -T2 http://api.tag09/hostnameExpected: wget: bad address 'api.tag09'
– deny-all now also blocks egress in other, so
even the DNS query fails.
Challenge
In the namespace tag09, all outbound traffic should be
blocked for all Pods – except DNS. Existing exceptions
(frontend to API) should keep working.
Hint: An egress rule with only ports
and no to applies to every destination.
Solution
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-egress-allow-dns
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- ports:
- port: 53
protocol: UDP
- port: 53
protocol: TCP
EOF
k exec intruder -- nslookup api.tag09.svc.cluster.local
k exec intruder -- wget -qO- -T3 http://example.com
k exec frontend -- wget -qO- -T3 http://api.tag09/hostname; echoExpected (shortened):
Name: api.tag09.svc.cluster.local
Address: 10.106.144.111
wget: download timed out
command terminated with exit code 1
api-86b79696fd-qwcrs
Policies are additive: frontend-egress additionally
allows the frontend the path to the API.
Clean up:
k config set-context --current --namespace=default
k delete ns tag09 otherSave your progress? Sign up for free
Exam Tip
- Open the docs page “Network Policies” and copy the example – it shows AND and OR side by side.
- After every policy, test with
k exec … -- wget -T2ornc -zv -w2: once allowed, once denied. - Egress tasks: remember DNS (UDP and TCP 53).
Enterprise Note
- A default deny per namespace belongs in every tenant template.
- Calico and Cilium additionally offer cluster-wide policies
(
GlobalNetworkPolicy,CiliumClusterwideNetworkPolicy) and L7 rules; Kubernetes itself is currently introducingClusterNetworkPolicyfor this. - Make network flows visible (Calico Whisker, Cilium Hubble) before you block them.
- More depth: “Kyverno & NetworkPolicies hands-on”.