Updated on 2024-01-04 GMT+08:00

CoreDNS

Introduction

CoreDNS is a DNS server that provides domain name resolution for Kubernetes clusters through a chain add-on.

CoreDNS is an open-source software and has been a part of CNCF. It provides a means for cloud services to discover each other in cloud-native deployments. Each of the plugins chained by CoreDNS provides a particular DNS function. You can integrate CoreDNS with only the plugins you need to make it fast, efficient, and flexible. When used in a Kubernetes cluster, CoreDNS can automatically discover services in the cluster and provide domain name resolution for these services. By working with DNS server, CoreDNS can resolve external domain names for workloads in a cluster.

This add-on is installed by default during cluster creation.

Kubernetes backs CoreDNS as the official default DNS for all clusters going forward.

CoreDNS official website: https://coredns.io/

Open source community: https://github.com/coredns/coredns

For details, see DNS.

Constraints

To run CoreDNS properly or upgrade CoreDNS in a cluster, ensure the number of available nodes in the cluster is greater than or equal to the number of CoreDNS instances and all CoreDNS instances are running. Otherwise, the add-on will malfunction or the upgrade will fail.

Installing the Add-on

This add-on has been installed by default. If it is uninstalled due to some reasons, you can reinstall it by performing the following steps:

  1. Log in to the CCE console and click the cluster name to access the cluster console. Choose Add-ons in the navigation pane, locate CoreDNS on the right, and click Install.
  2. On the Install Add-on page, configure the specifications.

    Table 1 CoreDNS parameters

    Parameter

    Description

    Pods

    Number of pods for the add-on.

    High availability is not possible with a single add-on pod. If an error occurs on the node where the add-on instance runs, the add-on will fail.

    Containers

    Queries per second (QPS) of the CoreDNS add-on is positively correlated with the CPU consumption. If the number of nodes or containers in the cluster grows, the CoreDNS pods will bear heavier workloads. Adjust the number of the CoreDNS pods and their CPU and memory quotas based on the cluster scale. For details, see Table 2.

    Table 2 Recommended CoreDNS quotas

    Nodes

    Recommended QPS

    Pods

    Requested vCPUs

    vCPU Limit

    Requested Memory

    Memory Limit

    50

    2500

    2

    500m

    500m

    512 MiB

    512 MiB

    200

    5000

    2

    1000m

    1000m

    1024 MiB

    1024 MiB

    1000

    10000

    2

    2000m

    2000m

    2048 MiB

    2048 MiB

    2000

    20000

    4

    2000m

    2000m

    2048 MiB

    2048 MiB

  3. Configure the add-on parameters.

    Table 3 CoreDNS add-on parameters

    Parameter

    Description

    Stub Domain

    A domain name server for a custom domain name. The format is a key-value pair. The key is a domain name suffix, and the value is one or more DNS IP addresses, for example, acme.local -- 1.2.3.4,6.7.8.9.

    For details, see Configuring the Stub Domain for CoreDNS.

    Advance Config

    • parameterSyncStrategy: indicates whether to configure consistency check when the add-on is upgraded.
      • ensureConsistent: indicates that the configuration consistency check is enabled. If the configuration recorded in the cluster is inconsistent with the actual configuration, the add-on cannot be upgraded.
      • force: indicates that the configuration consistency check is ignored during an upgrade. In this case, you must ensure that the current effective configuration is the same as the original configuration. After the add-on is upgraded, restore the value of parameterSyncStrategy to ensureConsistent to enable the configuration consistency check again.
      • inherit: indicates that custom settings are automatically inherited during an upgrade. After the add-on is upgraded, restore the value of parameterSyncStrategy to ensureConsistent to enable the configuration consistency check again.
    • stub_domains: sub domains, which allow you to configure a domain name server for a custom domain name. A sub domain is in the format of a key-value pair, where the key is the suffix of a DNS domain name and the value is one or more DNS IP addresses.
    • upstream_nameservers: IP address of the upstream DNS server.
    • servers: nameservers, which are available in CoreDNS v1.23.1 and later versions. You can customize nameservers. For details, see dns-custom-nameservers.
      plugins indicates the configuration of each component in CoreDNS. Retain the default settings typically to prevent CoreDNS from being unavailable due to configuration errors. Each plugin component contains name, parameters (optional), and configBlock (optional). The format of the generated Corefile is as follows:
      $name  $parameters {
      $configBlock
      }

      Table 4 describes common plugins. For details, see Plugins.

      Example:

      {
           "servers": [
      		   {
      			"plugins": [
      				{
      					"name": "bind",
      					"parameters": "{$POD_IP}"
      				},
      				{
      					"name": "cache",
      					"parameters": 30
      				},
      				{
      					"name": "errors"
      				},
      				{
      					"name": "health",
      					"parameters": "{$POD_IP}:8080"
      				},
                                      {
      					"name": "ready",
      					"{$POD_IP}:8081"
      				},
      				{
      					"configBlock": "pods insecure\nfallthrough in-addr.arpa ip6.arpa",
      					"name": "kubernetes",
      					"parameters": "cluster.local in-addr.arpa ip6.arpa"
      				},
      				{
      					"name": "loadbalance",
      					"parameters": "round_robin"
      				},
      				{
      					"name": "prometheus",
      					"parameters": "{$POD_IP}:9153"
      				},
      				{
      					"configBlock": "policy random",
      					"name": "forward",
      					"parameters": ". /etc/resolv.conf"
      				},
      				{
      					"name": "reload"
      				}
      			],
      			"port": 5353,
      			"zones": [
      				{
      					"zone": "."
      				}
      			]
      		}
      	],
      	"stub_domains": {
      		"acme.local": [
      			"1.2.3.4",
      			"6.7.8.9"
      		]
      	},
      	"upstream_nameservers": ["8.8.8.8", "8.8.4.4"]
      }
    Table 4 Default plugin configuration of the active CoreDNS zone

    Plugin Name

    Description

    bind

    Host IP address listened by CoreDNS. Retain the default value {$POD_IP}. For details, see bind.

    cache

    Enables DNS cache. For details, see cache.

    errors

    Errors are logged to stdout. For details, see errors.

    health

    Health check for CoreDNS. {$POD_IP}:8080 is listened to. Retain the default setting. Otherwise, the CoreDNS health check will fail and the add-on will restart repeatedly. For details, see health.

    ready

    Whether the backend server is ready to receive traffic. {$POD_IP}:8081 is listened to. If the backend server is not ready, CoreDNS will suspend DNS resolution until the backend server is ready. For details, see ready.

    kubernetes

    CoreDNS Kubernetes plugin, which provides the service parsing capability in a cluster. For details, see kubernetes.

    loadbalance

    Round-robin DNS load balancer that randomizes the order of A, AAAA, and MX records in an answer. For details, see loadbalance.

    prometheus

    API for obtaining CoreDNS metrics. {$POD_IP}:9153 is listened to in the default zone. Retain the default setting. Otherwise, Prometheus cannot collect CoreDNS metrics. For details, see Prometheus.

    forward

    Forwards any queries that are not within the cluster domain of Kubernetes to predefined resolvers (/etc/resolv.conf). For details, see forward.

    reload

    Automatically reloads modified Corefiles. After you modify a ConfigMap, wait for two minutes for the modification to take effect. For details, see reload.

    log

    Enables CoreDNS logging. For details, see log.

    Example:

    {
       "name": "log"
    }

    template

    A quick response template, where AAAA indicates an IPv6 request. If NXDOMAIN is returned in an rcode response, no IPv6 resolution result is returned. For details, see template.

    Example:

    {
       "configBlock": "rcode NXDOMAIN",
       "name": "template",
       "parameters": "ANY AAAA" 
    }

  4. Configure scheduling policies for the add-on.

    • Scheduling policies do not take effect on add-on instances of the DaemonSet type.
    • When configuring multi-AZ deployment or node affinity, ensure that there are nodes meeting the scheduling policy and that resources are sufficient in the cluster. Otherwise, the add-on cannot run.
    Table 5 Configurations for add-on scheduling

    Parameter

    Description

    Multi-AZ Deployment

    • Preferred: Deployment pods of the add-on will be preferentially scheduled to nodes in different AZs. If all the nodes in the cluster are deployed in the same AZ, the pods will be scheduled to that AZ.
    • Forcible: Deployment pods of the add-on will be forcibly scheduled to nodes in different AZs. If there are fewer AZs than pods, the extra pods will fail to run.

    Node Affinity

    • Incompatibility: Node affinity is disabled for the add-on.
    • Node Affinity: Specify the nodes where the add-on is deployed. If you do not specify the nodes, the add-on will be randomly scheduled based on the default cluster scheduling policy.
    • Specified Node Pool Scheduling: Specify the node pool where the add-on is deployed. If you do not specify the node pool, the add-on will be randomly scheduled based on the default cluster scheduling policy.
    • Custom Policies: Enter the labels of the nodes where the add-on is to be deployed for more flexible scheduling policies. If you do not specify node labels, the add-on will be randomly scheduled based on the default cluster scheduling policy.

      If multiple custom affinity policies are configured, ensure that there are nodes that meet all the affinity policies in the cluster. Otherwise, the add-on cannot run.

    Taints and Tolerations

    Using both taints and tolerations allows (not forcibly) the add-on Deployment to be scheduled to a node with the matching taints, and controls the Deployment eviction policies after the node where the Deployment is located is tainted.

    The add-on adds the default tolerance policy for the node.kubernetes.io/not-ready and node.kubernetes.io/unreachable taints, respectively. The tolerance time window is 60s.

    For details, see Taints and Tolerations.

  5. Click Install.

