CoreDNS Configuration
Introduction
This tutorial demonstrates how to configure CoreDNS, the default DNS server in Kubernetes, to resolve custom domain names within the cluster. You will learn how to modify the CoreDNS ConfigMap to add custom DNS entries and verify that they resolve correctly from within your pods.
Prerequisites:
- A running Kubernetes cluster (Minikube, Kind, or a cloud provider’s Kubernetes service).
kubectlcommand-line tool configured to interact with your cluster.
Task 1: Inspecting the Default CoreDNS Configuration
First, let’s examine the default CoreDNS configuration. This will give us a baseline to understand what we’re changing.
-
Get the CoreDNS ConfigMap:
NODE_TYPE // bashkubectl get configmap coredns -n kube-system -o yaml -
Examine the output. Look for the
Corefilekey in thedatasection. This contains the CoreDNS configuration. You will see entries likekubernetes,prometheus, anderrorsplugins.NODE_TYPE // yamlapiVersion: v1 data: Corefile: | .:53 { errors health { lameduck } 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 loop reload loadbalance } kind: ConfigMap metadata: name: coredns namespace: kube-systemTheCorefileis the primary configuration file for CoreDNS. It defines how CoreDNS handles DNS queries.
Task 2: Creating a Custom DNS Entry
Now, let’s add a custom DNS entry to the CoreDNS configuration. We’ll create an entry for example.local that resolves to the IP address 192.168.1.100.
-
Edit the CoreDNS ConfigMap:
NODE_TYPE // bashkubectl edit configmap coredns -n kube-system -
In the
Corefilesection, add a new zone forexample.localbefore the existing.zone:NODE_TYPE // yamlapiVersion: v1 data: Corefile: | example.local:53 { errors forward . 8.8.8.8 8.8.4.4 } .:53 { errors health { lameduck } 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 loop reload loadbalance } kind: ConfigMap metadata: name: coredns namespace: kube-systemBe careful when editing the ConfigMap. Incorrect syntax can break DNS resolution in your cluster.Theforward . 8.8.8.8 8.8.4.4line tells CoreDNS to forward any requests for domains outsideexample.localto Google’s public DNS servers.
Task 3: Applying a Static DNS Record
Now let’s configure CoreDNS to return a static IP address for our example.local domain.
-
Edit the CoreDNS ConfigMap:
NODE_TYPE // bashkubectl edit configmap coredns -n kube-system -
Modify the
Corefilesection to include ahostsblock. Replace the forwarding entry with a static IP record:NODE_TYPE // yamlapiVersion: v1 data: Corefile: | example.local:53 { errors hosts { 192.168.1.100 example.local fallthrough } } .:53 { errors health { lameduck } 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 loop reload loadbalance } kind: ConfigMap metadata: name: coredns namespace: kube-systemThehostsplugin allows you to define static DNS records within CoreDNS. Thefallthroughkeyword ensures that if a record is not found in thehostsfile, CoreDNS will continue to the next plugin in the chain.
Task 4: Restarting CoreDNS Deployments
Kubernetes will automatically rollout the changes to the CoreDNS pods.
-
To expedite the rollout, you can manually restart the CoreDNS deployments.
NODE_TYPE // bashkubectl rollout restart deployment coredns -n kube-system
Task 5: Verifying the DNS Resolution
Now, let’s verify that our custom DNS entry resolves correctly from within a pod in the cluster.
-
Create a test pod:
NODE_TYPE // yamlapiVersion: v1 kind: Pod metadata: name: dns-test spec: containers: - name: busybox image: busybox:latest command: - sleep - "3600" imagePullPolicy: IfNotPresent restartPolicy: AlwaysSave this as
dns-test.yamland apply it:NODE_TYPE // bashkubectl apply -f dns-test.yaml -
Get a shell into the test pod:
NODE_TYPE // bashkubectl exec -it dns-test -- sh -
Use
nslookuporpingto resolveexample.local:NODE_TYPE // bashnslookup example.localor
NODE_TYPE // bashping example.local -
Expected output (using
nslookup):NODE_TYPE // outputServer: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: example.local Address 1: 192.168.1.100If the output shows that
example.localresolves to192.168.1.100, then your configuration is working correctly.Ifnslookupis not available in the busybox image, you can useping -c 1 example.localand check the IP address in the output.
Conclusion
In this tutorial, you learned how to configure CoreDNS in Kubernetes to resolve custom domain names. You modified the CoreDNS ConfigMap to add a static DNS entry and verified that it resolves correctly from within a pod. This is a fundamental skill for managing internal DNS resolution in Kubernetes clusters.