๐Ÿ” Ever Wonder How Pods in a Kubernetes Cluster Seamlessly Communicate via DNS?

How Internal DNS Works in Kubernetes (And How to Add Custom Records with CoreDNS) Ever Wonder How Pods in a Kubernetes Cluster Seamlessly Communicate via DNS?

When working with Kubernetes, one of the most magical experiences is watching how pods and services can easily discover and talk to each other — often with just a name like my-service.default.svc.cluster.local.

But behind this simplicity is a powerful and flexible system built into Kubernetes: CoreDNS.

๐Ÿ“ฆ What is CoreDNS?

CoreDNS is the default DNS server for Kubernetes clusters. It acts as a DNS server that runs inside the cluster and handles service discovery. Instead of hardcoding IPs or deploying your own DNS infrastructure, CoreDNS dynamically registers and resolves names based on the Kubernetes API.

Key Responsibilities of CoreDNS:

  • Resolving internal service names to ClusterIP addresses
  • Allowing pods in one namespace to discover services in another
  • Forwarding unknown DNS queries to external DNS servers (e.g., Google)
  • Supporting custom or static DNS entries

๐Ÿงช Real-World Example: How DNS Resolves Inside Kubernetes

Imagine you have a deployment with a service named orders-api in the default namespace. From another pod, you can connect using:

http://orders-api.default.svc.cluster.local

Or even simply:

http://orders-api

...if you're calling from the same namespace. Kubernetes automatically builds a search domain list so these names are resolved step-by-step.

▶️ How to Test DNS Resolution:

kubectl run dns-test --image=busybox:1.28 --rm -it --restart=Never -- /bin/sh
nslookup orders-api.default.svc.cluster.local

๐Ÿง  How CoreDNS Works Behind the Scenes

  1. A pod sends a DNS query (e.g., for orders-api.default.svc.cluster.local).
  2. CoreDNS intercepts the query inside the cluster.
  3. It communicates with the Kubernetes API to retrieve the service's ClusterIP.
  4. The response is sent back to the requesting pod via kubelet.

All this happens in milliseconds, allowing pods to communicate as if they’re on the same network — regardless of node distribution.

๐Ÿ“ Where Is the CoreDNS Configuration?

CoreDNS runs as a deployment inside the kube-system namespace. Its configuration is stored in a ConfigMap:

kubectl -n kube-system edit configmap coredns

๐ŸŒ How to Add Custom DNS Entries (Internal or External)

You might want to add custom entries to:

  • Resolve internal services not managed by Kubernetes (e.g., legacy VMs)
  • Override external domain names for testing or isolation
  • Create shortcut names for commonly accessed IPs

✅ Example: Adding Static DNS for Internal Service

Add this section inside your Corefile:

hosts {
    10.100.1.100  internal-db.local
    fallthrough
}

Now, any pod can reach internal-db.local and be routed to 10.100.1.100.

๐ŸŒ Example: Forwarding All External DNS to Google

Add or update this block in your Corefile:

forward . 8.8.8.8 8.8.4.4

This ensures that any external domain (like example.com) is resolved using Google's public DNS.

๐Ÿ” Apply the Changes

After modifying the coredns ConfigMap, restart the deployment to apply changes:

kubectl -n kube-system rollout restart deployment coredns

✅ Tips for Troubleshooting Kubernetes DNS

  • Use nslookup or dig from inside a pod to test resolution.
  • Check that the coredns pods are running and not in CrashLoopBackOff.
  • Inspect logs: kubectl logs -n kube-system -l k8s-app=kube-dns
  • Make sure your pods use the correct DNS server in /etc/resolv.conf

๐Ÿ“Œ TL;DR

  • Kubernetes provides built-in DNS through CoreDNS for seamless service discovery.
  • You can customize DNS using hosts and forward plugins in the CoreDNS ConfigMap.
  • Changes are effective after restarting the CoreDNS deployment.

๐Ÿ“Ž Useful Resources

Have you ever debugged a DNS issue in your cluster? Or added a custom DNS entry in CoreDNS? Share your experience in the comments or drop me a message — always happy to learn from each other!

Comments

Popular posts from this blog

๐Ÿ” Why You Only See a Private IP Inside an AWS EC2 Instance — Even If It Has a Public IP

A Day in the Life of a DevOps Engineer – Roles & Responsibilities

Docker and Its Usage in DevOps – A Complete Guide