Skip to main content
Interactive k8s-cilium Configurator →Open Architecture Blueprint ↗
P0 INCIDENTL7 BARM11 · 6 MIN READ

Zero-Trust CiliumNetworkPolicy DNS Blackhole & Rolling Deploy 502s

Prevent default-deny Cilium egress policies from blocking kube-dns:53 and eliminate Kubernetes rolling deployment 502s with preStop graceful drain hooks.

PRODUCTION SYMPTOM & ALERT SIGNATURE

Applying a strict CiliumNetworkPolicy immediately triggers DNS lookup timeouts (i/o timeout to 10.96.0.10:53), and every rolling deployment causes a 3-second burst of HTTP 502 errors.

rate(coredns_dns_requests_total[5m]) == 0 OR increase(ingress_nginx_requests{status="502"}[2m]) > 15
01 · ROOT-CAUSE MECHANICS (WHY IT HAPPENS)

In Kubernetes and Cilium, selecting a pod with an egress policy switches that pod to default-deny egress. If you allow egress only to pgbouncer:6432 without explicitly permitting UDP/TCP port 53 to kube-dns, pods cannot resolve service hostnames at all.

During pod termination, kubelet sends SIGTERM to the container IN PARALLEL with kube-proxy / Cilium removing the pod IP from EndpointSlices and eBPF BPF maps across cluster nodes.

If the app immediately closes its listening socket on SIGTERM (preStop=0s), in-flight traffic still routed by nodes whose eBPF maps update 1-4 seconds later hits ECONNREFUSED and returns HTTP 502.

02 · LIVE TRIAGE COMMANDS (COPY & RUN)
Watch live eBPF policy drops in Hubble to spot blocked DNS egress
$ hubble observe --pod checkout-api --verdict DROPPED --last 20
Smoking gun: DROPPED UDP/53 egress packets to kube-system/coredns.
Inspect pod terminationGracePeriodSeconds and preStop lifecycle hook
$ kubectl get deploy checkout-api -o jsonpath="{.spec.template.spec.terminationGracePeriodSeconds} {.spec.template.spec.containers[0].lifecycle}"
Smoking gun: Empty lifecycle block (no preStop sleep) causing abrupt socket closure on SIGTERM.
Verify EndpointSlice updates vs pod termination timestamps
$ kubectl get events --sort-by=.lastTimestamp | grep -E "Killing|Unhealthy"
Smoking gun: HTTP 502 burst aligned to the exact second of Killing events.
03 · PRODUCTION DIFF (BROKEN VS HARDENED)
BROKEN · Broken Policy & Deployment (blocks DNS + abrupt SIGTERM shutdown)
# 1. Cilium egress allows DB only -> silently blocks kube-dns:53!
egress:
  - toEndpoints:
      - matchLabels: { app: pgbouncer }
    toPorts:
      - ports: [{ port: "6432", protocol: TCP }]

# 2. Pod exits immediately on SIGTERM while ingress still routes to it
containers:
  - name: api
    image: app:v42
    # Missing lifecycle.preStop drain delay!
HARDENED · Production Zero-Trust Cilium Policy + Zero-Drop Rolling Drain
# 1. Explicitly allow DNS egress to kube-dns + DB egress
egress:
  - toEndpoints:
      - matchLabels:
          k8s:io.kubernetes.pod.namespace: kube-system
          k8s-app: kube-dns
    toPorts:
      - ports:
          - { port: "53", protocol: UDP }
          - { port: "53", protocol: TCP }
  - toEndpoints:
      - matchLabels: { app: pgbouncer }
    toPorts:
      - ports: [{ port: "6432", protocol: TCP }]

# 2. Drain EndpointSlice / eBPF maps before SIGTERM stops the listener
terminationGracePeriodSeconds: 35
containers:
  - name: api
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "sleep 10"]
04 · MINUTE-24 BAR-RAISER CHECKPOINT (L7 · M11)TEST YOUR INSTINCT · ZERO SIGNUP

Why does adding a 10-second preStop sleep eliminate HTTP 502s during Kubernetes rolling updates?

05 · POST-FIX VERIFICATION CHECKLIST
  • Confirm hubble observe --verdict DROPPED shows zero unintended port-53 drops.
  • Run a continuous 100 RPS load test during kubectl rollout restart deploy/checkout-api and verify 0 HTTP 502 responses.
  • Ensure terminationGracePeriodSeconds (e.g., 35s) is strictly greater than preStop sleep (10s) plus max in-flight request duration.
06 · FREQUENTLY ASKED ENGINEERING QUESTIONS

Does a Kubernetes namespace provide network isolation by default?

No. By default, every pod in every namespace can communicate with every other pod unless a NetworkPolicy or CiliumNetworkPolicy selects the pod.

Why must both UDP 53 and TCP 53 be allowed for kube-dns?

Standard DNS queries use UDP 53, while large responses (> 512 bytes, DNSSEC, or large headless service SRV records) fall back to TCP 53.

Should my container process also handle SIGTERM gracefully after preStop?

Yes. After preStop completes, the app receives SIGTERM, stops accepting new requests, finishes in-flight requests cleanly, closes DB pools, and exits 0 before SIGKILL.