What is CoreDNS in Kubernetes: Simple Explanation and Usage
CoreDNS is the default DNS server used inside a Kubernetes cluster to translate service names into IP addresses. It helps pods find each other by resolving names, acting like a phone book for services within the cluster.How It Works
Imagine a busy office where everyone needs to call each other by name but only knows phone numbers. CoreDNS acts like the office directory that matches names to phone numbers so calls connect correctly. In Kubernetes, pods and services communicate using names, and CoreDNS translates these names into IP addresses so the network can route the traffic.
CoreDNS runs as a small service inside the cluster and listens for DNS queries from pods. When a pod asks for the IP of a service name, CoreDNS looks up its internal records and replies with the correct IP. This process happens quickly and automatically, allowing dynamic service discovery as pods start, stop, or move.
Example
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}When to Use
Use CoreDNS whenever you run a Kubernetes cluster because it is the built-in DNS service that enables pods to find each other by name. It is essential for service discovery, which is a core part of how Kubernetes applications communicate.
CoreDNS is also useful when you want to customize DNS behavior inside your cluster, such as adding custom domain names, blocking certain domains, or forwarding queries differently. It supports plugins that let you extend its features easily.
Real-world use cases include:
- Resolving service names to IPs for microservices communication
- Custom DNS rules for internal applications
- Monitoring DNS queries with Prometheus integration
Key Points
- CoreDNS is the default DNS server in Kubernetes clusters.
- It translates service and pod names into IP addresses for communication.
- CoreDNS runs inside the cluster and handles DNS queries from pods.
- It is highly configurable with plugins and custom rules.
- Essential for service discovery and network communication in Kubernetes.