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:

  1. Cluster Services: Resolves DNS names for Kubernetes services (e.g., my-service.my-namespace.svc.cluster.local).
  2. Pod DNS: Resolves pod IP addresses using their DNS names, depending on the DNS policy.
  3. External Domains: Forwards DNS queries for external domains (e.g., google.com) to upstream DNS servers (like your cloud provider’s DNS).

CoreDNS Features

  1. 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.
  1. Pod DNS:
    • Pods can communicate using DNS names rather than hardcoding IPs.
    • Example:
my-pod.my-namespace.pod.cluster.local
  1. DNS Forwarding:
    • Queries for external domains are forwarded to upstream DNS servers.
  2. Custom Configurations:
    • CoreDNS can be extended using plugins for advanced functionality (e.g., caching, filtering, or logging).
  3. 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

  1. A pod makes a DNS query (e.g., my-service.my-namespace.svc.cluster.local).
  2. The query is sent to the kube-dns service.
  3. 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

  1. Flexibility: Highly customizable using plugins.
  2. Efficiency: Lightweight and fast with built-in caching.
  3. Extensibility: Plugins allow advanced DNS functionalities.
  4. Built for Kubernetes: Designed to integrate seamlessly with Kubernetes for service discovery.

Comparison: CoreDNS vs kube-dns

FeatureCoreDNSkube-dns
ArchitecturePlugin-based, modularFixed, monolithic
PerformanceBetter performance, uses caching efficientlySlower in large clusters
CustomizationHighly customizable via pluginsLimited customization
Resource UsageLightweightHigher resource usage
Default in K8sSince Kubernetes 1.13Before Kubernetes 1.11