Ground floorLesson 3 of 3
  1. Containers in Kubernetes terms, not reconciled
  2. Isolation: namespaces, cgroups and the runtime, not reconciled
  3. DNS in Kubernetes terms, not reconciled
    1. The resolution path
    2. Records a cluster uses
    3. resolv.conf, search domains and ndots
    4. TTLs and caching
    5. Transport
    6. Security and failure modes

DNS in Kubernetes terms

Every Service name a pod uses goes through resolv.conf, search domains and CoreDNS before it becomes an IP. Learn the path, the records, ndots, caching and the ways cluster DNS fails.

  • 18 min
  • 4 objectives
  • 6 scenes
  • Includes an On AWS layer

Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.

Pods find each other by name. A Service’s cluster IP is stable, but nobody hard-codes it: clients ask for web or web.shop and let DNS answer. When DNS is slow, every request that opens a connection is slow; when it is down, almost everything looks down. Knowing the path a lookup takes is what makes those failures quick to place.

Reconcile, in 6 scenes

The resolution path

A lookup has three kinds of participant. The stub resolverThe resolver library inside an application (glibc, musl, or a language runtime’s own) that reads /etc/resolv.conf and sends queries to the configured nameserver. in the client process sends one question to a recursive resolver, which does the legwork: it asks a root server where com. lives, asks the com. servers where example.com. lives, and asks the authoritative server for the answer, caching each step.

In a cluster, the recursive step is CoreDNS. The kubelet points every pod’s resolver at the cluster DNS Service (named kube-dns for compatibility, though the pods run CoreDNS; the kube-dns add-on itself is deprecated in Kubernetes 1.37). CoreDNS answers names under cluster.local itself, from the Services and EndpointSlices it watches through its kubernetes plugin, and forwards everything else to the upstream nameservers listed in its node’s /etc/resolv.conf.

A pod's resolver configuration, one cluster name and one external name
kubectl exec dnsutils -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
kubectl exec dnsutils -- nslookup kubernetes.default
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	kubernetes.default.svc.cluster.local
Address: 10.96.0.1
kubectl exec dnsutils -- nslookup example.com
Server:		10.96.0.10
Address:	10.96.0.10#53

Non-authoritative answer:
Name:	example.com
Address: 192.0.2.10

Non-authoritative answer marks the forwarded name: CoreDNS relayed it from upstream rather than owning the zone. The cluster name has no such line because CoreDNS is authoritative for cluster.local.

Records a cluster uses

RecordCluster nameAnswer
A / AAAAweb.shop.svc.cluster.localThe Service’s cluster IP (IPv4 / IPv6)
A / AAAA (headless Service)same name, clusterIP: NoneOne record per ready pod IP
SRV_http._tcp.web.shop.svc.cluster.localPort and target, only for named ports
CNAMEan ExternalName ServiceThe external name it aliases
A (pod)10-244-1-2.shop.pod.cluster.localThat pod’s IP
PTR10.0.96.10.in-addr.arpaReverse lookup of a Service or endpoint IP

SRV records exist only for named Service ports. For a normal Service the SRV target is the Service name itself; for a headless Service there is one answer per backing pod. An ExternalName Service is nothing but a CNAME:

An ExternalName Service and a headless Service
kubectl exec lowdots -- nslookup billing
Server:		10.96.0.10
Address:	10.96.0.10#53

billing.default.svc.cluster.local	canonical name = payments.example.com.
Name:	payments.example.com
Address: 203.0.113.20
kubectl exec lowdots -- dig +short web.default.svc.cluster.local
10.244.1.3
10.244.2.3

The headless web Service returned two pod IPs, one per replica, and the client picks one; there is no virtual IP and no kube-proxy balancing. Client-side load balancers such as gRPC’s use this to see every backend, and a headless Service is also what gives each StatefulSet pod a stable name such as web-0.web.shop.svc.cluster.local.

Public zones add records a cluster never serves. A CAA record names the certificate authorities allowed to issue for a domain: defined in RFC 6844 (January 2013), checked by every public CA since 2017-09-08 under CA/Browser Forum Ballot 187, and specified by RFC 8659 since November 2019. A certificate request from cert-manager fails when CAA lists a different CA.

resolv.conf, search domains and ndots

The kubelet writes each pod’s /etc/resolv.conf from its dnsPolicy. The default, ClusterFirst, sends queries to cluster DNS with three search domains: the pod’s namespace, svc.cluster.local and cluster.local. The policy named Default is not the default: it copies the node’s resolver settings and cannot resolve Services. Pods with hostNetwork: true need ClusterFirstWithHostNet to reach cluster DNS, and None takes everything from dnsConfig.

