DNS principles and configuration

Updated at:

Learn about DNS resolution principles in ACS clusters and how to configure DNS policies for different scenarios.

Background information

ACS clusters do not deploy a DNS service by default. You must select CoreDNS when you create an ACS cluster. The DNS service is exposed through the kube-dns Service name. Run the following command to view the details of the kube-dns Service.

kubectl get svc kube-dns -n kube-system

Expected output:

NAME       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   172.24.0.10   <none>        53/UDP,53/TCP,9153/TCP   27d

DNS resolution principles in clusters

In ACS clusters, the kubelet startup parameters include --cluster-dns=<dns-service-ip> and --cluster-domain=<default-local-domain>, which set the IP address of the cluster DNS server and the primary cluster domain suffix, respectively.

The DNS resolution configuration file in a Pod is /etc/resolv.conf. The file contains the following content:

nameserver xx.xx.0.10
search kube-system.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

Parameter

Description

nameserver

Defines the IP address of the DNS server.

search

Sets the suffix rules for domain name lookup. The more suffixes are configured, the more lookup attempts DNS resolution requires. An ACS cluster matches 3 suffixes: kube-system.svc.cluster.local, svc.cluster.local, and cluster.local. Up to 4 queries may be needed to get the correct resolution result.

options

Defines the options of the DNS resolution configuration file. Multiple key-value pairs are supported. For example, if this parameter is set to ndots:5, a domain name that contains more dots than the ndots value is considered a fully qualified domain name (FQDN). This name is resolved directly. A domain name that contains fewer dots than the ndots value has the search suffixes appended before the query.

Based on the preceding Pod configuration, the cluster sends domain name queries to the cluster DNS server to get the result. The queries cover both Services defined in the cluster and external domain names.

Cluster dnsPolicy configuration and scenarios

ACS lets you configure a different DNS policy for each Pod by using the dnsPolicy field. ACS clusters support the following three policies:

  • ClusterFirst: Uses CoreDNS for DNS resolution. The DNS service address configured in /etc/resolv.conf within the Pod is the kube-dns address of the cluster DNS service. This is the default policy for workloads in the cluster.

  • None: Ignores the cluster DNS policy. You must provide the dnsConfig field to specify the DNS configuration.

  • Default: Directly inherits the DNS resolution configuration of the cluster node. In an ACS cluster, the Pod uses the Elastic Compute Service (ECS) /etc/resolv.conf file, which is configured with Alibaba Cloud DNS.

The following three scenarios show how to configure dnsPolicy for each of the preceding policies.

Scenario 1: Use CoreDNS provided by the ACS cluster for DNS resolution

For this scenario, use the dnsPolicy: ClusterFirst policy. Sample configuration:

apiVersion: v1
kind: Pod
metadata:
  name: alpine
  namespace: default
spec:
  containers:
  - image: alpine
    command:
      - sleep
      - "10000"
    imagePullPolicy: Always
    name: alpine
  dnsPolicy: ClusterFirst

Scenario 2: Customize the DNS configuration at the Pod level

When you need to specify a DNS configuration for Deployment-type workloads, use the dnsPolicy: None policy. Sample configuration:

apiVersion: v1
kind: Pod
metadata:
  name: alpine
  namespace: default
spec:
  containers:
  - image: alpine
    command:
      - sleep
      - "10000"
    imagePullPolicy: Always
    name: alpine
  dnsPolicy: None
  dnsConfig:
    nameservers: ["169.254.xx.xx"]
    searches:
    - default.svc.cluster.local
    - svc.cluster.local
    - cluster.local
    options:
    - name: ndots
      value: "2"

The following table describes the parameters in dnsConfig:

Parameter

Description

nameservers

Used as the list of IP addresses of the DNS servers for the Pod. Up to 3 IP addresses can be specified. When the dnsPolicy of the Pod is set to None, the list must contain at least 1 IP address. Otherwise, this property is optional. The listed DNS IP addresses are merged into the dnsPolicy-generated DNS resolution file, in its nameserver field. Duplicate addresses are removed.

searches

The list of DNS search domains for hostname lookups in the Pod. This property is optional. If specified, the provided list is merged into the base search domains generated from the selected DNS policy. Duplicate domain names are removed. Kubernetes allows a maximum of 6 search domains.

options

An optional list of objects. Each object can have a name property (required) and a value property (optional). The contents of this property are merged into the options generated from the specified DNS policy, and duplicate entries are removed.

Scenario 3: Use the Alibaba Cloud ECS DNS configuration

If your application Pod does not need to access other Services in the cluster and needs DNS resolution only through Alibaba Cloud DNS, use the dnsPolicy: Default policy. This keeps DNS resolution away from CoreDNS. Sample configuration:

apiVersion: v1
kind: Pod
metadata:
  name: alpine
  namespace: default
spec:
  containers:
  - image: alpine
    command:
      - sleep
      - "10000"
    imagePullPolicy: Always
    name: alpine
  dnsPolicy: Default

Use HostAliases to configure /etc/hosts for container Pods

To resolve a domain name to a fixed IP address inside a container Pod, enable the hosts plugin in CoreDNS. For more information about the procedure, see Configure extended features based on CoreDNS .

To make domain-name-to-fixed-IP resolution apply only inside a specific container Pod, use the HostAliases field to modify the /etc/hosts file in that Pod. Sample configuration:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  hostAliases:
  - ip: "127.0.**.**"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.**.**"
    hostnames:
    - "foo.remote"
  containers:
  - name: cat-hosts
    image: busybox:1.28
    command:
    - cat
    args:
    - "/etc/hosts"

When you update the spec field of the Pod to include the hostAliases field, the /etc/hosts file is initialized with the following content after the Pod starts.

# Kubernetes-managed hosts file.
127.0.**.**	localhost
10.200.**.**	hostaliases-pod

# Entries added by HostAliases.
127.0.**.**	foo.local	bar.local
10.1.**.**	foo.remote	bar.remote

The output shows that the domain names foo.local, bar.local, and foo.remote are all bound to fixed IP addresses.