← All insightsSecurity

Zero-trust networking in Kubernetes with NetworkPolicies

Nir Geier, CodeWizard · 27 June 2025 · 3 min read · Intermediate

A fresh Kubernetes cluster is flat: any pod can reach any other pod on any port. That means one compromised container can probe your entire cluster. NetworkPolicies let you move to default-deny and allow only intended traffic.

Default-deny a namespace

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny, namespace: app }
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]

Then allow exactly what is needed

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: web-to-api, namespace: app }
spec:
  podSelector: { matchLabels: { app: api } }
  ingress:
    - from: [{ podSelector: { matchLabels: { app: web } } }]
      ports: [{ port: 8080 }]
  • Your CNI must enforce NetworkPolicy (Calico, Cilium) - some do not
  • Do not forget egress - DNS (kube-dns) needs an explicit allow
  • Test policies in staging; a wrong rule can black-hole a service
Default-deny is the difference between one compromised pod and a compromised cluster.

Start with default-deny in one namespace, add allow rules from real traffic, and expand. Pair it with mTLS from a mesh for defense in depth.

Like where this is going?

Get hands-on help from the engineers who write these posts.

Book a free consult