ndotsThe resolv.conf option that sets how many dots a name needs before the resolver tries it as an absolute name first. Kubernetes sets 5. is the rule that turns one name into several queries. A name with fewer dots than ndots is tried with each search domain appended first, and only then as written. web becomes web.default.svc.cluster.local on the first try, which is the point. But api.github.com has two dots, so the resolver first asks for api.github.com.default.svc.cluster.local, api.github.com.svc.cluster.local and api.github.com.cluster.local, each returning NXDOMAIN, before the real name, and twice over when the client asks for A and AAAA. A trailing dot (api.github.com.) marks a name absolute and skips the walk. A pod that mostly calls external names can lower ndots:

apiVersion: v1
kind: Pod
metadata:
  name: lowdots
spec:
  dnsConfig:
    options:
    - name: ndots
      value: "2"
  containers:
  - name: shell
    image: nicolaka/netshoot:v0.16
    command: ["sleep", "infinity"]

With ndots:2, api.github.com goes straight upstream, while web and web.shop still use the search list. Kubernetes allows up to 32 search domains and 2048 characters in total.

ExplainerName resolution in a pod

Client namespace
ndots

Podtoolboxshop10.244.3.4

/etc/resolv.conf

search
shop.svc.cluster.localsvc.cluster.localcluster.local
nameserver
10.96.0.10
options
ndots:5
  1. ledger.shop.svc.cluster.localnot sent yet
  2. ledger.svc.cluster.localnot sent yet
  3. ledger.cluster.localnot sent yet
  4. ledgernot sent yet

CoreDNSkube-dns10.96.0.10

  • kubernetes cluster.local in-addr.arpa ip6.arpaanswers cluster.local names from the API: authoritative
  • forward . /etc/resolv.confsends everything else to the node's resolver at 172.18.0.1

No query sent yet.

Answer

Waiting for the resolver.

$ kubectl -n shop exec toolbox -- nslookup ledger
Resolve the name to see what nslookup prints.

The name has no dots, fewer than ndots:5, so the resolver tries every search domain before the name as typed.

Pick a name and a namespace and resolve it. The pod's resolver expands the name through its search domains according to ndots, and CoreDNS answers each query or returns NXDOMAIN.

TTLs and caching

Every answer carries a TTL, and every cache on the path may hold it that long. CoreDNS’s own configuration lives in the coredns ConfigMap:

The Corefile
kubectl -n kube-system get configmap coredns -o jsonpath='{.data.Corefile}'
.:53 {
    errors
    health {
       lameduck 5s
    }
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
       pods insecure
       fallthrough in-addr.arpa ip6.arpa
       ttl 30
    }
    prometheus :9153
    forward . /etc/resolv.conf {
       max_concurrent 1000
    }
    cache 30 {
       disable success cluster.local
       disable denial cluster.local
    }
    loop
    reload
    loadbalance
}

The kubernetes plugin stamps cluster answers with ttl 30 (its own default is 5 seconds), so a client that caches may keep using a pod IP up to 30 seconds after the pod is gone. cache 30 holds forwarded answers for at most 30 seconds; without a number, the cache plugin’s ceilings are 3600 seconds for answers and 1800 for denials. The disable lines keep cluster names out of that cache, because the kubernetes plugin already answers them from memory.

Negative answers are cached too (RFC 2308). An NXDOMAIN for a name you are about to create can be held for the zone’s negative TTL, so a record that “should exist” keeps failing until that entry expires. Caches also live outside DNS servers: language runtimes, nscd, and systemd-resolved on hosts, which resolvectl flush-caches empties.

Transport

Queries go over UDP port 53. An answer too large for the UDP payload comes back with the truncated bit set, and the client retries over TCP port 53; RFC 7766 makes TCP support mandatory for every DNS implementation. Firewalls, security groups and NetworkPolicies must allow both. A default-deny egress policy needs a matching allow for DNS:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
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: 53

DNS over TLS (port 853) and DNS over HTTPS (port 443) encrypt the hop from a stub to its resolver. Browsers and public resolvers use them; in-cluster traffic rarely does.

Security and failure modes

DNSSEC signs zone data so a validating resolver can detect forged answers; a bad signature turns into SERVFAIL, not a wrong address. It does not encrypt anything. Inside a cluster, the common failures are plainer. With a default-deny egress policy and no DNS allow rule, the policy drops every query and each lookup waits out its timeout:

Port 53 blocked by a default-deny egress policy, then allowed
kubectl apply -f deny-egress.yaml
networkpolicy.networking.k8s.io/deny-egress created
kubectl exec dnsutils -- nslookup kubernetes.default
;; connection timed out; no servers could be reached

command terminated with exit code 1
kubectl apply -f allow-dns.yaml
networkpolicy.networking.k8s.io/allow-dns created
kubectl exec dnsutils -- nslookup kubernetes.default
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	kubernetes.default.svc.cluster.local
Address: 10.96.0.1

allow-dns.yaml is the policy from the previous section; with it applied beside the deny, the same lookup answers again. A timeout, not NXDOMAIN, says the question never got an answer: suspect the network path or the resolver’s health before the record. Overloaded CoreDNS replicas, too few of them, or UDP conntrack races on the node cause intermittent five-second stalls, glibc’s default resolver timeout. NodeLocal DNSCache runs a caching agent on every node as a DaemonSet, so most queries never cross the network and the agent can reach CoreDNS over TCP, which avoids the UDP conntrack races. The loop plugin stops CoreDNS when its upstream points back at itself, a common result of a node whose resolv.conf names 127.0.0.53.

Take DNS away from the whole cluster rather than one pod. Scale CoreDNS to zero or delete its pods, then run the first lookups on this page again: they time out until CoreDNS pods are ready, and after a scale to zero nothing brings them back until you scale it up.

Break the cluster's DNS

Each button runs its command on the cluster. This line follows what happens next.

Follow a lookup on your cluster

  1. kubectl --context kind-kx run lookup --image=nicolaka/netshoot:v0.16 --command -- sleep infinity
  2. kubectl --context kind-kx wait --for=condition=Ready pod/lookup --timeout=60s
  3. kubectl --context kind-kx exec lookup -- cat /etc/resolv.conf
  4. kubectl --context kind-kx exec lookup -- nslookup kubernetes.default
  5. kubectl --context kind-kx exec lookup -- nslookup example.com
  6. kubectl --context kind-kx -n kube-system get configmap coredns -o jsonpath='{.data.Corefile}'
  7. kubectl --context kind-kx delete pod lookup

For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. The cluster name answers from CoreDNS directly; example.com comes back as a non-authoritative answer forwarded from the node's upstream resolver.

Actual state

Check your understanding

6 checks

Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.

  1. Check 1 of 6

    A pod in namespace shop with the default DNS settings looks up api. Which name does its resolver ask for first?

  2. Check 2 of 6

    With ndots:5 and the three default search domains, how many names does a pod try for example.com before it asks for example.com. itself?

  3. Check 3 of 6

    A Service exposes port: 80 with no name. A client looks up _http._tcp.web.default.svc.cluster.local. What does it get?

  4. Check 4 of 6

    An egress NetworkPolicy allows UDP 53 to CoreDNS and nothing else. Most lookups work, but a few large answers fail. Why?

  5. Check 5 of 6

    A pod looked up reports.example.com and got NXDOMAIN. Seconds later someone created the record in the public zone, yet the pod's lookups keep failing for several minutes. Why?

  6. Check 6 of 6

    Check runs your command on a private cluster; what it prints shows here.

Lesson not yet reconciled

Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met

  • Trace a lookup from a pod's resolv.conf through the cluster resolver to an authoritative server. (not yet met)
  • Pick the record type (A, AAAA, CNAME, SRV, PTR) a cluster or external use needs. (not yet met)
  • Explain how search domains and ndots turn one short name into several queries. (not yet met)
  • Reason about TTLs and negative caching when a record changes. (not yet met)

Pass every check above to reconcile this lesson.

Every check has passed, so the desired state is met. First-try accuracy: 0 of 6. This lesson's review cards are in your queue.

Changed since the notes

This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.

Show 9 corrections
  1. Nuance, true with conditionsDNS Fundamentals

    The notes saidAlias records don't incur additional query charges (also flagged: presents alias queries as generally free)

    CorrectedRoute 53 does not charge for alias queries whose target is an AWS resource (ELB, CloudFront, S3 website endpoint, API Gateway, VPC interface endpoint, Global Accelerator, App Runner, Elastic Beanstalk, OpenSearch Service, AppSync). An alias that targets another record in the same hosted zone is billed like a normal query.

    Sourcedocs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-choosing-alias-non-alias.html

  2. Stale, true once, no longerDNS Record Types

    The notes saidReverse DNS for Elastic IPs is requested through AWS Support

    CorrectedSelf-service: EC2 console (Elastic IPs > Actions > Update reverse DNS) or aws ec2 modify-address-attribute --allocation-id <id> --domain-name <name>. A forward A record to the EIP must exist first, and the EIP is locked to the account while a PTR is set. Only GovCloud (US) still requires a support case.

    Sourcedocs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Elastic_Addressing_Reverse_DNS.html

  3. Stale, true once, no longerDNS Record Types

    The notes saidCAA record … relatively newer record type

    CorrectedCAA was defined in RFC 6844 (January 2013). CA/Browser Forum Ballot 187 made CAA checking mandatory for public CAs from 2017-09-08. RFC 8659 (November 2019) obsoletes RFC 6844.

    Sourcecabforum.org/2017/03/08/ballot-187-make-caa-checking-mandatory

  4. Nuance, true with conditionsDNS Record Types

    The notes saidAlias targets: ELB, CloudFront, S3 website, same-zone records, API Gateway, VPC interface endpoints, Global Accelerator

    CorrectedThe list is incomplete. Current alias targets also include App Runner services, Elastic Beanstalk environments, OpenSearch Service custom domains and AppSync domain names.

    Sourcedocs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-choosing-alias-non-alias.html

  5. Wrong, not trueDNS Record Types

    The notes saidAlias pricing: 'you pay for the alias record query, not for resolving the target'

    CorrectedAlias queries to AWS-resource targets are free. Only aliases to another Route 53 record are billed.

    Sourcedocs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-choosing-alias-non-alias.html

  6. Wrong, not trueDNS Record Types

    The notes saidPods can query SRV records to get hostnames and ports of individual pods

    CorrectedSRV records exist only for named Service ports (port-name.proto.svc.ns.svc.cluster.local). For a normal Service the target is the Service's own name. Only a headless Service returns one answer per backing Pod (hostname.svc.ns.svc.cluster.local).

    Sourcekubernetes.io/docs/concepts/services-networking/dns-pod-service

  7. Stale, true once, no longerAWS Route 53

    The notes saidGeoproximity routing is only available with Route 53 Traffic Flow (two entries)

    CorrectedSince 2024-01-10 geoproximity is a standard routing policy for records in public and private hosted zones, available through the console, API, CLI and SDK. Only the bias map visualization is limited to Traffic Flow.

    Sourceaws.amazon.com/about-aws/whats-new/2024/01/amazon-route-53-expands-geoproximity-routing

  8. Nuance, true with conditionsAWS Route 53

    The notes saidMultivalue: up to eight healthy records; Route 53 returns all healthy values (two entries)

    CorrectedYou can create more than eight multivalue records. Route 53 answers each query with up to eight healthy records and gives different answers to different resolvers. With eight or fewer healthy records it returns all of them. If every record is unhealthy it returns up to eight unhealthy records.

    Sourcedocs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy-multivalue.html

  9. Stale, true once, no longerDNS Security and Operations

    The notes saidsystemd-resolve --flush-caches on Linux with systemd

    Correctedsystemd 239 renamed the tool to resolvectl. Use resolvectl flush-caches. The systemd-resolve name survives only as a compatibility name on older installs.

    Sourceman7.org/linux/man-pages/man1/resolvectl.1.html

Every correction across the guide, by note and topic.

Sources

Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .

  1. kubernetes.io/docs/concepts/services-networking/dns-pod-service
  2. kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution
  3. kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers
  4. kubernetes.io/docs/tasks/administer-cluster/nodelocaldns
  5. coredns.io/plugins/kubernetes
  6. coredns.io/plugins/cache
  7. coredns.io/plugins/forward
  8. man7.org/linux/man-pages/man5/resolv.conf.5.html
  9. rfc-editor.org/rfc/rfc2308
  10. rfc-editor.org/rfc/rfc7766
  11. docs.aws.amazon.com/vpc/latest/userguide/AmazonDNS-concepts.html
  12. docs.aws.amazon.com/Route53/latest/DeveloperGuide/resolver.html
  13. docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zones-private.html
  14. docs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-choosing-alias-non-alias.html
  15. docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Elastic_Addressing_Reverse_DNS.html
  16. docs.aws.amazon.com/eks/latest/userguide/community-addons.html

DNS in Kubernetes terms

↓ ↑ step, 1 to 6 scene, Esc leave

kind cluster kxLast command: Nothing run yet