创建集群时会自动安装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域名格式为
默认情况下,会将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解析过程,如下图所示: