Don't forget DNS: the one egress every pod needs

Don't forget DNS: the one egress every pod needs

The cluster-wide default-deny from the last lesson is correct - and it just broke almost every app. The moment egress is denied, pods can't reach CoreDNS, so every hostname lookup fails. The app doesn't log "blocked by policy"; it logs "connection to some-name timed out" and looks randomly broken. DNS is the one egress essentially every workload needs, so it's the first carve-out you add.

What you'll learn

The policy

apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
  name: allow-dns-egress
spec:
  order: 900
  namespaceSelector: kubernetes.io/metadata.name not in {"calico-system", "kube-system", "kube-public", "tigera-operator"}
  types:
    - Egress
  egress:
    - action: Allow
      protocol: UDP
      destination:
        selector: k8s-app == "kube-dns"
        ports: [53]
    - action: Allow
      protocol: TCP
      destination:
        selector: k8s-app == "kube-dns"
        ports: [53]

What to observe

Allowed

Denied

The trap is allowing DNS by port alone. A rule that allows "UDP/53 to anywhere" lets a compromised pod tunnel data out over port 53 to an attacker's resolver. Pin the destination selector to CoreDNS so only the real DNS service is reachable.

{
  "question": "Why does this policy exclude kube-system and the other system namespaces from its subject?",
  "options": [
    "System namespaces can't run network policy",
    "So the default-deny doesn't apply to CoreDNS itself - otherwise you'd block the DNS service you're trying to allow access to",
    "Because CoreDNS uses port 53 and system namespaces use a different port"
  ],
  "answer": 1,
  "explain": "The carve-out is for workloads reaching DNS. If the lockdown also covered the DNS pods, you'd cut off the very service every pod is trying to reach."
}

Recap

DNS is the universal exception: allow egress to CoreDNS on UDP and TCP 53, pinned to its label, and keep the system plane out of your lockdown. Allowing the right traffic also means matching it precisely - which protocol, which port. That's the next lesson: ports and protocols.