You apply this and the api pods now fail every request with could not resolve host: postgres.shop.svc; the team insists 'the policy allows Postgres'. Which line is the bug, and which line is a second, quieter problem?
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-lockdown
namespace: shop
spec:
podSelector:
matchLabels:
app: api
policyTypes: [Ingress, Egress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
ports:
- port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: postgres
ports:
- port: 5432
- to:
- ipBlock:
cidr: 0.0.0.0/0
except: [10.0.0.0/8]
ports:
- port: 443
The egress block (line 18) forgets to allow DNS to kube-dns, and the 0.0.0.0/0 rule on 443 (line 26) opens the whole internet.
- Line 18: Egress isolation is on, but the egress block has no rule allowing DNS (UDP/TCP 53) to CoreDNS in kube-system, so every name lookup is dropped before the Postgres rule can matter.
- Line 26: Allowing 0.0.0.0/0 on 443 with only the private range excepted lets the pod reach any public host, including the cloud metadata service if it is not in the except list on that platform; broad egress defeats the lockdown.
DNS is the egress rule everyone forgets: with Egress in policyTypes, resolution is denied unless explicitly allowed, and the failure looks like 'the policy is not working'. The broad 443 rule is the second-order problem: a lockdown that permits the whole internet is not one.
Blaming CoreDNS or saying 'the policy is not working'. The policy is working exactly as written: resolution is denied before the Postgres rule can matter.