← All writing

Kubernetes Network Policies: From Default-Allow to Zero Trust

· Sourabh G Kulkarni

The flat network trap in Kubernetes, why pods can talk to anything by default, and how to implement zero-trust ingress and egress policies with Calico and Cilium.

Kubernetes Network Policies: From Default-Allow to Zero Trust

In a default Kubernetes cluster, the network model is completely flat: every pod can communicate directly with every other pod across every namespace. A compromised frontend pod can open a raw socket to your production database without needing to break through a firewall.

Kubernetes NetworkPolicies exist to fix this by implementing a Zero Trust architecture: default-deny all non-whitelisted traffic.

flowchart LR
    subgraph Frontend["Namespace: web"]
        WebPod["Frontend Pod"]
    end

    subgraph Backend["Namespace: api"]
        ApiPod["API Gateway Pod"]
        WorkerPod["Background Worker"]
    end

    subgraph Database["Namespace: data"]
        DBPod["PostgreSQL Primary"]
    end

    WebPod -->|Allowed (Port 443/8080)| ApiPod
    WebPod x-.-x|BLOCKED by NetworkPolicy| DBPod
    WorkerPod x-.-x|BLOCKED by NetworkPolicy| DBPod
    ApiPod -->|Allowed (Port 5432)| DBPod

1. The CNI Prerequisite: Calico or Cilium

A common mistake in Kubernetes is writing a NetworkPolicy resource on a cluster running a basic cloud CNI (like standard AWS VPC CNI without policy controllers or flannel). NetworkPolicies are ignored unless an enforcing CNI plugin is installed.

Always verify that your cluster has a policy-enforcing CNI active:

  • Calico: Uses Linux iptables and IP sets.
  • Cilium: Uses eBPF in the Linux kernel for high-throughput, low-latency packet filtering without iptables table bloat.

2. Step 1: Default-Deny All Traffic

In Zero Trust, start by locking the namespace down completely. No inbound (ingress) or outbound (egress) traffic is permitted unless explicitly granted:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: production-api
spec:
  podSelector: {} # Applies to all pods in this namespace
  policyTypes:
    - Ingress
    - Egress

Once applied, pods in production-api cannot receive connections, nor can they initiate connections—even DNS queries to CoreDNS will drop until whitelisted.


3. Step 2: Allow Essential CoreDNS Egress

Every pod needs to resolve domain names. Whitelist outbound port 53 to kube-system:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns-egress
  namespace: production-api
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

4. Step 3: Granular Ingress Whitelisting

Allow only the frontend ingress controller or API gateway to connect to your backend service on port 8080:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-ingress-from-frontend
  namespace: production-api
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/component: backend-api
  policyTypes:
    - Ingress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              environment: production-web
          podSelector:
            matchLabels:
              app.kubernetes.io/component: frontend
      ports:
        - protocol: TCP
          port: 8080

5. Debugging Network Policies in Production

When a connection fails, determining whether it’s a DNS issue, an app error, or a dropped packet by policy can be challenging.

# 1. Test connectivity interactively from inside the pod namespace
kubectl run netshoot --rm -i --tty --image nicolaka/netshoot -- /bin/bash

# 2. Check TCP socket handshake
nc -zv backend-service.production-api 8080

# 3. For Cilium clusters, use the live policy trace CLI
cilium monitor --type drop

Treat network policies like infrastructure code: commit them alongside service Helm charts to ensure security boundaries scale automatically with your deployments.