Configure DNS policies for Pods in a cluster in different scenarios by using the dnsPolicy field. Use the HostAliases field to resolve domain names to fixed IP addresses from inside a container Pod.
Prerequisites
-
You have created ACK managed cluster or ACK Serverless cluster. For instructions, see Create an ACK managed cluster and ACK Serverless quick start.
Background information
For details about how DNS resolution works in a cluster, see DNS add-ons in a cluster.
By default, a Container Service for Kubernetes (ACK) cluster deploys a CoreDNS workload and exposes the DNS service through the Service named kube-dns. The CoreDNS workload deployed by ACK is backed by two Pods named coredns. Based on the configuration inside a Pod, the cluster sends domain name requests to the cluster DNS server to get the results.
-
View the details of the kube-dns Service:
kubectl get svc kube-dns -n kube-system -
View the details of the CoreDNS Pods:
kubectl get deployment coredns -n kube-system
Use the dnsPolicy field to configure DNS policies for Pods in a cluster
ACK allows you to use the dnsPolicy field to configure a different DNS policy for each Pod. ACK clusters support four policies:
-
ClusterFirst: This is the default DNS policy. When a Pod needs to resolve a domain name, it first queries the CoreDNS service inside the cluster. Resolving domain names through CoreDNS means that the /etc/resolv.conf file of the Pod is automatically configured to point to the kube-dns Service address.
-
None: If you use this policy, Kubernetes ignores the DNS policies of the cluster. You must provide the dnsConfig field to specify the DNS configuration. Otherwise, the Pod may fail to resolve any domain name correctly.
-
Default: The Pod directly inherits the DNS resolution configuration of the cluster node. That is, in an ACK cluster, the Pod directly uses the /etc/resolv.conf file of the Elastic Compute Service (ECS) instance. This file is configured with Alibaba Cloud DNS.
-
ClusterFirstWithHostNet: This policy forces the ClusterFirst policy in hostNetwork mode (by default, the Default policy is used).
For these four policies, this topic describes four scenarios that show how to configure dnsPolicy.
Scenario 1: Use the CoreDNS provided by the ACK cluster for DNS resolution
To resolve domain names by using the CoreDNS provided by the ACK cluster, use the dnsPolicy: ClusterFirst policy. The following example shows the 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
To specify a DNS configuration for a Deployment workload, use the dnsPolicy: None policy. The following example shows the 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 |
A list of IP addresses used as the DNS servers for the Pod. Up to 3 IP addresses can be specified. When the dnsPolicy of the Pod is set to |
|
searches |
A list of DNS search domains used for hostname lookups in the Pod. This property is optional. After you specify it, the provided list is merged into the base search domains generated from the selected DNS policy, and duplicate domain names are removed. Kubernetes allows up to 6 search domains. Note
If the DNS server is unreachable, only the first search domain is tried. |
|
options |
An optional list of objects, where each object can have a name property (required) and a value property (optional). The content of this property is merged into the options generated from the specified DNS policy, and duplicate entries are removed. For more information about options, see DNS resolution and caching policies. |
For more information, see DNS configuration instructions on the official Kubernetes website.
Scenario 3: Use the DNS configuration of Alibaba Cloud ECS
If your application Pod needs no access to other Services in the cluster and requires resolution only through Alibaba Cloud DNS instead of CoreDNS, use the dnsPolicy: Default policy. The following example shows the configuration:
apiVersion: v1
kind: Pod
metadata:
name: alpine
namespace: default
spec:
containers:
- image: alpine
command:
- sleep
- "10000"
imagePullPolicy: Always
name: alpine
dnsPolicy: Default
Scenario 4: Access cluster Services in hostNetwork mode
If your application Pod uses hostNetwork:true to configure the network, the applications running in the Pod can directly see the network interfaces of the host. The DNS policy defaults to Default, so the Pod cannot access Services in the cluster. To access Services in the cluster in this network mode, use the dnsPolicy: ClusterFirstWithHostNet policy. The following example shows the configuration:
apiVersion: v1
kind: Pod
metadata:
name: alpine
namespace: default
spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
containers:
- image: alpine
command:
- sleep
- "10000"
imagePullPolicy: Always
name: alpine
Use the HostAliases field to resolve domain names to fixed IP addresses from inside a container Pod
Resolve domain names to fixed IP addresses from inside a container Pod by using one of the following two methods:
-
Global configuration (affects the resolution results of a specific domain name for all Pods in the entire cluster): To resolve a domain name to a fixed IP address from inside a container Pod, enable the hosts plugin in CoreDNS. For instructions, see Configure extended features based on CoreDNS.
-
Configuration for a single Pod: To resolve a domain name to a fixed IP address only inside a specific container Pod, use the HostAliases field to modify its /etc/hosts file. The following example shows the 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 the spec field of the Pod includes the
hostAliasesfield, the /etc/hosts file is initialized with the following content after the Pod starts:# Kubernetes-managed hosts file. 127.0.**.** localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet fe00::0 ip6-mcastprefix fe00::1 ip6-allnodes fe00::2 ip6-allrouters 10.200.**.** hostaliases-pod # Entries added by HostAliases. 127.0.**.** foo.local bar.local 10.1.**.** foo.remote bar.remoteThe domain names foo.local, bar.local, and foo.remote are all bound to fixed IP addresses.
References
-
For more DNS configuration and tuning operations, see DNS best practices.
-
To learn about DNS resolution and caching policies, see DNS resolution and caching policies.