LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Kubernetes

Local Domain Names and CoreDNS

In the dynamic world of Kubernetes, Pods are born and destroyed constantly. Every time a new Pod spins up, it gets a brand-new IP address. If you hardcode these IP addresses into your applications, they will break the moment a Pod restarts. Kubernetes solves this chaotic environment using a built-in Domain Name System (DNS) powered by CoreDNS.

Local Domain Names and CoreDNS in Kubernetes

In the dynamic world of Kubernetes, Pods are born and destroyed constantly. Every time a new Pod spins up, it gets a brand-new IP address. If you hardcode these IP addresses into your applications, they will break the moment a Pod restarts. Kubernetes solves this chaotic environment using a built-in Domain Name System (DNS) powered by CoreDNS.

Instead of relying on fragile IP addresses, microservices discover each other using predictable, human-readable local domain names. Understanding how CoreDNS resolves these names is crucial for deploying reliable, interconnected applications.

What you will learn
  • How CoreDNS automatically assigns domain names to Services and Pods.
  • The structure of a Kubernetes Fully Qualified Domain Name (FQDN).
  • How to configure applications to seamlessly route traffic between namespaces.
  • How the resolv.conf file works inside a container.

1. The Role of CoreDNS

By default, every Kubernetes cluster comes with a DNS server called CoreDNS. It runs as a set of Pods in the kube-system namespace. Whenever you create a new Kubernetes Service, CoreDNS automatically generates a DNS record for it.

When your frontend application needs to talk to the backend, it simply asks CoreDNS to resolve the name "backend". CoreDNS intercepts this query, looks up the corresponding Service IP, and hands it back to the frontend.

Resource Type FQDN Structure Example Record
Standard Service <service>.<namespace>.svc.cluster.local auth-api.default.svc.cluster.local
Headless Service <pod-name>.<service>.<namespace>.svc.cluster.local db-0.database.prod.svc.cluster.local
Pod (Direct) <pod-ip>.<namespace>.pod.cluster.local 10-244-1-5.default.pod.cluster.local

2. Practical Implementation & Code

Let's see this in action. If you have two microservices—a web frontend and a backend API—you should never instruct the frontend to call http://10.96.4.5. Instead, you create a Kubernetes Service for the backend, and configure your frontend to use the local domain name.

Creating the Backend Service

Example 1: backend-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: backend-api
  namespace: finance
spec:
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

How to call it from a Pod in the same namespace

curl http://backend-api

How to call it from a Pod in a different namespace

curl http://backend-api.finance.svc.cluster.local
Important: If two Pods are in the same namespace, they can discover each other using just the short service name (e.g., backend-api). If they are in different namespaces, you must use the Fully Qualified Domain Name (FQDN).

3. Architecture / Workflow Visualized

To understand how this magic works, we need to look at the network flow when one Pod makes a request to another.

Kubernetes DNS Resolution Flow

CoreDNS Server Frontend Pod Backend Service 1. Query "backend-api" 2. Response: 10.96.5.5 3. HTTP GET to 10.96.5.5

The Frontend Pod transparently queries CoreDNS to resolve the service name into a ClusterIP before initiating the HTTP connection.

4. Step-by-Step Execution Breakdown

When you start a Pod, Kubernetes automatically injects specific DNS configuration into the container's /etc/resolv.conf file. Let's look at how the resolution happens.

Step 1: The Request Starts: The frontend application makes a network request to http://backend-api. Because this isn't an IP address, the container's operating system checks /etc/resolv.conf.
Step 2: Appending Search Domains: The resolv.conf file tells the OS to append search strings. It tries backend-api.finance.svc.cluster.local first.
Step 3: CoreDNS Lookup: The query is sent to the CoreDNS service IP (usually 10.96.0.10). CoreDNS matches the FQDN to its internal records and returns the ClusterIP of the backend-api Service.
Watch out: Kubernetes uses a setting called ndots:5 by default. This means if you search for a domain with fewer than 5 dots (like google.com), CoreDNS will try appending all local Kubernetes search domains first before finally resolving the external domain. This can cause unnecessary DNS load for external traffic.

5. Hands-On Practice

The best way to understand CoreDNS is to spin up a temporary testing pod and manually query the DNS server.

Beginner Exercise 1: Start a testing Pod

Deploy a simple busybox pod to run DNS commands inside your cluster.

Hint: Use kubectl run -i --tty busybox --image=busybox:1.28 --restart=Never -- sh.

Intermediate Exercise 2: Perform an nslookup

From inside your busybox shell, find the IP address of the default Kubernetes API service.

Expected result: Running nslookup kubernetes.default will output a valid ClusterIP.

Advanced Exercise 3: Inspect resolv.conf

View the DNS configuration injected into your container by the kubelet.

Hint: Run cat /etc/resolv.conf inside the pod and look at the `nameserver` and `search` lines.

6. Interview Questions

If you are interviewing for a DevOps, SRE, or Kubernetes engineering role, you will almost certainly face questions about cluster networking and DNS.

Q1. How does a Pod know where the CoreDNS server is located?

Answer: When the kubelet starts a Pod, it injects the ClusterIP of the CoreDNS service into the container's /etc/resolv.conf file under the nameserver directive. This ensures all outbound DNS queries from the container go straight to CoreDNS.

Q2. What is a "Headless Service" and how does it change DNS resolution?

Answer: A standard Service returns a single ClusterIP that load-balances across pods. A Headless Service (created by setting clusterIP: None) bypasses the load balancer. When you query its DNS name, CoreDNS returns a list of individual IP addresses for every active Pod behind that service. This is heavily used by StatefulSets like databases where clients need to connect to specific replicas.

Conclusion

Mastering local domain names in Kubernetes fundamentally changes how you design applications. By fully leveraging CoreDNS and the svc.cluster.local architecture, you abstract away the volatile nature of container IP addresses. This allows you to build highly decoupled, resilient microservices that communicate smoothly across namespaces without relying on brittle configurations.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.