Kubernetes network policies, explained by default-deny
Start each namespace from default deny and add one allow per conversation. How selectors combine (one dash turns AND into OR), the DNS egress everyone forgets, and why your CNI decides whether any of it happens.
ON THIS PAGE
Write Kubernetes network policies backwards: first one policy per namespace that selects every Pod and allows nothing, then one allow for each conversation your application really has. A NetworkPolicy cannot say "deny", so default deny is the only way to get a "no" out of the API at all. Three things then decide whether your policies do what you meant: how the selectors in a rule combine, the DNS lookups every Pod makes, and whether your network plugin enforces any of it.
Based on the article from Gazeek, our sister site for hands-on CKA and CKAD labs: Kubernetes NetworkPolicies explained. Every policy in full, the DNS egress rule and the commands to test them are there.
Why start from deny#
Until a policy selects a Pod, that Pod accepts and makes any connection. Once a policy selects it for a direction, only what some policy allows gets through in that direction. Policies only add allowances, and when several select the same Pod their allowances are summed; order never matters. So the model has no deny rule: you isolate a Pod and then decline to allow things.
Starting from deny changes what your policy files are. Instead of a list of things you remembered to block, they become an inventory of the conversations your application has. A connection you forgot fails in staging with a timeout, rather than staying open in production where nobody looks.
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressAn empty podSelector picks every Pod in the namespace, and no rules means nothing is allowed either way. Name both policy types explicitly: if you leave policyTypes out, Kubernetes infers Egress only when the policy has egress rules, and a deny with no rules has none.
One thing NetworkPolicy will not do for you: there is no cluster-wide default. Every new namespace starts wide open, so the default deny belongs in whatever creates namespaces (your Helm chart, your namespace template, your GitOps bootstrap), not in a runbook.
Allows, and the dash that turns AND into OR#
Each allow names the Pods it protects with podSelector and the peers it admits under from or to. Peers in another namespace need a namespaceSelector; the label kubernetes.io/metadata.name, set by the control plane on every namespace, lets you select one by name. Here is the version that means "Prometheus Pods, and only those in the monitoring namespace":
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheusBoth selectors sit in one list item, so a peer must match both. Put a dash before podSelector and they become two items, either of which is enough: now every Pod in monitoring gets in, plus any Pod labelled app: prometheus in your own namespace. The YAML still looks reasonable and still applies cleanly. When you review a policy, read kubectl describe networkpolicy, which prints how Kubernetes understood it, rather than the file.
Default deny in two namespaces adds a second trap. A connection from one to the other needs an egress allow on the caller and an ingress allow on the receiver. That is two policies, in two namespaces, often owned by two teams, and either one missing looks the same from outside.
DNS, and the probes that survive#
Deny egress and every lookup of a Service name fails, so the application looks broken even when the connection you allowed is right there in the policy. The first egress allow in any namespace is to the cluster DNS Pods in kube-system on port 53, over UDP and TCP, because DNS falls back to TCP for large answers. Check the DNS Pods' labels on your own cluster before you select them.
Health checks are the opposite surprise: they keep working. Traffic from a Pod's own node is always allowed, so the kubelet's liveness and readiness probes pass through a default deny untouched. Istio's ambient mode can change that: it rewrites probe traffic to a link-local source address, which some network plugins then block, and it tunnels Pod traffic over port 15008, which a port-restricted allow will not admit.
The CNI decides whether any of this happens#
The API server stores any valid NetworkPolicy. Enforcing it is the network plugin's job, and a plugin without NetworkPolicy support accepts the objects and ignores them, so every connection still works and nothing warns you. Calico and Cilium enforce them; check what your cluster runs before you debug a selector.
NetworkPolicy has no logging of its own, and a blocked connection usually just times out. So test each policy from two throwaway Pods, one labelled as an allowed peer and one not, and expect one success and one timeout. Both succeeding points at the plugin or the labels; both failing usually means DNS. Gazeek's CKAD network policy labs give you a cluster whose plugin enforces them, which is the fastest way to build the instinct.
The diagram#
A namespace under default deny, with the three allows that make it work: frontend to web on its port, DNS out to kube-system, and Prometheus in from the monitoring namespace. Open it in archhy and press Make a copy to draw your own namespace's conversations.
Go deeper#
Gazeek's full article has every policy as complete YAML, the DNS egress rule with its selectors, and the test commands, with what each result tells you.
