๐ 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
- A pod sends a DNS query (e.g., for
orders-api.default.svc.cluster.local). - CoreDNS intercepts the query inside the cluster.
- It communicates with the Kubernetes API to retrieve the service's ClusterIP.
- 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
nslookupordigfrom inside a pod to test resolution. - Check that the
corednspods 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
hostsandforwardplugins 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
Post a Comment