0
0
Kubernetesdevops~5 mins

DNS in Kubernetes (CoreDNS) - Commands & Configuration

Choose your learning style9 modes available
Introduction
Kubernetes uses DNS to let pods find each other by name instead of IP addresses. CoreDNS is the default DNS service in Kubernetes that answers these name requests inside the cluster.
When your application pods need to communicate using service names instead of IPs.
When you want to access a pod or service by a friendly name inside the cluster.
When you deploy multiple services and want automatic name resolution without manual IP tracking.
When you want to debug DNS issues inside your Kubernetes cluster.
When you need to customize DNS behavior for your cluster using CoreDNS plugins.
Config File - coredns.yaml
coredns.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health
        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
        cache 30
        loop
        reload
        loadbalance
    }

This ConfigMap defines the CoreDNS configuration for the Kubernetes cluster.

Corefile is the main configuration file for CoreDNS.

  • .:53 means CoreDNS listens on port 53 for all domains.
  • kubernetes cluster.local in-addr.arpa ip6.arpa enables DNS for Kubernetes services and pods.
  • forward . /etc/resolv.conf forwards DNS queries CoreDNS can't answer to the upstream DNS servers.
  • Other plugins like cache, health, and loadbalance improve performance and reliability.
Commands
This command applies the CoreDNS ConfigMap to the kube-system namespace to configure DNS inside the cluster.
Terminal
kubectl apply -f coredns.yaml -n kube-system
Expected OutputExpected
configmap/coredns configured
-f - Specifies the file to apply
-n - Specifies the namespace where CoreDNS runs
This command lists the CoreDNS pods to check if they are running after applying the configuration.
Terminal
kubectl get pods -n kube-system -l k8s-app=kube-dns
Expected OutputExpected
NAME READY STATUS RESTARTS AGE coredns-558bd4d5db-abcde 1/1 Running 0 2m
-n - Namespace to look in
-l - Label selector to filter CoreDNS pods
This command runs nslookup inside a pod to test if DNS resolves the Kubernetes service name correctly.
Terminal
kubectl exec -n default -it busybox -- nslookup kubernetes.default
Expected OutputExpected
Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: kubernetes.default.svc.cluster.local Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
-n - Namespace where the pod runs
-it - Interactive terminal to run commands inside the pod
Key Concept

If you remember nothing else from this pattern, remember: CoreDNS runs inside Kubernetes to translate service and pod names into IP addresses automatically.

Common Mistakes
Not applying the CoreDNS ConfigMap in the kube-system namespace
CoreDNS only works when configured in the kube-system namespace where it runs.
Always use '-n kube-system' when applying or checking CoreDNS configurations.
Trying to resolve service names from outside the cluster without proper DNS forwarding
CoreDNS only resolves names inside the cluster; external DNS needs separate setup.
Test DNS resolution from inside a pod using commands like nslookup or dig.
Forgetting to check CoreDNS pod status after configuration changes
If CoreDNS pods are not running or crash, DNS resolution will fail.
Always verify CoreDNS pods are running with 'kubectl get pods -n kube-system -l k8s-app=kube-dns'.
Summary
Apply the CoreDNS ConfigMap in the kube-system namespace to configure cluster DNS.
Check CoreDNS pods are running to ensure DNS service is active.
Test DNS resolution inside a pod using nslookup to confirm service names resolve correctly.