nodemethod
KubernetesIntermediate

Default-deny Kubernetes network policies

Start every namespace from deny-all, then allow only the flows each service actually needs.

By default every pod in a Kubernetes cluster can reach every other pod. Network policies, enforced by the CNI plugin (Calico, Cilium, or the cloud provider's own), turn that into an allow-list. A namespace-wide default deny for ingress and egress, followed by narrow policies per service, means a compromised pod can reach only what that service was meant to reach.

NAMESPACE: SHOP · POLICY: DEFAULT-DENY:8080:9000:5432:53deniedLBIngress controllernamespace: ingressAPPwebapp=webAPPapiapp=apiDBpostgresapp=dbRTkube-dnskube-system!debug podno matching policy
traffictunnel / replicationblocked

When to use it

  • Any multi-tenant or multi-team cluster
  • Clusters running internet-facing services
  • Compliance scopes that need network segmentation inside the cluster

When to avoid it

  • Clusters whose CNI does not enforce policies: the objects are accepted but do nothing

How traffic flows

  1. Apply a policy that selects all pods and allows nothing. All ingress and egress in the namespace stops.
  2. Allow DNS egress to kube-dns, or nothing else will work.
  3. Add one ingress policy per service naming exactly which pods or namespaces may connect, and on which port.
  4. Add matching egress rules for outbound calls, including to databases and external APIs.

Configuration examples

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: shop
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: shop
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 }

Examples are starting points. Review provider limits, names and versions for your environment before applying.

What to monitor

SignalWhy it matters
Denied flows by source and destinationSeparates a missing rule from a pod that should never connect.
Namespaces without a default-deny policyEvery new namespace starts wide open.
Policy changesA broad allow added during an incident often stays forever.

Common failure modes

Forgot to allow DNS

Everything in the namespace fails name resolution after default-deny.

Pulse sees DNS timeouts spike right after the policy is applied and names the missing rule.

Wide-open namespace

A new team namespace has no policies at all.

Sentinel lists namespaces without default-deny in its daily audit.

Checklist

0 of 5 done
Was this pattern useful?

Run this pattern with NodeMethod watching it.

NodeMethod is onboarding early-access teams now. Tell us about your environment and we will be in touch.

esc
↑↓ to move↵ to open