Kubernetes NetworkPolicies: Zero-Trust Networking Between Pods
Restrict pod-to-pod traffic with NetworkPolicies: default deny, allowing specific ingress and egress, namespace selectors, DNS egress and testing.
Published December 2, 2026
By default, every pod in a Kubernetes cluster can talk to every other pod. That flat network means a single compromised container can reach your databases, internal APIs and metadata services. NetworkPolicies let you declare which traffic is allowed, turning the cluster network into an allow-list.
The concept
A NetworkPolicy selects pods by label and defines allowed ingress (incoming) and/or egress (outgoing) traffic by pod selectors, namespace selectors, IP blocks and ports. Policies are additive: once any policy selects a pod for a direction, only traffic explicitly allowed by some policy is permitted in that direction.
NetworkPolicies are enforced by the CNI plugin, not by Kubernetes itself. Calico, Cilium and most managed-cloud network plugins enforce them; a CNI that does not support them silently ignores the objects. The standard approach is a default-deny policy per namespace, followed by narrow allow rules, remembering that egress deny also blocks DNS unless you allow it.
Allow-list networking
Rollout plan
- Confirm your CNI enforces NetworkPolicy
- Apply default-deny ingress (and egress) per namespace
- Allow DNS egress to kube-dns
- Allow specific app-to-app paths by labels and ports
- Test with temporary pods; monitor drops
Who can be allowed
| Selector | Matches | Example |
|---|---|---|
| podSelector | Pods in the same namespace by label | app: frontend |
| namespaceSelector | All pods in matching namespaces | kubernetes.io/metadata.name: monitoring |
| namespace + pod selector | Specific pods in specific namespaces | prometheus pods in monitoring |
| ipBlock | CIDR ranges (mostly external) | 10.0.0.0/8 except 10.0.5.0/24 |
Practical examples
Default deny all traffic in a namespace
An empty podSelector selects every pod.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny, namespace: shop }
spec:
podSelector: {}
policyTypes: [Ingress, Egress]Allow frontend → api and DNS egress
Two narrow rules.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: api-from-frontend, namespace: shop }
spec:
podSelector: { matchLabels: { app: api } }
ingress:
- from: [{ podSelector: { matchLabels: { app: frontend } } }]
ports: [{ port: 8080, protocol: TCP }]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: allow-dns, namespace: shop }
spec:
podSelector: {}
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 }]Test a path
Run a throwaway pod with the right labels.
kubectl run probe --rm -it -n shop --labels app=frontend --image=busybox -- wget -qO- -T 3 http://api:8080/health
kubectl run probe --rm -it -n shop --labels app=other --image=busybox -- wget -qO- -T 3 http://api:8080/health # should time outCommon mistakes
- Assuming policies work on a CNI that does not enforce them.
- Default-deny egress without allowing DNS, breaking every service lookup.
- Putting namespaceSelector and podSelector in separate list items (OR) when you meant AND.
- Forgetting monitoring and ingress controllers need allowed paths too.
Key takeaways
- Without policies, all pods can talk to all pods.
- Start with default deny, then allow specific paths.
- Enforcement depends on your CNI.
- Allow DNS egress and test every path.
FAQ
Do NetworkPolicies work at layer 7?
Standard NetworkPolicies are L3/L4 (IPs, ports). CNIs like Cilium and service meshes add HTTP-aware policies.
Can I block access to the cloud metadata service?
Yes, with egress policies denying the metadata IP (often 169.254.169.254), which is a good hardening step.
How do I visualise allowed traffic?
Tools such as Cilium Hubble or policy visualisers show flows and which policies allow or drop them.