Components

Table 6 CoreDNS components

Component

Description

Resource Type

CoreDNS

DNS server for clusters

Deployment

How Does Domain Name Resolution Work in Kubernetes?

DNS policies can be configured for each pod. Kubernetes supports DNS policies Default, ClusterFirst, ClusterFirstWithHostNet, and None. For details, see DNS for Services and Pods. These policies are specified in the dnsPolicy field in the pod-specific.

  • Default: Pods inherit the name resolution configuration from the node that the pods run on. The custom upstream DNS server and the stub domain cannot be used together with this policy.
  • ClusterFirst: Any DNS query that does not match the configured cluster domain suffix, such as www.kubernetes.io, is forwarded to the upstream name server inherited from the node. Cluster administrators may have extra stub domains and upstream DNS servers configured.
  • ClusterFirstWithHostNet: For pods running with hostNetwork, set its DNS policy ClusterFirstWithHostNet.
  • None: It allows a pod to ignore DNS settings from the Kubernetes environment. All DNS settings are supposed to be provided using the dnsPolicy field in the pod-specific.
  • Clusters of Kubernetes v1.10 and later support Default, ClusterFirst, ClusterFirstWithHostNet, and None. Clusters earlier than Kubernetes v1.10 support only Default, ClusterFirst, and ClusterFirstWithHostNet.
  • Default is not the default DNS policy. If dnsPolicy is not explicitly specified, ClusterFirst is used.

Routing

Without stub domain configurations: Any query that does not match the configured cluster domain suffix, such as www.kubernetes.io, is forwarded to the upstream DNS server inherited from the node.

With stub domain configurations: If stub domains and upstream DNS servers are configured, DNS queries are routed according to the following flow:

  1. The query is first sent to the DNS caching layer in CoreDNS.
  2. From the caching layer, the suffix of the request is examined and then the request is forwarded to the corresponding DNS:
    • Names with the cluster suffix, for example, .cluster.local: The request is sent to CoreDNS.
    • Names with the stub domain suffix, for example, .acme.local: The request is sent to the configured custom DNS resolver that listens, for example, on 1.2.3.4.
    • Names that do not match the suffix (for example, widget.com): The request is forwarded to the upstream DNS.
Figure 1 Routing