Senior
How is DNS resolution carried out in Kubernetes? Tell me about FQDN and the DNS resolution chain.
sobes.tech AI
Answer from AI
In Kubernetes, DNS resolution occurs through the built-in DNS service (usually CoreDNS), which is responsible for resolving service and pod names within the cluster.
FQDN in Kubernetes
The Fully Qualified Domain Name (FQDN) for a service in Kubernetes is constructed using the template:
<service>.<namespace>.svc.<cluster-domain>
<service>— the name of the service<namespace>— the namespace where the service residessvc— a fixed part indicating a service<cluster-domain>— the cluster domain, default iscluster.local
For example, a service my-service in the default namespace will have the FQDN my-service.default.svc.cluster.local.
DNS resolution chain
- Pod sends a DNS query to the DNS server specified in
/etc/resolv.conf(usually CoreDNS). - CoreDNS checks local records for Kubernetes services and pods.
- If the name is short (e.g.,
my-service), resolution occurs with the addition of search domains from/etc/resolv.conf, such asmy-service.default.svc.cluster.local. - If the name is an FQDN, CoreDNS attempts to resolve it directly.
- If CoreDNS cannot resolve the name, the request is forwarded to an external DNS (e.g., Google DNS).
Thus, DNS in Kubernetes allows pods to refer to services by short names, and when necessary, use full domain names for precise identification.
Example /etc/resolv.conf in a pod:
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
Here, 10.96.0.10 is the IP of CoreDNS, and ndots:5 means that names with fewer than 5 dots are considered incomplete, and search domains are appended to them.