DNS概述
更新时间 2026-09-14 17:59:00
最近更新时间: 2026-09-14 17:59:00
本节介绍网络的用户指南:服务发现DNS概述。
创建集群时会自动安装CoreDNS插件,用来提供集群内部域名解析。在kube-system命名空间下,可以查看到CoreDNS相关Pod:
# kubectl -nkube-system get po -l k8s-app=kube-dns
NAME READY STATUS RESTARTS AGE
coredns-84f6584c85-5rmgq 1/1 Running 0 41h
coredns-84f6584c85-rs4n6 1/1 Running 0 41h
作为集群内部DNS服务器,CoreDNS会将Service域名与Service的IP记录起来,Pod可以向CoreDNS查询Service域名获取对应IP地址。Pod访问的Service域名格式为service.namespace.svc.localDomain,其中service为Service名称,namespace为命名空间名称,localDomain为集群内部域名,默认为cluster.local。若客户端和服务端在同一个命名空间下,可通过service直接访问。建议在集群内部使用Service域名访问Pod,无需感知具体Service地址。
默认情况下,会将Coredns Service的地址作为域名解析服务地址写在Pod的/etc/resolv.conf
# kubectl -nkube-system get svc kube-dns
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 9d
# kubectl exec -it nginx-demo-748fb499d7-8f2t5 -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
Pod内通过Service域名访问后端Pod的DNS解析过程,如下图所示: