cheat-sheets

Kubernetes DNS

Cluster DNS is served by CoreDNS in the kube-system namespace (Kubernetes, K3S).

Names Inside the Cluster #

NAMERESOLVES TO
<SERVICE>Service in the same namespace
<SERVICE>.<NAMESPACE>Service in another namespace
<SERVICE>.<NAMESPACE>.svc.cluster.localFully qualified service name
<POD-IP-DASHED>.<NAMESPACE>.pod.cluster.localA Pod by IP (e.g. 10-1-2-3)
<POD>.<HEADLESS-SVC>.<NAMESPACE>.svc.cluster.localStatefulSet Pod via a headless Service

Debugging #

COMMANDDESCRIPTION
kubectl run dns-test --rm -it --image=busybox:1.36 --restart=Never -- nslookup kubernetes.defaultTest cluster DNS from a Pod
kubectl run dns-test --rm -it --image=nicolaka/netshoot --restart=Never -- dig +short <SERVICE>.<NS>.svc.cluster.localQuery with dig
kubectl -n kube-system get pods -l k8s-app=kube-dnsCoreDNS pods
kubectl -n kube-system logs -l k8s-app=kube-dnsCoreDNS logs
kubectl -n kube-system get svc kube-dnsThe cluster DNS IP
kubectl exec <POD> -- cat /etc/resolv.confWhat a Pod uses (ndots:5 is the default)
kubectl -n kube-system rollout restart deployment corednsRestart CoreDNS

Custom DNS Settings #

Edit coredns config map #

Add entry to the Corefile: | section of the configmap/coredns in section kube-system.

yml
.:53 {
  # ...
}
import /etc/coredns/custom/*.server

Add new config map #

Example for local DNS server using the clcreative.home zone.

yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  clcreative.server: |
    clcreative.home:53 {
      forward . 10.20.0.10
    }

This coredns-custom ConfigMap and the import line work out of the box on K3s. Restart CoreDNS afterwards.

Static host records #

yaml
data:
  hosts.override: |
    hosts {
      10.0.0.5 nas.home
      fallthrough
    }

Per-Pod DNS #

yaml
spec:
  dnsPolicy: ClusterFirst        # default
  dnsConfig:
    options:
      - name: ndots
        value: "2"               # fewer search-domain lookups for external names

Other policies: Default (node resolver), None (use only dnsConfig), ClusterFirstWithHostNet.

Common Problems #

SYMPTOMCAUSE
External names slow or time outndots:5 search expansion, UDP drops. Use FQDNs with a trailing dot, or lower ndots
Names fail on some nodes onlyCNI or node firewall blocking UDP/TCP 53 between nodes
Split-DNS names failForward the zone in CoreDNS as shown above
SERVFAIL on external lookupsUpstream resolver unreachable from the node (/etc/resolv.conf on the node)
Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.