Skip to content
StackDecode
AdvancedLesson 23 of 35

NetworkPolicy

Firewall rules between pods.

By default every pod can talk to every other pod. A NetworkPolicy restricts that. Enforcement needs a CNI that supports it (Calico, Cilium and others).

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: db-allow-web, namespace: prod }
spec:
  podSelector: { matchLabels: { app: db } }
  policyTypes: [Ingress]
  ingress:
    - from:
        - podSelector: { matchLabels: { app: web } }
      ports:
        - port: 5432

A common baseline is a default-deny policy per namespace, then explicit allows.