Day 14: NetworkPolicies with Cilium and Hubble
Goal: You turn on Hubble and follow every packet in the cluster, lock down the namespace with default deny, open it up again selectively, use
hubble observe --verdict DROPPEDto find out which rule drops a packet – and get to know the three most common mistakes: wrong port, forgotten DNS, wrong namespace selector · Duration: approx. 90 min · Prerequisite: Day 12 (clusterndbgwith Cilium,webandwerkzeug)
Briefing
- Without a NetworkPolicy, in Kubernetes every pod may talk to
every other pod. As soon as a policy selects a pod for a
direction (
Ingress= incoming,Egress= outgoing), the rule for that direction is: only what some policy allows. Policies never block anything additionally, they only add up permissions. - Policies are not enforced by Kubernetes but by the network plugin – here by Cilium, via eBPF directly at the pod. Cilium groups pods with the same labels into an identity (a number) and checks identity against identity, not IP against IP.
- A dropped connection looks like packet loss to the application:
timeout, no “Connection refused”. That is why
curl -vdoes not help. Hubble shows every decision Cilium makes with sender, receiver, port and verdict (FORWARDED,DROPPED). - The ports in a policy are the ports at the pod, not
those of the service: a service
80 → 8080needs8080in the policy (or the name of the container port).
| Hubble shows | means |
|---|---|
to-endpoint FORWARDED |
packet arrived at the destination pod |
policy-verdict:L3-L4 INGRESS ALLOWED |
first packet of a connection, allowed by a policy |
policy-verdict:none INGRESS DENIED |
no policy allows it, direction incoming |
Policy denied DROPPED |
the packet was dropped because of that |
(world) |
sender or destination is outside the cluster |
Drill 1: Turn on Hubble
Cilium already collects the events on every node. What is missing is
Hubble Relay (collects the events from all nodes) and
the hubble command line.
# on netz
cd ~/ndbg/k8s
# install Hubble Relay (changes the Helm installation of Cilium)
cilium hubble enable
# wait until Relay runs (up to 5 min); strip color codes, only the Relay line, only the last one
cilium status --wait --wait-duration 5m 2>/dev/null | sed 's/\x1b\[[0-9;]*[A-Za-z]//g' | grep "Hubble Relay" | tail -1
kubectl -n kube-system get deployment hubble-relayExpected:
\__/¯¯\__/ Hubble Relay: OK
NAME READY UP-TO-DATE AVAILABLE AGE
hubble-relay 1/1 1 1 16s
While waiting, cilium status reports
1 errors a few times – that is Relay starting up. Only the
last line counts.
Now the Hubble CLI, in the version the project marks as stable, with checksum:
# on netz
cd /tmp
# stable version from the Hubble repository (one line, e.g. v1.20.4)
HUBBLE_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/hubble/main/stable.txt)
# architecture of the system (amd64 or arm64)
ARCH=$(dpkg --print-architecture)
# download archive and checksum file:
# -L --fail follow redirects, abort on HTTP error
# --remote-name-all save every URL under its own name
# {,.sha256sum} the shell turns this into two URLs: .tar.gz and .tar.gz.sha256sum
curl -sL --fail --remote-name-all https://github.com/cilium/hubble/releases/download/$HUBBLE_VERSION/hubble-linux-$ARCH.tar.gz{,.sha256sum}
# compare the checksum
sha256sum --check hubble-linux-$ARCH.tar.gz.sha256sum
# unpack only the file "hubble" from the archive to /usr/local/bin (x = extract, z = gzip, f = file, -C = target directory)
sudo tar xzf hubble-linux-$ARCH.tar.gz -C /usr/local/bin hubble
rm hubble-linux-$ARCH.tar.gz*
hubble version
cd ~/ndbg/k8sExpected:
hubble-linux-amd64.tar.gz: OK
hubble v1.20.4@HEAD-3e268bb compiled with go1.27.3 on linux/amd64
Relay is a service in the cluster; netz cannot reach it
directly. cilium hubble port-forward forwards
localhost:4245 there – in the background, as long as this
terminal stays open:
# on netz
# forwarding in the background (&), discard output
cilium hubble port-forward > /dev/null 2>&1 &
sleep 3
# does Hubble see both nodes?
hubble status
# one request, then everything the tools pod sent in the last 10 s:
# --from-pod default/werkzeug only packets with this pod as sender (namespace/name)
# --since 10s only events from the last 10 seconds
kubectl exec werkzeug -- curl -s -m3 http://web/hostname; echo
hubble observe --from-pod default/werkzeug --since 10sExpected (times, ports, pod names and IDs differ):
Healthcheck (via localhost:4245): Ok
Current/Max Flows: 8,190/8,190 (100.00%)
Flows/s: 7.35
Connected Nodes: 2/2
web-69856b5777-p9vjn
Nov 27 08:09:29.884: default/werkzeug (ID:1853) <> kube-system/kube-dns:53 (world) pre-xlate-fwd TRACED (UDP)
Nov 27 08:09:29.884: default/werkzeug:50086 (ID:1853) -> kube-system/coredns-559f6c778d-jhrg6:53 (ID:52881) to-endpoint FORWARDED (UDP)
Nov 27 08:09:29.886: default/werkzeug:50086 (ID:1853) -> kube-system/coredns-559f6c778d-jhrg6:53 (ID:52881) to-overlay FORWARDED (UDP)
Nov 27 08:09:29.909: default/werkzeug:35880 (ID:1853) -> default/web-69856b5777-p9vjn:8080 (ID:8424) to-endpoint FORWARDED (TCP Flags: SYN)
Nov 27 08:09:29.909: default/werkzeug:35880 (ID:1853) -> default/web-69856b5777-p9vjn:8080 (ID:8424) to-endpoint FORWARDED (TCP Flags: ACK)
Nov 27 08:09:29.910: default/werkzeug:35880 (ID:1853) -> default/web-69856b5777-p9vjn:8080 (ID:8424) to-endpoint FORWARDED (TCP Flags: ACK, PSH)
Nov 27 08:09:29.914: default/werkzeug:35880 (ID:1853) -> default/web-69856b5777-p9vjn:8080 (ID:8424) to-endpoint FORWARDED (TCP Flags: ACK, FIN)
The whole request in seven lines: first the DNS query to
kube-dns – before translation (pre-xlate)
still as a service without an identity, hence (world) –,
then to a CoreDNS pod, namely via the overlay to the control plane
(to-overlay, the topic of Day 15). After that the TCP
connection directly to the pod on port 8080: SYN, ACK,
data, FIN. Behind every name is its identity (ID:1853 = all
pods with the label run=werkzeug in the namespace
default).
Save your progress? Sign up for free
Drill 2: Default deny
The usual first policy in a namespace: all pods are selected for incoming traffic, and there is not a single permission.
# on netz
cat > deny-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: deny-ingress}
spec:
podSelector: {}
policyTypes: [Ingress]
EOF
kubectl apply -f deny-ingress.yaml
kubectl get networkpolicy
# -sS = silent, but show errors; -m3 = give up after 3 s
kubectl exec werkzeug -- curl -sS -m3 http://web/hostname
echo "exit=$?"
# only dropped packets from the last 10 s (--verdict DROPPED)
hubble observe --verdict DROPPED --since 10spodSelector: {}– an empty selector selects all pods of the namespace.policyTypes: [Ingress]– the policy applies to incoming traffic; since noingress:rules follow, nothing is allowed.
Expected:
networkpolicy.networking.k8s.io/deny-ingress created
NAME POD-SELECTOR AGE
deny-ingress <none> 1s
curl: (28) Connection timed out after 3002 milliseconds
command terminated with exit code 28
exit=28
Nov 27 08:09:37.721: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:09:37.721: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
Nov 27 08:09:38.768: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:09:38.768: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
Nov 27 08:09:39.792: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:09:39.792: default/werkzeug:60208 (ID:1853) <> default/web-69856b5777-p9vjn:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
curl only sees a timeout (exit code 28). Hubble says
exactly what happens: three SYN packets – the first and two retries one
second apart – were dropped incoming at the
web pod, because no policy allows them
(policy-verdict:none). The direction is right in the
verdict: INGRESS means you look for the gap at the
receiver.
The block applies to all senders, including from outside via the NodePort from Day 12:
# on netz
curl -sS -m3 http://172.18.0.3:30080/hostname; echo " exit=$?"Expected:
curl: (28) Connection timed out after 3002 milliseconds
exit=28
Save your progress? Sign up for free
Drill 3: Open selectively
web should be reachable again – but only from the tools
pod.
# on netz
cat > allow-werkzeug.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-werkzeug}
spec:
podSelector: {matchLabels: {app: web}}
policyTypes: [Ingress]
ingress:
- from:
- podSelector: {matchLabels: {run: werkzeug}}
ports:
- {protocol: TCP, port: http}
EOF
kubectl apply -f allow-werkzeug.yaml
kubectl exec werkzeug -- curl -sS -m3 http://web/hostname; echo
curl -sS -m2 http://172.18.0.3:30080/hostname; echo " exit=$?"
# only connection setups to web (--to-pod, --tcp-flags SYN)
hubble observe --to-pod default/web --tcp-flags SYN --since 8spodSelector: {matchLabels: {app: web}}– which pods the policy applies to (the receivers).from: - podSelector: {matchLabels: {run: werkzeug}}– who may send: pods with this label in the same namespace.kubectl rungave the tools pod the labelrun=werkzeug.port: http– the name of the container port fromweb.yaml(containerPort: 8080). That stays correct even if someone changes the port in the container.
Expected (pod names and times differ):
networkpolicy.networking.k8s.io/allow-werkzeug created
web-69856b5777-2np6w
curl: (28) Connection timed out after 2002 milliseconds
exit=28
Nov 27 08:10:17.207: default/werkzeug:48132 (ID:1853) -> default/web-69856b5777-2np6w:8080 (ID:8424) policy-verdict:L3-L4 INGRESS ALLOWED (TCP Flags: SYN)
Nov 27 08:10:17.207: default/werkzeug:48132 (ID:1853) -> default/web-69856b5777-2np6w:8080 (ID:8424) to-endpoint FORWARDED (TCP Flags: SYN)
Nov 27 08:10:17.297: 172.18.0.1:57568 (world) <> default/web-69856b5777-2np6w:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:10:17.297: 172.18.0.1:57568 (world) <> default/web-69856b5777-2np6w:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
Nov 27 08:10:18.320: 172.18.0.1:57568 (world) <> default/web-69856b5777-2np6w:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:10:18.320: 172.18.0.1:57568 (world) <> default/web-69856b5777-2np6w:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
Two policies now select web: deny-ingress
(allows nothing) and allow-werkzeug (allows the tools pod).
Together the sum applies – the tools pod gets through
(L3-L4 INGRESS ALLOWED), the request from netz
via the NodePort does not. It appears as world with the
address 172.18.0.1, that is netz in the kind
network.
Save your progress? Sign up for free
Drill 4: Block outgoing – and forget DNS
Now the other direction: the tools pod may only reach
web.
# on netz
cat > werkzeug-egress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: werkzeug-egress}
spec:
podSelector: {matchLabels: {run: werkzeug}}
policyTypes: [Egress]
egress:
- to:
- podSelector: {matchLabels: {app: web}}
ports:
- {protocol: TCP, port: http}
EOF
kubectl apply -f werkzeug-egress.yaml
# by name and directly by ClusterIP (the IP from Day 12, for you: kubectl get svc web)
kubectl exec werkzeug -- curl -sS -m5 http://web/hostname
kubectl exec werkzeug -- curl -sS -m5 http://10.96.157.184/hostname; echo
# the last two dropped packets of the tools pod (--last 2)
hubble observe --from-pod default/werkzeug --verdict DROPPED --since 15s --last 2Expected (the ClusterIP differs):
networkpolicy.networking.k8s.io/werkzeug-egress created
curl: (28) Resolving timed out after 5000 milliseconds
command terminated with exit code 28
web-69856b5777-p9vjn
Nov 27 08:10:40.100: default/werkzeug:40663 (ID:1853) <> kube-system/coredns-559f6c778d-jhrg6:53 (ID:52881) policy-verdict:none EGRESS DENIED (UDP)
Nov 27 08:10:40.100: default/werkzeug:40663 (ID:1853) <> kube-system/coredns-559f6c778d-jhrg6:53 (ID:52881) Policy denied DROPPED (UDP)
The classic mistake after the first egress policy: “Resolving
timed out”. The connection to web would be allowed
– by IP it works, after all –, but the DNS query to CoreDNS no longer
gets out (EGRESS DENIED (UDP), port 53). To the application
this looks like a DNS problem, to Day 13 like a healthy CoreDNS. Only
Hubble shows that the policy of the sender is to
blame.
The permission for DNS, appended to the file:
# on netz
cat >> werkzeug-egress.yaml <<'EOF'
- 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
kubectl apply -f werkzeug-egress.yaml
kubectl exec werkzeug -- curl -sS -m5 http://web/hostname; echo
# and the internet? (discard output, show only errors)
kubectl exec werkzeug -- curl -sS -m3 -o /dev/null http://example.com
hubble observe --from-pod default/werkzeug --verdict DROPPED --since 5s | grep world | tail -1namespaceSelectorandpodSelectorin the same list entry (only one-in front) means: pods withk8s-app=kube-dnsin the namespacekube-system. Kubernetes sets the labelkubernetes.io/metadata.nameon every namespace itself.- UDP and TCP 53: large answers (over 1232 bytes, see
udp 70 false 1232in the CoreDNS log from Day 13) come via TCP.
Expected (the address of example.com differs):
networkpolicy.networking.k8s.io/werkzeug-egress configured
web-69856b5777-p9vjn
curl: (28) Connection timed out after 3000 milliseconds
command terminated with exit code 28
Nov 27 08:14:46.096: default/werkzeug:46624 (ID:1853) <> 172.66.147.243:80 (world) Policy denied DROPPED (TCP Flags: SYN)
Names work again, the internet stays blocked – the name
example.com is resolved, but Cilium drops the outgoing
connection to 172.66.147.243 (world).
Save your progress? Sign up for free
Exercise
A colleague has written a permission to web for a batch
pod:
# on netz
# pod with the label app=batch (--labels) that only sleeps
kubectl run batch --image=nicolaka/netshoot:v0.17 --labels=app=batch -- sleep infinity
kubectl wait --for=condition=Ready pod/batch --timeout=120s
cat > allow-batch.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-batch}
spec:
podSelector: {matchLabels: {app: web}}
policyTypes: [Ingress]
ingress:
- from:
- podSelector: {matchLabels: {app: batch}}
ports:
- {protocol: TCP, port: 80}
EOF
kubectl apply -f allow-batch.yaml
kubectl exec batch -- curl -sS -m3 http://web/hostname“But the service listens on port 80!” Still: timeout. Find the mistake and fix it.
Solution
# on netz
kubectl exec batch -- curl -sS -m3 http://web/hostname
hubble observe --from-pod default/batch --to-pod default/web --tcp-flags SYN --since 5s --last 2
# port of the service and where it forwards to
kubectl get svc web -o jsonpath='{.spec.ports[0].port} -> {.spec.ports[0].targetPort}'; echo
# what the policy allows
kubectl get networkpolicy allow-batch -o jsonpath='{.spec.ingress[0].ports}'; echoExpected:
curl: (28) Connection timed out after 3001 milliseconds
command terminated with exit code 28
Nov 27 08:11:01.393: default/batch:41158 (ID:57600) <> default/web-69856b5777-2np6w:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 27 08:11:01.393: default/batch:41158 (ID:57600) <> default/web-69856b5777-2np6w:8080 (ID:8424) Policy denied DROPPED (TCP Flags: SYN)
80 -> http
[{"port":80,"protocol":"TCP"}]
Hubble shows the packet at the pod on 8080. The
translation 80 → 8080 already happened at the sender
(socket LB, Day 12); at the receiver, where the policy applies, port 80
does not exist at all. The policy allows a port on which no packet ever
arrives. Fix: enter the pod’s port, ideally by its name.
# on netz
sed -i 's/port: 80}/port: http}/' allow-batch.yaml
kubectl apply -f allow-batch.yaml
kubectl exec batch -- curl -sS -m3 http://web/hostname; echoExpected:
networkpolicy.networking.k8s.io/allow-batch configured
web-69856b5777-2np6w
This applies to every NetworkPolicy implementation, not just Cilium: policies see pods, not services.
Save your progress? Sign up for free
Challenge
Monitoring runs in its own namespace and should be allowed to query
web:
# on netz
kubectl create namespace monitoring
kubectl -n monitoring run prometheus --image=nicolaka/netshoot:v0.17 --labels=app=prometheus -- sleep infinity
kubectl -n monitoring wait --for=condition=Ready pod/prometheus --timeout=120s
cat > allow-monitoring.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-monitoring}
spec:
podSelector: {matchLabels: {app: web}}
policyTypes: [Ingress]
ingress:
- from:
- namespaceSelector: {matchLabels: {name: monitoring}}
podSelector: {matchLabels: {app: prometheus}}
ports:
- {protocol: TCP, port: http}
EOF
kubectl apply -f allow-monitoring.yaml
kubectl -n monitoring exec prometheus -- curl -sS -m3 http://web.default/hostnameTimeout. This time the port is right (http). Find out
which labels Cilium sees at the sender, and fix the policy.
Hint:
cilium-dbg identity get <ID> in the Cilium pod shows
all labels of an identity.
Solution
# on netz
# the dropped packet – the sender's identity is in the parentheses (ID:…)
hubble observe --from-namespace monitoring --verdict DROPPED --since 10s --last 1
# the labels of this identity (insert the number from the line above):
# -n kube-system exec ds/cilium in a pod of the DaemonSet cilium
# -c cilium-agent in the container cilium-agent
kubectl -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg identity get 26223
kubectl get namespace monitoring --show-labelsExpected (the identity differs):
Nov 27 08:11:20.811: monitoring/prometheus:54258 (ID:26223) <> default/web-69856b5777-2np6w:8080 (ID:8424) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
ID LABELS
26223 k8s:app=prometheus
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name=monitoring
k8s:io.cilium.k8s.policy.cluster=kind-ndbg
k8s:io.cilium.k8s.policy.serviceaccount=default
k8s:io.kubernetes.pod.namespace=monitoring
NAME STATUS AGE LABELS
monitoring Active 10s kubernetes.io/metadata.name=monitoring
The policy looks for a namespace with the label
name=monitoring. That label does not exist – the namespace
is called monitoring, but carries only
kubernetes.io/metadata.name=monitoring. Cilium copies the
namespace labels into the pod’s identity
(io.cilium.k8s.namespace.labels…), which is why you see in
one place everything a policy can match against. Fix with
one command plus apply:
# on netz
sed -i 's|{name: monitoring}|{kubernetes.io/metadata.name: monitoring}|' allow-monitoring.yaml
kubectl apply -f allow-monitoring.yaml
kubectl -n monitoring exec prometheus -- curl -sS -m3 http://web.default/hostname; echoExpected:
networkpolicy.networking.k8s.io/allow-monitoring configured
web-69856b5777-2np6w
The alternative – setting the label name=monitoring on
the namespace – works too, but depends on a label someone can forget.
Kubernetes always sets kubernetes.io/metadata.name.
Save your progress? Sign up for free
Checkpoint
# on netz
# five policies in the namespace default
kubectl get networkpolicy -o name
# three allowed senders (only the first three characters of the answer)
kubectl exec werkzeug -- curl -s -m3 http://web/hostname | cut -c1-3
kubectl exec batch -- curl -s -m3 http://web/hostname | cut -c1-3
kubectl -n monitoring exec prometheus -- curl -s -m3 http://web.default/hostname | cut -c1-3
# still blocked from outside
curl -s -m3 http://172.18.0.3:30080/hostname; echo "exit=$?"
# Hubble sees both nodes
hubble status | grep ConnectedExpected:
networkpolicy.networking.k8s.io/allow-batch
networkpolicy.networking.k8s.io/allow-monitoring
networkpolicy.networking.k8s.io/allow-werkzeug
networkpolicy.networking.k8s.io/deny-ingress
networkpolicy.networking.k8s.io/werkzeug-egress
web
web
web
exit=28
Connected Nodes: 2/2
Clean up
Day 15 needs free traffic again. Hubble stays installed.
# on netz
kubectl delete networkpolicy --all
kubectl delete pod batch
kubectl delete namespace monitoring
# NodePort open again
curl -s -m3 http://172.18.0.3:30080/hostname | cut -c1-3Expected:
networkpolicy.networking.k8s.io "allow-batch" deleted from default namespace
networkpolicy.networking.k8s.io "allow-monitoring" deleted from default namespace
networkpolicy.networking.k8s.io "allow-werkzeug" deleted from default namespace
networkpolicy.networking.k8s.io "deny-ingress" deleted from default namespace
networkpolicy.networking.k8s.io "werkzeug-egress" deleted from default namespace
pod "batch" deleted from default namespace
namespace "monitoring" deleted
web
The cilium hubble port-forward forwarding ends when you
close the terminal. In a new terminal you restart it with the same
command as in Drill 1.
Interview note
“Since the NetworkPolicy rollout, the service no longer works.” First
Hubble (or the equivalent of the plugin in use):
hubble observe --verdict DROPPED --to-pod <target> or
--from-pod <source>. The line tells you direction
(INGRESS/EGRESS), port and identities. Then
check the three classics: service port instead of pod port, DNS
forgotten for egress, namespace selector on a label that doesn’t exist.
Bonus points: the difference between
- namespaceSelector: … podSelector: … (one entry = AND) and
two entries each with its own - (OR – then every pod in the
namespace and every matching pod in the own namespace
may connect).
Enterprise note
Default deny per namespace is mandatory in many companies (BSI
IT-Grundschutz, PCI DSS). So that this doesn’t lead to the same timeouts
in every team: automatically put a standard policy for DNS egress into
every new namespace (e.g. via Kyverno), roll out policies from Git and
with tests, and feed Hubble metrics on dropped packets
(hubble_drop_total by reason) into monitoring. A sudden
rise in POLICY_DENIED after a deployment is almost always a
forgotten permission, not an attack.