The Service Discovery Principle of DNS in Kubernetes Clusters
By Boli
Prerequisites
- Create a Kubernetes cluster through Alibaba Cloud Container Service for Kubernetes (ACK). Please refer to the following guide for more information: https://www.alibabacloud.com/help/doc-detail/95108.htm
- Connect the Kubernetes clusters through kubectl
DNS Service in Clusters
A set of DNS services is deployed in the Kubernetes clusters and exposed because of the kube-dns service name. Run the following command to check the kube-dns service details:
kubectl get SVC kube-dns -n kube-system
The output results:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 172.24.0.10 <none> 53/UDP,53/TCP,9153/TCP 27d
The service backend contains two Pods named coredns. The resolution principle of CoreDNS will be explained later. Run the following command to check the Pod details:
kubectl get deployment coredns -n kube- system
The output results:
NAME READY UP-TO-DATE AVAILABLE AGE
coredns 2/2 2 2 27d
The Principle of Domain Name Resolution in Clusters
The kubelet on Kubernetes cluster nodes contains, --cluster-dns=${dns-service-ip} and --cluster-domain=${default-local-domain}. These two DNS-related parameters are used to set the IP address and primary domain suffix of the cluster DNS server.
Check the DNS configuration file, /etc/resolv.conf, of the Pod in dnsPolicy:ClusterFirst mode under the default cluster namespace. The dnsPolicy will be introduced later:
nameserver 172.24.0.10
search default.svc.cluster.local SVC.cluster.local cluster.local
options ndots :5
Parameter descriptions:
- nameserver: It defines the IP address of the DNS server.
-
search: It sets the rules for finding the suffixes of domain names; more suffixes result in more matching queries for DNS. The cluster matches up with three suffixes, namely
default.svc.cluster.local,svc.cluster.local, andcluster.local. A maximum of eight queries, four each for IPV4 and IPV6, is performed to obtain the correct resolution result. - option: It defines the configuration file option of DNS and supports multiple KV values. For example, if the parameter is configured as ndots:5 and its dots in the domain name string is more than 5, it is regarded to have a full domain name and is resolved directly. If it is less than 5, the search suffix is appended before the parameter is resolved.
According to the above file configuration, try to resolve it in the Pod:
-
For services in the same namespace, such as Kubernetes: Add the search domain once and send
kubernetes.default.svc.cluster.local. One IPV4 domain name resolution request is sent to 172.24.0.10 for resolution. -
For services in different namespaces, such as kube-dns.kue-system: Add the search domain twice and send
kube-dns.kue-system.default.svc.cluster.local.andkube-dns.kue-system.svc.cluster.local. Two IPV4 domain name resolution requests are sent to 172.24.0.10 for the correct resolution result. -
For external services, such as aliyun.com: Add search domain three times and send
aliyun.com.default.svc.cluster.local.,aliyun.com.svc.cluster.local.,aliyun.com.cluster.local., and aliyun.com. Four IPV4 domain name resolution requests are sent to 172.24.0.10 for the correct resolution result.
Pod dnsPolicy
In the Kubernetes cluster, it’s supported to configure different DNS policies for each Pod through the dnsPolicy field. Four policies are currently available:
-
ClusterFirst: Domain name is resolved by cluster DNS services. The DNS service address configured in the Pod
/etc/resolv.conf fileis the kube-DNS address of the cluster DNS service. This is the default DNS policy for cluster workloads. - None: Cluster DNS policy is ignored. The dnsConfig field is required to specify the DNS configuration information.
-
Default: POD directly inherits DNS configuration of cluster nodes. The
/etc/resolv.conffile is directly used of nodes in the cluster. - ClusterFirstWithHostNetwork: The ClusterFirst policy is enforced in hostNetWork mode. The Default policy is used by default.
CoreDNS
CoreDNS is a standard service discovery component of Kubernetes.
- A Pod in dnsPolicy: ClusterFirst mode uses CoreDNS to resolve the internal and external domain names of clusters.
In the kube-system namespace, the cluster has a configmap named coredns. The file configuration information of its Conrefile filed is listed below. CoreDNS provides services based on the Corefile plug-ins.
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
Plug-in descriptions:
- errors: It outputs error information to the standard output.
-
health: It generates the health check report of CoreDNS. The default listening port is 8080 for the health check. Please visit
http://localhost:8080/healthto view the report. -
ready: It generates the plug-in status report of CoreDNS. The default listening port is 8181 for readability check. Please visit
http://localhost:8181/readyto view the readability. When all plug-ins are in the running state, the ready status is 200. - Kubernetes: The CoreDNS Kubernetes plug-ins provide service resolution inside the clusters.
-
prometheus: It is the metrics API of CoreDNS. Please visit
http://localhost:9153/metricsto view the monitoring data in prometheus format. -
forward (or proxy): It forwards a domain name query request to a predefined DNS server. By default, when domain names are not within Kubernetes fields, queries are forwarded to the predefined resolver
(/etc/resolv.conf). The default configuration is using the/etc/resolv.conffile on the host. - cache: DNS cache
- loop: Loop detection – If a loop is detected, CoreDNS is suspended.
- reload: It allows automatic reloads of a changed Corefile. After the ConfigMap configuration, wait two minutes for the changes to take effect.
- loadbalance: It is a cyclic DNS load balancer that randomly records the order of A, AAAA, and MX records.
Read previous post:
Read next post:
Setting a Whitelist to Specified Pod Segments in Terway for ACK
-
-
Alibaba Container Service
230 posts | 34 followers
Follow
Comments
-
-
Alibaba Container Service
230 posts | 34 followers
Follow
Related Products
-
ECS(Elastic Compute Service)
Elastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn More -
Super Computing Cluster
Super Computing Service provides ultimate computing performance and parallel computing cluster services for high-performance computing through high-speed RDMA network and heterogeneous accelerators such as GPU.
Learn More -
Elastic High Performance Computing
A HPCaaS cloud platform providing an all-in-one high-performance public computing service
Learn More -
Link IoT Edge
Link IoT Edge allows for the management of millions of edge nodes by extending the capabilities of the cloud, thus providing users with services at the nearest location.
Learn More
More Posts by Alibaba Container Service
- ACK One Fleet: Scaling Agent Sandboxes from Single-Cluster to Multi-Cluster
- Deep Dive: How Kimi's AI Agent Runs on Alibaba Cloud
- Caching is Efficiency: Achieving Precise LLM Cache Hits with Alibaba Cloud ACK GIE
- ACK One Fleet Multi-Cluster Canary Release: A "Safety Valve" for AI Inference Services
- Intelligent Scheduling for AI Inference: Cluster-Level Priority Elastic Scheduling
- When Agents Meet Workflows—Can Intelligence Become More Controllable?
- Koordinator Column 1: Viewing AI Computing Power's "Rigidity" and "Elasticity" through Gang Scheduling
- Container Technology Evolution for LLMs and AI Agents
- How ASM Ambient Mode Innovates Kubernetes Egress Traffic Management
- Best Practices for Ray on ACK: Secure Deployment of AI Data Processing/Training/Inference Environments