1. 程式人生 > 其它 >k8s service的DNS名稱解析之CoreDNS

k8s service的DNS名稱解析之CoreDNS

1. CoreDNS名稱解析

  • CoreDNS:是一個DNS伺服器,Kubernetes預設採用,以Pod部署在叢集中,CoreDNS服務監視Kubernetes API,為每一個Service建立DNS記錄用於域名解析。

  • CoreDNS YAML檔案:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns

  • ClusterIP A記錄格式: ..svc.cluster.local

  • 示例:my-svc.my-namespace.svc.cluster.local

  • CoreDNS工作流程圖

2. 案例

  • 檢視service服務

    [root@k8s-master service]# kubectl get service
    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        8d
    probe-demo   ClusterIP   10.104.161.168   <none>        80/TCP         4d1h
    web          NodePort    10.100.222.42    <none>        80:30009/TCP   19h
    
  • 啟動一個busybox服務進行測試

    [root@k8s-master service]# kubectl run -it dns-test --image=busybox:1.28.4 -- /bin/sh
    If you don't see a command prompt, try pressing enter.
    / # nslookup web
    Server:    10.96.0.10
    Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
    
    Name:      web
    Address 1: 10.100.222.42 web.default.svc.cluster.local
    
    
  • 註釋,使用名稱解析需要先安裝CoreDNS服務