kubernetes kubernetes/networking
CoreDNS: DNS and Service Discovery
DNS for Services and Pods | Kubernetes
CoreDNS
ExternalDNS
Core Idea
CoreDNS is Kubernetes’ default DNS: it resolves queries for services and pods so components reach each other by name instead of IP, and you tune it through its ConfigMap.
- Role and features of CoreDNS in Kubernetes, plus its architecture.
- The CoreDNS ConfigMap is where the server’s behavior is defined.
- DNS query flow, benefits, and a CoreDNS versus kube-dns comparison.
CoreDNS is a highly flexible and extensible DNS server that serves as the default DNS provider in Kubernetes. It is responsible for resolving DNS queries for services and pods within a Kubernetes cluster, enabling seamless communication between components using DNS names rather than IP addresses.
Role of CoreDNS in Kubernetes
CoreDNS runs as a DNS service inside the cluster and provides name resolution for:
- Cluster Services: Resolves DNS names for Kubernetes services (e.g.,
my-service.my-namespace.svc.cluster.local). - Pod DNS: Resolves pod IP addresses using their DNS names, depending on the DNS policy.
- External Domains: Forwards DNS queries for external domains (e.g.,
google.com) to upstream DNS servers (like your cloud provider’s DNS).
CoreDNS Features
- Service Discovery:
- Resolves DNS names for Kubernetes services and endpoints.
- Example:
my-service.my-namespace.svc.cluster.local
- Resolves to the ClusterIP or a specific pod behind the service.
- Pod DNS:
- Pods can communicate using DNS names rather than hardcoding IPs.
- Example:
my-pod.my-namespace.pod.cluster.local
- DNS Forwarding:
- Queries for external domains are forwarded to upstream DNS servers.
- Custom Configurations:
- CoreDNS can be extended using plugins for advanced functionality (e.g., caching, filtering, or logging).
- Lightweight and Modular:
- CoreDNS is written in Go and uses a plugin-based architecture, allowing it to be tailored to specific use cases.
CoreDNS Architecture
CoreDNS operates as a pod in the Kubernetes cluster. It uses a ConfigMap to define its configuration.
Example CoreDNS Deployment
- CoreDNS runs as a Deployment with two or more replicas for redundancy.
- Pods in the cluster use the kube-dns service to forward DNS queries to CoreDNS.
CoreDNS ConfigMap
The behavior of CoreDNS is configured using a ConfigMap in the kube-system namespace.
Example ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
fallthrough
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}👉 ConfigMaps & Secrets > ConfigMaps
DNS Query Flow in Kubernetes
- A pod makes a DNS query (e.g.,
my-service.my-namespace.svc.cluster.local). - The query is sent to the kube-dns service.
- CoreDNS receives the query and:
- Resolves the DNS name if it matches a Kubernetes service or pod.
- Forwards the query to an upstream DNS server if it’s for an external domain.
Benefits of CoreDNS
- Flexibility: Highly customizable using plugins.
- Efficiency: Lightweight and fast with built-in caching.
- Extensibility: Plugins allow advanced DNS functionalities.
- Built for Kubernetes: Designed to integrate seamlessly with Kubernetes for service discovery.
Comparison: CoreDNS vs kube-dns
| Feature | CoreDNS | kube-dns |
|---|---|---|
| Architecture | Plugin-based, modular | Fixed, monolithic |
| Performance | Better performance, uses caching efficiently | Slower in large clusters |
| Customization | Highly customizable via plugins | Limited customization |
| Resource Usage | Lightweight | Higher resource usage |
| Default in K8s | Since Kubernetes 1.13 | Before Kubernetes 1.11 |