Day 12: NetworkPolicies
Goal: You block Pod traffic by default, open specific paths on L3, L4 and L7, allow egress by DNS name and set cluster-wide blocks that no allow can override. · Duration: approx. 90 min · Prerequisite: Day 0
Briefing
- CKNE: Network Security and Policy – “Securing Traffic with Network Policies”.
- Without a policy, every Pod talks to every other Pod. The first policy that selects a Pod in a direction turns on default deny there; after that, allows add up.
- NetworkPolicy only knows L3/L4 and has no deny. CiliumNetworkPolicy (CNP) can do L7, DNS names, entities and deny; the CiliumClusterwideNetworkPolicy (CCNP) applies cluster-wide.
- Always prove it three ways:
curl/nc,hubble observe --verdict DROPPED, policy status in the agent (cilium-dbg endpoint list).
tag12-other/good (role=client) ──┐
tag12-other/bad ────────────────┤
tag12/client (app=client) ───────┴──> tag12/notes-api :8080 (Service :80)
└──> kube-dns · Nodes :10250 · router .123 · Internet (by name only)
Drill 1: The test field
Two namespaces, both with the label ckne=tag12 (for
Drill 8). The clients are curlimages/curl – the image ships
with curl, nc and nslookup:
mkdir -p ~/ckne/tag12 && cd ~/ckne/tag12
k create namespace tag12
k create namespace tag12-other
k label namespace tag12 tag12-other ckne=tag12
k -n tag12 create deployment notes-api \
--image=ghcr.io/thomaszachmann/notes-api:0.5.1 --replicas=2 --port=8080 \
$do > notes-api.yaml
k set env --local -f notes-api.yaml NOTES_API_KEY=lab-key-123 -o yaml \
> notes-api-env.yaml
k apply -f notes-api-env.yaml
k -n tag12 expose deployment notes-api --port=80 --target-port=8080
k -n tag12 run client --image=curlimages/curl:8.17.0 -l app=client,role=client \
--command -- sleep infinity
k -n tag12-other run good --image=curlimages/curl:8.17.0 -l role=client \
--command -- sleep infinity
k -n tag12-other run bad --image=curlimages/curl:8.17.0 \
--command -- sleep infinity
k -n tag12 rollout status deployment notes-api
k wait --for=condition=Ready pod --all -n tag12-other
k -n tag12 get cep
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/versionExpected (abridged):
deployment "notes-api" successfully rolled out
pod/bad condition met
pod/good condition met
NAME SECURITY IDENTITY ENDPOINT STATE IPV4
client 15951 ready 10.0.2.58
notes-api-665494b9df-dkctb 64913 ready 10.0.1.112
notes-api-665494b9df-nd4cg 64913 ready 10.0.2.198
0.5.1
0.5.1
0.5.1
All three clients reach notes-api. Policies work with these identities, not with IPs: both notes-api Pods carry the same labels and therefore the same identity.
Save your progress? Sign up for free
Drill 2: Default deny and the DNS exception
A policy with an empty podSelector and both
policyTypes, but without rules, blocks everything in the
namespace:
cat > default-deny.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: tag12
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
EOF
k apply -f default-deny.yaml
k -n tag12 exec client -- nslookup -timeout=2 notes-api
k -n tag12 exec client -- curl -s -m3 10.96.0.1:443
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12 --last 2Expected (abridged, timestamps omitted):
;; connection timed out; no servers could be reached
command terminated with exit code 1
command terminated with exit code 28
tag12/client:60376 (ID:15951) <> 172.16.0.126:6443 (kube-apiserver)
policy-verdict:none EGRESS DENIED (TCP Flags: SYN)
tag12/client:60376 (ID:15951) <> 172.16.0.126:6443 (kube-apiserver)
Policy denied DROPPED (TCP Flags: SYN)
Hubble doesn’t show the ClusterIP 10.96.0.1, but the
real destination: Cilium translates Service addresses at the socket
already, so the policy then sees the backend. Now allow DNS to CoreDNS –
UDP and TCP, because large responses come over TCP:
cat > allow-dns.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: tag12
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
EOF
k apply -f allow-dns.yaml
k -n tag12 exec client -- nslookup notes-api.tag12.svc.cluster.local
k -n tag12 exec client -- curl -s -m3 notes-api/version
hubble observe -P --port-forward-port 0 --namespace tag12 --to-port 53 --last 1Expected (abridged):
Name: notes-api.tag12.svc.cluster.local
Address: 10.109.31.111
command terminated with exit code 28
tag12/client:47319 (ID:15951) -> kube-system/coredns-559f6c778d-nz7t6:53 (ID:13185)
policy-verdict:L3-L4 EGRESS ALLOWED (UDP)
DNS works, notes-api doesn’t yet – egress from the client and ingress at notes-api are both closed. The agent on the client’s node shows the policy status:
NODE=$(k -n tag12 get pod client -o jsonpath='{.spec.nodeName}')
CP=$(k -n kube-system get pod -l k8s-app=cilium \
--field-selector spec.nodeName=$NODE -o name)
k -n kube-system exec $CP -c cilium-agent -- cilium-dbg endpoint list \
| grep -E 'ENDPOINT|app=client|app=notes-api'Expected (abridged):
ENDPOINT POLICY (ingress) POLICY (egress) IDENTITY LABELS ... IPv4
388 Enabled Enabled 15951 k8s:app=client 10.0.2.58
3952 Enabled Enabled 64913 k8s:app=notes-api 10.0.2.198
Enabled means: default deny applies to this direction.
You only see the notes-api Pod on this node – cilium-dbg
only knows local endpoints.
Save your progress? Sign up for free
Drill 3: Combining selectors – the AND/OR trap
The client may reach notes-api; notes-api should only accept
role=client Pods from tag12-other. First
attempt:
cat > client-egress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-egress
namespace: tag12
spec:
podSelector:
matchLabels:
role: client
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: notes-api
ports:
- protocol: TCP
port: 8080
EOF
cat > api-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-ingress
namespace: tag12
spec:
podSelector:
matchLabels:
app: notes-api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tag12-other
- podSelector:
matchLabels:
role: client
ports:
- protocol: TCP
port: 8080
EOF
k apply -f client-egress.yaml -f api-ingress.yaml
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/versionExpected:
networkpolicy.networking.k8s.io/client-egress created
networkpolicy.networking.k8s.io/api-ingress created
0.5.1
0.5.1
0.5.1
bad gets through as well. Two list entries (two
-) mean OR: “the whole namespace
tag12-other” or “role=client in the policy’s
own namespace”. For AND, both selectors belong in
one entry – in api-ingress.yaml, delete
the - before podSelector and indent the line
to match namespaceSelector:
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tag12-other
podSelector:
matchLabels:
role: client
ports:
- protocol: TCP
port: 8080k apply -f api-ingress.yaml
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/version
k -n tag12 describe networkpolicy api-ingress \
| sed -n '/Allowing ingress/,/Not affecting/p'
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12 \
--to-port 8080 --last 4Expected (abridged):
command terminated with exit code 28
0.5.1
command terminated with exit code 28
Allowing ingress traffic:
To Port: 8080/TCP
From:
NamespaceSelector: kubernetes.io/metadata.name=tag12-other
PodSelector: role=client
Not affecting egress traffic
tag12/client:59482 (ID:15951) <> tag12/notes-api-…-nd4cg:8080 (ID:64913)
Policy denied DROPPED (TCP Flags: SYN)
tag12-other/bad:36910 (ID:6458) <> tag12/notes-api-…-dkctb:8080 (ID:64913)
Policy denied DROPPED (TCP Flags: SYN)
bad is out – but now so is the namespace’s own
client: an entry with a namespaceSelector no
longer applies to the policy’s own namespace. describe
shows AND as two lines under one From:.
Add the own client as a second entry:
cat >> api-ingress.yaml <<'EOF'
- from:
- podSelector:
matchLabels:
role: client
ports:
- protocol: TCP
port: 8080
EOF
k apply -f api-ingress.yaml
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/versionExpected:
networkpolicy.networking.k8s.io/api-ingress configured
0.5.1
0.5.1
command terminated with exit code 28
Save your progress? Sign up for free
Drill 4: ipBlock with except, node IPs and port ranges
The client should reach the home network 172.16.0.0/24,
but not the router:
cat > client-lan.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-lan
namespace: tag12
spec:
podSelector:
matchLabels:
role: client
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 172.16.0.0/24
except:
- 172.16.0.123/32
EOF
k apply -f client-lan.yaml
k -n tag12 exec client -- nc -zv -w2 172.16.0.1 443
k -n tag12 exec client -- nc -zv -w2 172.16.0.123 22
k -n tag12 exec client -- nc -zv -w2 172.16.0.228 10250
k -n tag12 exec client -- nc -zv -w2 172.16.0.126 6443
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12 \
--since 20s | grep clientExpected (abridged, one line per destination):
172.16.0.1 (172.16.0.1:443) open
nc: 172.16.0.123 (172.16.0.123:22): Operation timed out
nc: 172.16.0.228 (172.16.0.228:10250): Operation timed out
nc: 172.16.0.126 (172.16.0.126:6443): Operation timed out
tag12/client:39305 (ID:15951) <> 172.16.0.123:22 (ID:16777272) Policy denied DROPPED
tag12/client:42831 (ID:15951) <> 172.16.0.228:10250 (remote-node) Policy denied DROPPED
tag12/client:45411 (ID:15951) <> 172.16.0.126:6443 (kube-apiserver) Policy denied …
Gateway .1 works, router is blocked by
except – as intended. But worker1 and
cp1 are closed too, even though they are inside the CIDR:
Cilium gives node IPs the identities host,
remote-node or kube-apiserver, and by default
ipBlock only matches world (the
ID:16777272 is a local CIDR identity). Calico and others
would let the nodes through. The fix in Cilium: toEntities
(Drill 5).
Port ranges come with endPort. To the
router, only TCP 20 through 23:
cat > client-router.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: client-router
namespace: tag12
spec:
podSelector:
matchLabels:
role: client
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 172.16.0.123/32
ports:
- protocol: TCP
port: 20
endPort: 23
EOF
k apply -f client-router.yaml
k -n tag12 exec client -- nc -zv -w2 172.16.0.123 22
k -n tag12 exec client -- curl -s -m3 http://172.16.0.123
k -n tag12-other exec bad -- curl -s -m3 http://172.16.0.123
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12 \
--to-port 80 --last 1Expected (abridged):
172.16.0.123 (172.16.0.123:22) open
command terminated with exit code 28
command terminated with exit code 7
tag12/client:44219 (ID:15951) <> 172.16.0.123:80 (ID:16777272) Policy denied DROPPED
Port 22 works despite the except in
client-lan: policies add up, and an except
only removes something from its own rule. Remember the curl exit codes:
7 = connection refused (the packet arrived, nothing is
listening), 28 = timeout (a policy dropped it). By the way,
--to-ip can’t be combined with --namespace,
but --to-port can.
Save your progress? Sign up for free
Drill 5: CiliumNetworkPolicy on L3/L4 with entities
From here on, only Cilium policies. Both kinds take effect together – for learning purposes, you remove the NetworkPolicies:
k -n tag12 delete networkpolicy --all
cat > cnp-client.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: client
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: client
egress:
- toEndpoints:
- matchLabels:
k8s:io.kubernetes.pod.namespace: kube-system
k8s:k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: ANY
- toEndpoints:
- matchLabels:
app: notes-api
toPorts:
- ports:
- port: "8080"
protocol: TCP
- toEntities:
- host
- remote-node
toPorts:
- ports:
- port: "10250"
protocol: TCP
EOF
cat > cnp-notes-api.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: notes-api
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: notes-api
ingress:
- fromEndpoints:
- matchLabels:
app: client
toPorts:
- ports:
- port: "8080"
protocol: TCP
EOF
k apply -f cnp-client.yaml -f cnp-notes-api.yaml
k -n tag12 get cnp
k -n tag12 exec client -- curl -s -m3 notes-api/version
for ip in 172.16.0.126 172.16.0.228 172.16.0.83; do
k -n tag12 exec client -- nc -zv -w2 $ip 10250
done
k -n tag12 exec client -- nc -zv -w2 172.16.0.126 6443
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/versionExpected:
NAME AGE VALID
client 5s True
notes-api 5s True
0.5.1
172.16.0.126 (172.16.0.126:10250) open
172.16.0.228 (172.16.0.228:10250) open
172.16.0.83 (172.16.0.83:10250) open
nc: 172.16.0.126 (172.16.0.126:6443): Operation timed out
command terminated with exit code 1
command terminated with exit code 28
Selectors without a namespace label refer to the policy’s namespace;
the label k8s:io.kubernetes.pod.namespace selects other
namespaces. host is the local node,
remote-node is every other one – that’s how you reach the
kubelet (10250) everywhere; not the API server (6443), because only port
10250 is allowed – for that, there would be the entity
kube-apiserver. VALID True only means: the
syntax is fine. What the agent makes of it:
k -n kube-system exec $CP -c cilium-agent -- cilium-dbg endpoint list \
| grep -E 'ENDPOINT|app=client|app=notes-api'
k -n kube-system exec $CP -c cilium-agent -- cilium-dbg policy get \
k8s:io.cilium.k8s.policy.name=notes-api k8s:io.cilium.k8s.policy.namespace=tag12
cilium config view | grep '^enable-policy 'Expected (abridged):
ENDPOINT POLICY (ingress) POLICY (egress) IDENTITY LABELS ... IPv4
388 Disabled Enabled 15951 k8s:app=client 10.0.2.58
3952 Enabled Disabled 64913 k8s:app=notes-api 10.0.2.198
[
{
"Subject": {
"matchLabels": {
"any:app": "notes-api",
"k8s:io.kubernetes.pod.namespace": "tag12"
}
},
"L3": [ { "matchLabels": { "any:app": "client", ... } } ],
"L4": [ { "ports": [ { "port": "8080", "protocol": "TCP" } ] } ],
...
"DefaultDeny": true,
"Ingress": true,
...
Revision: 279
enable-policy default
Unlike with the NetworkPolicies before, the client now only has
egress default deny: a CNP turns on default deny only for the direction
in which it has rules. policy get (marked deprecated in
1.20) shows how the agent translated the policy.
enable-policy default is the policy enforcement mode:
endpoints without a policy are open. always enforces
default deny for all endpoints – even without a policy –,
never turns policies off entirely (Helm value
policyEnforcementMode, don’t change it here).
Save your progress? Sign up for free
Drill 6: L7 rules for HTTP
notes-api should accept only read calls from the client. The
rules.http hang off the port:
cat > cnp-notes-api.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: notes-api
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: notes-api
ingress:
- fromEndpoints:
- matchLabels:
app: client
toPorts:
- ports:
- port: "8080"
protocol: TCP
rules:
http:
- method: GET
path: "/(version|healthz)"
- method: GET
path: "/notes"
EOF
k apply -f cnp-notes-api.yaml
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12 exec client -- curl -s -m3 notes-api/notes
k -n tag12 exec client -- curl -s -m3 -X POST -H 'X-API-Key: lab-key-123' \
-d '{"text":"L7"}' notes-api/notes
k -n tag12 exec client -- curl -s -o /dev/null -w '%{http_code}\n' notes-api/notes/42
hubble observe -P --port-forward-port 0 --namespace tag12 --verdict DROPPED \
--protocol http --last 2Expected (abridged):
0.5.1
[]
Access denied
403
tag12/client:49996 (ID:15951) -> tag12/notes-api-…-dkctb:8080 (ID:64913)
http-request DROPPED (HTTP/1.1 POST http://notes-api/notes)
tag12/client:57836 (ID:15951) -> tag12/notes-api-…-nd4cg:8080 (ID:64913)
http-request DROPPED (HTTP/1.1 GET http://notes-api/notes/42)
On L7 there’s no timeout, but a response: Envoy answers the forbidden
request with 403 Access denied. path is a
regular expression over the whole path, which is why
/notes/42 fails. hubble observe --type l7 also
shows the allowed requests, including status and latency.
Save your progress? Sign up for free
Drill 7: Egress by DNS name (toFQDNs)
toFQDNs needs the DNS proxy: only a DNS L7 rule lets
Cilium read the responses and learn the IPs of the names.
cat > cnp-client-fqdn.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: client-fqdn
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: client
egress:
- toEndpoints:
- matchLabels:
k8s:io.kubernetes.pod.namespace: kube-system
k8s:k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: ANY
rules:
dns:
- matchPattern: "*"
- toFQDNs:
- matchName: example.com
- matchPattern: "*.kubernetes.io"
toPorts:
- ports:
- port: "443"
protocol: TCP
EOF
k apply -f cnp-client-fqdn.yaml
k -n tag12 exec client -- curl -sI -m5 https://example.com | head -1
k -n tag12 exec client -- curl -sI -m5 https://blog.kubernetes.io | head -1
k -n tag12 exec client -- curl -sI -m5 https://kubernetes.io | head -1
k -n tag12 exec client -- curl -sI -m5 https://github.com | head -1
hubble observe -P --port-forward-port 0 --namespace tag12 --protocol dns --last 1
hubble observe -P --port-forward-port 0 --namespace tag12 --verdict DROPPED \
--to-port 443 --last 1
k -n kube-system exec $CP -c cilium-agent -- cilium-dbg fqdn cache listExpected (abridged):
HTTP/2 200
HTTP/2 302
command terminated with exit code 28
command terminated with exit code 28
tag12/client:37299 (ID:15951) <- kube-system/coredns-…-vwrtf:53 (ID:13185)
dns-response proxy FORWARDED (DNS Answer "140.82.121.3" TTL: 30 (Proxy github.com. A))
tag12/client:50710 (ID:15951) <> 140.82.121.3:443 (world) Policy denied DROPPED
Endpoint Source FQDN TTL ExpirationTime IPs
388 lookup example.com. 30 2026-11-18T23:28:08.131Z 2606:4700:…
388 lookup kubernetes.io. 30 2026-11-18T23:28:11.431Z 15.197.167.9…
388 lookup blog.kubernetes.io. 30 2026-11-18T23:28:10.634Z 34.107.204.2…
388 lookup github.com. 30 2026-11-18T23:28:17.090Z 140.82.121.3
388 lookup example.com. 5 2026-11-18T23:27:41.130Z 104.20.23.15…
*.kubernetes.io doesn’t match kubernetes.io
itself – for that you also need matchName. Name resolution
works for all names (matchPattern: "*" in the DNS part);
only the TCP connection gets blocked. Warning: policies act on IPs, not
on names:
k -n tag12 exec client -- curl -sI -m5 https://www.example.com | head -1
k -n tag12 exec client -- nslookup www.example.com | grep -c 104.20.23.154Expected:
HTTP/2 200
1
www.example.com sits at the same CDN on the same IPs as
example.com – and is therefore reachable as well. For cloud
services behind shared IPs, FQDN egress is therefore coarser than the
policy looks.
Save your progress? Sign up for free
Drill 8: Deny beats allow, CCNP cluster-wide
notes-api now accepts traffic from the whole cluster – just never
from bad. Allow and deny in one policy:
k -n tag12 delete cnp notes-api
cat > cnp-notes-api-deny.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: notes-api-deny
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: notes-api
ingress:
- fromEntities:
- cluster
toPorts:
- ports:
- port: "8080"
protocol: TCP
ingressDeny:
- fromEndpoints:
- matchLabels:
k8s:io.kubernetes.pod.namespace: tag12-other
run: bad
EOF
k apply -f cnp-notes-api-deny.yaml
k -n tag12 exec client -- curl -s -m3 notes-api/version
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/version
hubble observe -P --port-forward-port 0 --verdict DROPPED --from-pod tag12-other/bad \
--to-port 8080 --last 2Expected (abridged):
0.5.1
0.5.1
command terminated with exit code 28
tag12-other/bad:50026 (ID:6458) <> tag12/notes-api-…-dkctb:8080 (ID:64913)
policy-verdict:L3-Only INGRESS DENIED (TCP Flags: SYN)
tag12-other/bad:50026 (ID:6458) <> tag12/notes-api-…-dkctb:8080 (ID:64913)
Policy denied by denylist DROPPED (TCP Flags: SYN)
fromEntities: cluster allows bad too, yet
the deny still wins – no matter which policy it’s in. “denied by
denylist” distinguishes it in Hubble from the normal default deny. Now a
block for all Pods in namespaces with
ckne=tag12: no SSH to the router. In the lab,
cluster-wide objects get the prefix tag12-:
k -n tag12-other exec bad -- nc -zv -w2 172.16.0.123 22
cat > ccnp-no-ssh.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: tag12-no-router-ssh
spec:
endpointSelector:
matchLabels:
k8s:io.cilium.k8s.namespace.labels.ckne: tag12
egressDeny:
- toCIDR:
- 172.16.0.123/32
toPorts:
- ports:
- port: "22"
protocol: TCP
EOF
k apply -f ccnp-no-ssh.yaml
k get ccnp
k -n tag12-other exec bad -- nc -zv -w2 172.16.0.123 22
k -n tag12-other exec bad -- curl -sI -m5 https://example.com | head -1
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/versionExpected (abridged):
172.16.0.123 (172.16.0.123:22) open
ciliumclusterwidenetworkpolicy.cilium.io/tag12-no-router-ssh created
NAME VALID
tag12-no-router-ssh True
nc: 172.16.0.123 (172.16.0.123:22): Operation timed out
command terminated with exit code 28
command terminated with exit code 28
SSH is closed – but bad and good can’t get
out at all anymore. Even a pure deny policy turns on default deny for
its direction (k explain ccnp.spec.enableDefaultDeny). For
a pure guardrail, you turn that off:
cat >> ccnp-no-ssh.yaml <<'EOF'
enableDefaultDeny:
egress: false
ingress: false
EOF
k apply -f ccnp-no-ssh.yaml
k -n tag12-other exec bad -- nc -zv -w2 172.16.0.123 22
k -n tag12-other exec bad -- curl -sI -m5 https://example.com | head -1
k -n tag12-other exec good -- curl -s -m3 notes-api.tag12/version
hubble observe -P --port-forward-port 0 --namespace tag12-other --verdict DROPPED \
--to-port 22 --last 1Expected (abridged):
nc: 172.16.0.123 (172.16.0.123:22): Operation timed out
HTTP/2 200
0.5.1
tag12-other/bad:46209 (ID:6458) <> 172.16.0.123:22 (ID:16777277)
Policy denied by denylist DROPPED (TCP Flags: SYN)
And what about the AdminNetworkPolicy from the Kubernetes project
network-policy-api? Its successor is called ClusterNetworkPolicy
(v1alpha2, tiers Admin and Baseline). Cilium
supports it from 1.20, but only with a Helm switch and an additional CRD
– both are off in the lab:
k api-resources | grep -ciE 'adminnetworkpolic|clusternetworkpolic'
helm show values cilium/cilium --version 1.20.5 | grep -A2 '^k8sClusterNetworkPolicy'Expected:
0
k8sClusterNetworkPolicy:
# -- Enable support for K8s Cluster Network Policy
enabled: false
Save your progress? Sign up for free
Exercise: Speed Drills
In the namespace tag12-speed, web
(nginx:1.29, app=web) and probe
(curlimages/curl:8.17.0, app=probe,
sleep infinity) are running – create them first.
- Block all incoming traffic in the namespace
tag12-speed. Target time: 1 min - Allow
webon port 80 only from Pods withrole=clientfromtag12-other. Target time: 2 min probemay only use DNS andhttps://example.com. Target time: 3 min- Show the most recent dropped flows in the namespace
tag12-speed. Target time: 20 s
Solution
k create namespace tag12-speed
k -n tag12-speed run web --image=nginx:1.29 -l app=web
k -n tag12-speed run probe --image=curlimages/curl:8.17.0 -l app=probe \
--command -- sleep infinity
k -n tag12-speed apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-ingress
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-from-other
spec:
podSelector:
matchLabels:
app: web
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tag12-other
podSelector:
matchLabels:
role: client
ports:
- protocol: TCP
port: 80
---
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: probe-fqdn
spec:
endpointSelector:
matchLabels:
app: probe
egress:
- toEndpoints:
- matchLabels:
k8s:io.kubernetes.pod.namespace: kube-system
k8s:k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: ANY
rules:
dns:
- matchPattern: "*"
- toFQDNs:
- matchName: example.com
toPorts:
- ports:
- port: "443"
protocol: TCP
EOF
k wait --for=condition=Ready pod --all -n tag12-speed
WIP=$(k -n tag12-speed get pod web -o jsonpath='{.status.podIP}')
k -n tag12-other exec good -- curl -s -m3 -o /dev/null -w '%{http_code}\n' $WIP
k -n tag12-other exec bad -- curl -s -m3 -o /dev/null -w '%{http_code}\n' $WIP
k -n tag12-speed exec probe -- curl -sI -m5 https://example.com | head -1
k -n tag12-speed exec probe -- curl -sI -m5 https://github.com | head -1
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12-speed \
--last 2
k delete namespace tag12-speedExpected (abridged):
200
000
command terminated with exit code 28
HTTP/2 200
command terminated with exit code 28
tag12-speed/probe:38546 (ID:5179) <> 140.82.121.3:443 (world) policy-verdict:none
EGRESS DENIED (TCP Flags: SYN)
tag12-speed/probe:38546 (ID:5179) <> 140.82.121.3:443 (world) Policy denied DROPPED
Save your progress? Sign up for free
Checkpoint
State after Drill 8:
k -n tag12 get cnpExpected: client,
client-fqdn and notes-api-deny, all
VALID True.
k get ccnpExpected: tag12-no-router-ssh with
VALID True.
k -n tag12 get networkpolicyExpected:
No resources found in tag12 namespace.
k -n tag12-other exec bad -- curl -s -m3 notes-api.tag12/versionExpected:
command terminated with exit code 28 (deny beats
allow).
k -n tag12 exec client -- curl -sI -m5 https://github.comExpected:
command terminated with exit code 28 (not in
toFQDNs).
hubble observe -P --port-forward-port 0 --verdict DROPPED --namespace tag12 --last 5Expected: lines with Policy denied or
Policy denied by denylist.
Challenge
notes-api should accept only GET /notes from the client,
and POST /notes only with the header
X-API-Key: lab-key-123 – Cilium already rejects everything
else, not just the app.
Hint:
k explain cnp.spec.ingress.toPorts.rules.http shows the
field headers.
Solution
k -n tag12 delete cnp notes-api-deny
cat > cnp-notes-api-write.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: notes-api-write
namespace: tag12
spec:
endpointSelector:
matchLabels:
app: notes-api
ingress:
- fromEndpoints:
- matchLabels:
app: client
toPorts:
- ports:
- port: "8080"
protocol: TCP
rules:
http:
- method: GET
path: "/notes"
- method: POST
path: "/notes"
headers:
- "X-API-Key: lab-key-123"
EOF
k apply -f cnp-notes-api-write.yaml
k -n tag12 exec client -- curl -s -o /dev/null -w '%{http_code}\n' -X POST \
-d '{"text":"ohne"}' notes-api/notes
k -n tag12 exec client -- curl -s -o /dev/null -w '%{http_code}\n' -X POST \
-H 'X-API-Key: falsch' -d '{"text":"falsch"}' notes-api/notes
k -n tag12 exec client -- curl -s -o /dev/null -w '%{http_code}\n' -X POST \
-H 'X-API-Key: lab-key-123' -d '{"text":"richtig"}' notes-api/notes
k -n tag12 exec client -- curl -s -o /dev/null -w '%{http_code}\n' notes-api/versionExpected:
403
403
201
403
Without a key and with a wrong key, Envoy blocks (403 instead of the
app’s 401); /version is no longer on the list. The old
policy notes-api-deny had to go: its L4 allow for the whole
cluster would have bypassed the L7 rules.
Save your progress? Sign up for free
Cleanup
k delete ccnp tag12-no-router-ssh
k delete namespace tag12 tag12-other
k get ccnpExpected: No resources found –
otherwise the cluster-wide policy would keep hitting new Pods in
namespaces with ckne=tag12. This day didn’t change any
Cilium Helm values.
Exam Tip
- AND/OR:
namespaceSelectorandpodSelectorin the same list entry = AND, separate-= OR. Then double-check withk describe networkpolicy. - Always pair egress blocks with a DNS exception (UDP and TCP 53 to
k8s-app=kube-dns); ports in policies are container ports (8080), not Service ports (80). - Docs: kubernetes.io “Network Policies”, docs.cilium.io “Layer 3/4/7 Examples”, “DNS based”, “Deny Policies”, “Policy Enforcement Modes”.
Enterprise Note
- Default deny per namespace comes with every new namespace via a policy engine (Kyverno generate) or GitOps; exceptions as a PR with a justification.
- Cluster-wide guardrails (metadata IP, admin networks) as a CCNP with
deny and
enableDefaultDeny: false– or in the future as a ClusterNetworkPolicy, tierAdmin. - Before enforcing, evaluate
policyAuditModeor Hubble flows to see which connections are actually needed (Policy Editor,hubble observe -o json). - In Cilium,
ipBlockdoesn’t match node and Pod IPs (configurable viapolicyCIDRMatchMode); handle node access withtoEntities. - FQDN policies depend on the DNS proxy: plan for the CoreDNS path, TTLs and shared CDN IPs.