Almost every request inside a Kubernetes cluster starts with a DNS lookup. When a frontend Pod calls http://orders or a worker connects to postgres.data, it relies on cluster DNS to turn that name into an IP address. When DNS works, nobody thinks about it. When it breaks, Services look down, requests time out, and the error messages rarely point at DNS.
This guide explains how DNS works in Kubernetes: which records the cluster creates, how a Pod’s resolver is set up, what CoreDNS does, how to tune DNS for a single Pod, and a step by step checklist for debugging resolution problems, all based on the official Kubernetes documentation. If Services are new to you, read our Kubernetes networking explained guide first.
How service discovery works in Kubernetes
Kubernetes publishes information about Services and Pods, and the cluster DNS server turns that information into DNS records. The kubelet on each node writes a resolver configuration (/etc/resolv.conf) into every container, so applications can look up Services by name instead of by IP address.
In most clusters the DNS server is CoreDNS. It runs in the kube-system namespace and sits behind a Service that is still named kube-dns, for backward compatibility with the older kube-dns add-on. Its Pods also keep the label k8s-app=kube-dns. The kubelet hands the IP of that Service to containers through its --cluster-dns flag, and the cluster domain (cluster.local by default) through --cluster-domain.
CoreDNS is a CNCF graduated project written in Go and built around plugins. It was accepted into the CNCF in February 2017 and graduated in January 2019. Its kubernetes plugin implements the Kubernetes DNS-Based Service Discovery specification, which defines the record names a cluster must serve.
DNS records for Services and Pods
The naming pattern is the most useful thing to remember. For a Service, the fully qualified name is:
my-svc.my-namespace.svc.cluster.local
What that name returns depends on the kind of Service:
- Normal Services (with a cluster IP) get A and/or AAAA records that resolve to the Service’s cluster IP. Traffic is then routed to one of the backing Pods.
- Headless Services (
clusterIP: None) get A and/or AAAA records that resolve to the IPs of all selected Pods. The client picks from that set itself. StatefulSets rely on this to give each replica a stable name. - SRV records are created for named ports, in the form
_port-name._port-protocol.my-svc.my-namespace.svc.cluster.local. They return the port number together with the target name.
Pods can get records too. When a Pod sets spec.hostname and spec.subdomain, and a headless Service with the same name as the subdomain exists in the namespace, DNS serves a name such as busybox-1.busybox-subdomain.my-namespace.svc.cluster.local that points at that Pod. The Pod must be ready to get a record, unless the Service sets publishNotReadyAddresses: true. The documentation warns that any other names that happen to resolve are implementation details and can change without notice, so stick to the documented layout.
Inside a Pod: resolv.conf, search domains and ndots
With the default DNS policy, the kubelet writes a resolv.conf like this one, taken from the Kubernetes documentation:
nameserver 10.32.0.10
search <namespace>.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
The nameserver line points at the cluster DNS Service. The search line lists the suffixes the resolver appends to short names. That is why a Pod can reach a Service in its own namespace as just data, and a Service in the prod namespace as data.prod. A plain data query from the test namespace will not find the Service in prod, because names without a namespace are limited to the Pod’s own namespace.
The ndots:5 option tells the resolver that a name with fewer than five dots should be tried with the search suffixes before it is tried as an absolute name. That is handy for cluster names. For an external name such as api.example.com, it means the resolver may try several cluster suffixes first, which adds extra queries to CoreDNS. If a workload makes many external lookups, use fully qualified names with a trailing dot, or lower ndots for that Pod with dnsConfig, shown in the next section.
Pod DNS policies and custom DNS config
The dnsPolicy field controls where a Pod’s resolver settings come from:
- ClusterFirst: used when nothing is set. Names outside the cluster domain are forwarded by the DNS server to upstream nameservers.
- Default: the Pod inherits the resolver configuration of its node. Despite the name, this is not the default policy.
- ClusterFirstWithHostNet: set this for Pods with
hostNetwork: true, otherwise they fall back to the Default behavior. It is not supported on Windows. - None: ignore the Kubernetes DNS settings and provide everything through
dnsConfig.
The dnsConfig field (stable since Kubernetes v1.14) works with any policy. It lets you add up to three nameservers, extra search domains and resolver options. This example comes from the Kubernetes documentation:
apiVersion: v1
kind: Pod
metadata:
namespace: default
name: dns-example
spec:
containers:
- name: test
image: nginx
dnsPolicy: "None"
dnsConfig:
nameservers:
- 192.0.2.1 # this is an example
searches:
- ns1.svc.cluster-domain.example
- my.dns.search.suffix
options:
- name: ndots
value: "2"
- name: edns0
With this spec, the container uses 192.0.2.1 as its nameserver, the two listed search domains, and the options ndots:2 and edns0. Kubernetes accepts up to 32 search domains, with a combined length of up to 2048 characters.
Configuring CoreDNS with the Corefile
CoreDNS reads its settings from a Corefile stored in the coredns ConfigMap in kube-system. The default Kubernetes Corefile turns on a short list of plugins:
errors: logs errors to stdout.healthandready: a health endpoint on port 8080 and a readiness endpoint on port 8181.kubernetes: answers queries for Services and Pods incluster.localand the reverse lookup zones.prometheus: exposes metrics on port 9153.forward: sends names outside the cluster to the resolvers listed in/etc/resolv.conf.cache,loop,reloadandloadbalance: response caching, forwarding loop detection, automatic reload of an edited Corefile, and round robin ordering of answers.
A common change is a stub domain, which sends queries for one private zone to a specific DNS server. The Kubernetes documentation uses a Consul server at 10.150.0.1 as the example, added as its own server block in the Corefile:
consul.local:53 {
errors
cache 30
forward . 10.150.0.1
}
Edit the ConfigMap with kubectl -n kube-system edit configmap coredns and allow up to two minutes for the reload plugin to pick up the change. CoreDNS does not accept FQDNs such as ns.foo.com as nameservers here, so use IP addresses.
For busy clusters, Kubernetes also offers NodeLocal DNSCache (stable since v1.18). It runs a DNS caching agent as a DaemonSet on every node, so Pods query a cache on their own node instead of crossing the network to a CoreDNS Pod. This skips iptables DNAT and connection tracking for DNS traffic, which reduces conntrack races and stops UDP DNS entries from filling the conntrack table. The agent can also upgrade its own queries to CoreDNS to TCP. Our Kubernetes workloads explained guide covers how DaemonSets behave.
Hands-on: debugging DNS resolution step by step
Prerequisites: a Kubernetes cluster running the CoreDNS add-on, and kubectl configured to talk to it. A local cluster from kind or minikube is enough; see our local Kubernetes clusters compared guide. The commands below come from the official Debugging DNS Resolution page.
1. Start a test Pod
kubectl apply -f https://k8s.io/examples/admin/dns/dnsutils.yaml
kubectl get pods dnsutils
This creates a Pod named dnsutils in the default namespace from the agnhost test image, which includes nslookup.
2. Run a lookup
kubectl exec -i -t dnsutils -- nslookup kubernetes.default
If this returns an address for kubernetes.default, cluster DNS is working. If it says it cannot resolve the name, continue with the next checks.
3. Check the Pod’s resolver config
kubectl exec -ti dnsutils -- cat /etc/resolv.conf
Confirm that the search path includes default.svc.cluster.local svc.cluster.local cluster.local and that the nameserver matches the IP of the kube-dns Service. Cloud providers may add extra search domains.
4. Check CoreDNS Pods, logs, Service and endpoints
kubectl get pods --namespace=kube-system -l k8s-app=kube-dns
kubectl logs --namespace=kube-system -l k8s-app=kube-dns
kubectl get svc --namespace=kube-system
kubectl get endpointslice -l kubernetes.io/service-name=kube-dns --namespace=kube-system
You want running CoreDNS Pods, no unexpected errors in the logs, a kube-dns Service on port 53 for UDP and TCP, and EndpointSlices that list the CoreDNS Pod IPs. If no CoreDNS Pod is running, the DNS add-on may not be deployed in your environment.
5. Check permissions and namespaces
kubectl describe clusterrole system:coredns -n kube-system
CoreDNS needs list and watch on endpoints, namespaces, pods, services and endpointslices. Missing permissions can show up as SERVFAIL answers. Also check the namespace: from a different namespace, query <service-name>.<namespace> rather than the bare Service name.
To see every query CoreDNS receives, add the log plugin to the Corefile for a while, then remove it again once you are done.
Known DNS issues to watch for
- systemd-resolved on nodes (for example on Ubuntu) replaces
/etc/resolv.confwith a stub file that can cause a forwarding loop. Point the kubelet--resolv-confflag at/run/systemd/resolve/resolv.conf. kubeadm detects this and sets the flag for you. - The glibc nameserver limit is three entries, and Kubernetes uses one of them. If a node already lists three nameservers, some are dropped.
- Alpine 3.17 and earlier lack TCP fallback in the resolver, so DNS responses larger than 512 bytes can fail. Use Alpine 3.18 or later.
- setHostnameAsFQDN makes the Pod hostname its full domain name, but Linux limits hostnames to 64 characters. A longer FQDN leaves the Pod stuck in Pending.
Key takeaways
- Services resolve as
my-svc.my-namespace.svc.cluster.local. Short names only work inside the same namespace. - Normal Services return their cluster IP, headless Services return Pod IPs, and named ports get SRV records.
- CoreDNS, a CNCF graduated project, serves cluster DNS behind a Service that is still called
kube-dns. ndots:5causes extra lookups for external names. Tune it per Pod withdnsConfigwhen needed.- Customize cluster DNS through the
corednsConfigMap, and consider NodeLocal DNSCache for busy clusters. - Debug in order: test Pod, resolv.conf, CoreDNS Pods and logs, Service, endpoints, RBAC, then namespace.
Sources
This guide is based on the Kubernetes documentation pages DNS for Services and Pods, Debugging DNS Resolution, Customizing DNS Service and Using NodeLocal DNSCache in Kubernetes Clusters, plus the Kubernetes DNS-Based Service Discovery specification, the CoreDNS kubernetes plugin documentation and the CNCF CoreDNS project page.
