FQDN rules and the DNS proxy
About 654 words
A packet carries an address, not a name. For toFQDNs: [{matchName: api.github.com}] to mean anything, Cilium has to know which addresses api.github.com resolves to, for this pod, right now. It learns that by watching the pod's DNS lookups through its DNS proxy. This page explains how the rendered manifests set that up, and what goes wrong when it isn't.
The DNS rule in default-deny
Every pod under the default deny gets one egress allow from the default-deny policy itself:
egress:
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
rules:
dns:
- matchPattern: "*"It does two things. It lets every pod query the cluster DNS on port 53 over UDP and TCP, which nothing works without. And the rules: dns part sends those queries through Cilium's DNS proxy. matchPattern: "*" allows every name, so the rule doesn't restrict lookups; it only makes Cilium see them. When a pod resolves api.github.com, the proxy notes the answer's addresses, and a toFQDNs rule for that name then allows connections to those addresses, for as long as the DNS answer is valid.
The DNS server is kube-dns in kube-system by default (k8s-app=kube-dns, which CoreDNS deployments usually keep). If yours runs elsewhere, say so in the policy:
spec:
dns:
namespace: dns
selector: app=corednsEndpoints in excluded namespaces
A pod in a namespace left out of the default deny isn't selected by default-deny, so it doesn't get the DNS rule. Its lookups bypass the proxy, and a toFQDNs rule in its allow policy would never learn an address. So when an endpoint in an excluded namespace has a fqdn: destination, its own policy gets the DNS rule too, commented the cluster DNS, through the DNS proxy (for toFQDNs). external-dns in the example catalog runs in kube-system:
egress:
# the cluster DNS, through the DNS proxy (for toFQDNs)
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
rules:
dns:
- matchPattern: "*"While the namespace is open, the toFQDNs rule allows nothing that wasn't allowed anyway, but the proxy sees the lookups, and the rule is ready for the day the namespace is enforced.
What Cilium needs
- The DNS proxy. It's part of Cilium's L7 proxy, which is on by default (
l7Proxyin Helm,--enable-l7-proxy). Without it, DNS rules andtoFQDNsdon't work. - Lookups the proxy can see. A pod that resolves names some other way, over DNS-over-HTTPS or against a resolver outside the cluster, never shows the proxy its answers. Its
toFQDNsrules allow nothing.
Things that trip people up
- A wildcard covers one label.
fqdn:*.example.commatchesa.example.com, notexample.comand nota.b.example.com. List each level you need. Cilium's**.prefix for any depth isn't accepted by the plugin. - CNAMEs. A name that is a CNAME for another resolves through it, and Cilium allows the addresses of the final answer for the name the pod asked for. Name what the application asks for, not what it ends up at.
- Connections before lookups. A pod that connects to an address it didn't resolve through the cluster DNS (one from a config file, or from its own long-lived DNS cache) has no answer the proxy saw, and is dropped. Name destinations like that with
cidr:instead. - Short TTLs and many addresses. CDNs answer with many addresses and short TTLs. Cilium keeps addresses it has seen for a while after the TTL expires, but a busy client can still hit an address it learned elsewhere. If a FQDN rule drops occasionally, check
hubble observe --to-fqdn <name>for the addresses involved.
