Sub topic 14
Keeping DNS working under egress restrictions
This chapter assumes you know how to write a NetworkPolicy, how default-deny works, and how namespaceSelector and podSelector combine. It covers one specific failure: a default-deny egress policy that breaks name resolution.
Why DNS breaks first
Pods find Services by name. Before a pod connects to backend, it asks the cluster DNS server to turn that name into an IP address. That lookup is outbound traffic from the pod, to the DNS pods in kube-system, on port 53.
A default-deny policy that lists Egress blocks that lookup too. The application then fails with "bad address" or a timeout, even if you allowed the real connection. Connecting by IP address would still work, which is why the problem is easy to misread.
The DNS egress policy
Add a policy that allows egress to the DNS pods on port 53. DNS uses UDP by default and falls back to TCP for large responses, so allow both.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: demo
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: 53Points to check:
podSelector: {}applies the rule to every pod indemo.- The
namespaceSelectorandpodSelectorsit in one list item, so they combine with AND. Only pods labeledk8s-app=kube-dnsinkube-systemmatch. - Policies are additive. This policy adds to
default-deny-all. It does not replace it.
Gotchas
- Label differences. CoreDNS pods carry
k8s-app=kube-dnsin most clusters, even though they run CoreDNS. Some distributions or node-local DNS caches use other labels or run in a different namespace. Check withkubectl get pods -n kube-system --show-labels. - Node-local DNS. If the cluster uses a node-local cache, pods may send queries to a node address instead of a pod. A pod selector will not match that address, so you may need an
ipBlockrule. - Namespace label. The label
kubernetes.io/metadata.nameis set automatically on every namespace in current Kubernetes versions. On very old clusters you must label the namespace yourself.
Verify
Run a lookup from a temporary pod in demo:
kubectl run dnstest --rm -it --image=busybox:1.36 --restart=Never -n demo -- nslookup backendWithout the DNS policy, the lookup times out. With it, you get an IP address back. Then run the connection test from the earlier chapter to confirm the application path is allowed as well.
Exercise
After you apply default-deny for Ingress and Egress in demo, wget http://backend:8080 from a frontend pod fails with "bad address", although a policy allows frontend egress to backend on TCP 8080. What is missing, and why does connecting to the backend pod's IP address directly work?
Show answer
The pod cannot resolve the name backend because egress to cluster DNS (kube-dns pods in kube-system, port 53 over UDP and TCP) is blocked. Add an egress policy such as allow-dns-egress. Using the IP address skips the DNS lookup, so only the already-allowed backend rule applies.