Core DNS in kubernetes

Mohammed IliyasMohammed Iliyas
5 min read

CoreDNS is a DNS (Domain Name System) server that is designed to be used in containerized environments, such as Kubernetes. It is a flexible and extensible DNS server that can be used to provide DNS services for a variety of use cases, including Kubernetes clusters. In this explanation, we will delve into the details of CoreDNS in Kubernetes, including its components, how it works, and how it is used in Kubernetes, along with examples and step-by-step explanations.

What is CoreDNS?

CoreDNS is a DNS server that is written in Go and is designed to be highly extensible and flexible. It is a popular choice for use in Kubernetes clusters because of its simplicity, scalability, and ease of use. CoreDNS is a CNCF (Cloud Native Computing Foundation) project, which means it is part of the same foundation that manages Kubernetes.

Components of CoreDNS

CoreDNS consists of several components that work together to provide DNS services:

  1. CoreDNS Server: This is the main DNS server that handles DNS queries and responses. It is responsible for resolving domain names to IP addresses.

  2. CoreDNS Plugins: These are extensions to the CoreDNS server that provide additional functionality. There are many plugins available, including plugins for Kubernetes, etcd, and more.

  3. CoreDNS Configuration: This is the configuration file that defines how CoreDNS should behave. It specifies the plugins to use, the zones to serve, and other settings.

How CoreDNS Works in Kubernetes

In a Kubernetes cluster, CoreDNS is typically deployed as a Deployment or a DaemonSet. This means that CoreDNS runs as a pod in the cluster, and it is responsible for providing DNS services to the other pods in the cluster.

Here's how it works:

  1. Pods Register with CoreDNS: When a pod is created in the cluster, it registers its hostname and IP address with CoreDNS. This is done using the Kubernetes DNS policy.

  2. CoreDNS Serves DNS Queries: When a pod in the cluster makes a DNS query (e.g., to resolve a domain name to an IP address), the query is sent to CoreDNS.

  3. CoreDNS Resolves the Query: CoreDNS uses its plugins and configuration to resolve the query. For example, if the query is for a service in the cluster, CoreDNS will use the Kubernetes plugin to resolve the query to the IP address of the service.

  4. CoreDNS Returns the Response: Once CoreDNS has resolved the query, it returns the response to the pod that made the query.

CoreDNS in Kubernetes: Step-by-Step Explanation

Step 1: Deploying CoreDNS

To deploy CoreDNS in a Kubernetes cluster, you can use the following command:

kubectl apply -f https://github.com/coredns/deployment/raw/master/kubernetes/deploy.sh

This command will deploy CoreDNS as a Deployment in the kube-system namespace.

Step 2: Verifying CoreDNS

To verify that CoreDNS is running, you can use the following command:

kubectl get pods -n kube-system | grep coredns

This command will show you the pods running CoreDNS in the kube-system namespace.

Step 3: Configuring CoreDNS

CoreDNS is configured using a configuration file. The default configuration file is stored in a ConfigMap named coredns in the kube-system namespace. You can view the configuration file using the following command:

kubectl get configmap coredns -n kube-system -o yaml

This command will show you the current configuration of CoreDNS.

Step 4: Creating a Service

To demonstrate how CoreDNS works, let's create a simple service in the cluster. First, create a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-service
  template:
    metadata:
      labels:
        app: my-service
    spec:
      containers:
      - name: my-service
        image: nginx:latest
        ports:
        - containerPort: 80

Apply the Deployment:

kubectl apply -f deployment.yaml

Next, create a Service:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-service
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: ClusterIP

Apply the Service:

kubectl apply -f service.yaml

Step 5: Resolving the Service with CoreDNS

Now, let's see how CoreDNS resolves the service. First, get the IP address of the Service:

kubectl get svc my-service

This command will show you the IP address of the Service. Let's say the IP address is 10.96.0.10.

Next, let's use CoreDNS to resolve the Service:

kubectl exec -it <coredns_pod_name> -- /coredns -dns.port=53 -conf /etc/coredns/Corefile

Replace <coredns_pod_name> with the name of a CoreDNS pod. This command will start an interactive CoreDNS shell.

In the CoreDNS shell, you can resolve the Service using the following command:

dig +short my-service.default.svc.cluster.local

This command will show you the IP address of the Service, which should match the IP address you got in the previous step.

CoreDNS Plugins

CoreDNS has many plugins that can be used to extend its functionality. Some common plugins include:

  1. Kubernetes Plugin: This plugin allows CoreDNS to resolve Kubernetes services and pods.

  2. Etcd Plugin: This plugin allows CoreDNS to store its configuration in etcd.

  3. Prometheus Plugin: This plugin allows CoreDNS to expose metrics to Prometheus.

CoreDNS Configuration

CoreDNS is configured using a configuration file called the Corefile. The Corefile specifies the plugins to use, the zones to serve, and other settings. Here's an example Corefile:

.:53 {
    errors
    health {
       lameduck 5s
    }
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
       pods insecure
       upstream
       fallthrough in-addr.arpa ip6.arpa
    }
    prometheus :9153
    forward . /etc/resolv.conf
    cache 30
    loop
    reload
    loadbalance
}

This Corefile specifies several things:

  1. Errors: This plugin logs errors to the console.

  2. Health: This plugin provides health checking for CoreDNS.

  3. Kubernetes: This plugin enables Kubernetes support.

  4. Prometheus: This plugin exposes metrics to Prometheus.

  5. Forward: This plugin forwards queries to the upstream DNS server specified in /etc/resolv.conf.

  6. Cache: This plugin enables caching of DNS responses.

  7. Loop: This plugin prevents CoreDNS from serving its own IP address.

  8. Reload: This plugin enables reloading of the CoreDNS configuration.

  9. Loadbalance: This plugin enables load balancing of DNS responses.

Conclusion

CoreDNS is a powerful and flexible DNS server that is well-suited for use in Kubernetes clusters. It provides a scalable and extensible way to manage DNS services in the cluster. By understanding how CoreDNS works and how it is used in Kubernetes, you can better manage your Kubernetes cluster and ensure that your applications can communicate with each other effectively.

10
Subscribe to my newsletter

Read articles from Mohammed Iliyas directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohammed Iliyas
Mohammed Iliyas

As a seasoned DevOps engineer, I bring a comprehensive set of skills to the table, encompassing version control with Git and GitOps using ArgoCD, containerization with Docker, and orchestration with Kubernetes, Helm, and Istio. I'm proficient in infrastructure provisioning and management using Terraform and Ansible, and have expertise in setting up and managing CI/CD pipelines with Jenkins, Azure Pipelines, and AWS CodePipeline. With extensive experience in cloud platforms, including Azure and AWS, I've deployed and managed applications on Azure Kubernetes Service (AKS) and AWS Elastic Container Service (ECS) and Elastic Container Service for Kubernetes (EKS). Additionally, I've integrated security practices into DevOps pipelines, ensuring secure and compliant software development and deployment. My technical prowess extends to Bash shell scripting, Linux system administration, and programming in Golang. Throughout my journey, I've developed a unique blend of skills that enable me to streamline development, deployment, and management of applications across multiple environments.