Skip to content

NetworkPolicy basics — restricting pod-to-pod traffic

Write working NetworkPolicies — default deny, ingress and egress rules, selector pitfalls, DNS egress, and which CNIs actually enforce them.

By default Kubernetes is flat — every Pod can talk to every other Pod on every port. NetworkPolicy is the standard way to fix that. It is also one of the easiest features to get wrong because it is selector-driven, additive, and silently no-ops on CNIs that don’t enforce it. This article gives you a working policy set: default-deny in a namespace, a tight allow rule for one service-to-service path, the egress rule for DNS that everyone forgets, and the verification commands that prove the policy is actually in effect.

How to verify

The first check is whether your CNI enforces NetworkPolicy at all. Calico, Cilium, Antrea, and kube-router enforce; the upstream Flannel does not (you’d add Calico’s policy plugin on top). Confirm:

kubectl get pods -n kube-system -l 'k8s-app in (calico-node,cilium,antrea-agent,kube-router)'
kubectl get crd | grep -E 'calico|cilium'

Then the policies themselves:

kubectl get networkpolicy -A
kubectl describe networkpolicy default-deny -n team-payments

And finally a live test from inside the namespace — start a debug Pod, try a connection, expect it to be blocked or allowed depending on policy:

kubectl run -n team-payments debug --rm -it \
  --image=nicolaka/netshoot -- \
  curl -m 3 -sv http://api.team-payments.svc.cluster.local 2>&1 | head -5

If the policy is deny but the connection succeeds, the CNI is not enforcing — fix that first.

What’s happening

A NetworkPolicy selects Pods (by label) within a namespace and specifies which traffic those Pods accept. Two policy types: Ingress controls incoming, Egress controls outgoing. By default, when no policy selects a Pod, all traffic is allowed; once any policy selects a Pod, traffic in the relevant direction is denied unless an explicit rule allows it. Policies are additive — a Pod can be selected by multiple policies, and the effective allowlist is the union of their rules.

NetworkPolicy is L3/L4 — IPs, ports, and protocols. It does not understand L7 (no HTTP method, no SNI). For L7 enforcement you need an extension (Cilium’s CiliumNetworkPolicy, an Envoy/Istio sidecar mesh policy). NetworkPolicy also doesn’t apply to host-network Pods (the rules live in the Pod network namespace, but host-network Pods share the host’s).

A handful of selectors do most of the work: podSelector (match Pods by label, scoped to the policy’s namespace), namespaceSelector (match all Pods in matching namespaces), and ipBlock (CIDR + exceptions, for nodes and external endpoints). Combining podSelector with namespaceSelector in the same from block means AND (Pods with the label in namespaces with the label); putting them in separate from blocks means OR.

The procedure

  1. Default-deny ingress AND egress in a namespace. Without an explicit egress rule, even DNS will break — that’s intentional, you’ll add DNS back in step 2.

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: default-deny
      namespace: team-payments
    spec:
      podSelector: {}
      policyTypes: ["Ingress", "Egress"]
  2. Allow DNS egress to kube-dns / CoreDNS. Without this, every Pod with no other egress policy fails to resolve hostnames.

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: allow-dns-egress
      namespace: team-payments
    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
  3. Allow one service-to-service path. Web Pods can reach the API Pods on port 8080, nothing else:

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: api-allow-web
      namespace: team-payments
    spec:
      podSelector:
        matchLabels: { app: api }
      policyTypes: ["Ingress"]
      ingress:
        - from:
            - podSelector:
                matchLabels: { app: web }
          ports:
            - protocol: TCP
              port: 8080
  4. Allow egress from web Pods to api Pods so the reverse direction works:

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: web-egress-to-api
      namespace: team-payments
    spec:
      podSelector: { matchLabels: { app: web } }
      policyTypes: ["Egress"]
      egress:
        - to:
            - podSelector:
                matchLabels: { app: api }
          ports:
            - protocol: TCP
              port: 8080
  5. Allow ingress from another namespace by labelling the source namespace and selecting it:

    kubectl label namespace observability purpose=metrics-scraping
    ingress:
      - from:
          - namespaceSelector:
              matchLabels: { purpose: metrics-scraping }
          - podSelector:
              matchLabels: { app: prometheus }
        ports: [{ protocol: TCP, port: 9100 }]
  6. Allow egress to an external CIDR for an integration the cluster needs to call. Exclude the cluster CIDR to keep “outside” meaning what you expect:

    egress:
      - to:
          - ipBlock:
              cidr: 203.0.113.0/24
              except: ["10.0.0.0/8"]
        ports: [{ protocol: TCP, port: 443 }]

Common pitfalls

  • Flannel (upstream) does not enforce NetworkPolicy — your YAML applies cleanly but traffic still flows; switch CNI or add a policy plugin.
  • Forgetting DNS egress is the single most common cause of “the policy broke everything” — Pods without it can’t resolve any Service name.
  • Operators and ingress controllers in kube-system get blocked too if you apply a deny-everything policy cluster-wide — scope policies per namespace, leave system namespaces unrestricted unless you’ve thought it through.
  • podSelector: {} matches all Pods in the policy’s namespace, not all Pods cluster-wide; cross-namespace selection needs namespaceSelector.
  • NetworkPolicy applies to Pod traffic, not the kubelet probes — readiness/liveness checks come from the node and are not blocked by policy; if your readiness checks are HTTP from outside the cluster, they aren’t policy-affected.

We operate NetworkPolicy for clients with default-deny baselines per namespace, an allowlist library checked into Git, and Calico/Cilium enforcement audited per release. See /en/services/managed-operations/.