从 Kubernetes v1.12 开始,CoreDNS 是推荐的 DNS 服务器,CoreDNS 是通用的权威 DNS 服务器,可以用作集群 DNS,符合 DNS 规范。


在每个kubelet启动参数加上如下两个参数:

--cluster-dns=169.169.0.100 \
--cluster-domain=cluster.local \


CoreDNS的ConfigMap:CoreDNS的配置文件Corefile

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health {
            lameduck 5s
        }
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }
        
        hosts { # 直接加hosts解析
          192.168.1.53 www.example.com
          fallthrough # 解析不到传递给下一个插件
        }
        
        rewrite name example.com example.default.svc.cluster.local # 类似cname,但是是指向集群内地址
        
        forward . /etc/resolv.conf
        forward . 114.114.114.114 223.5.5.5 # 指定DNS服务地址
        
        prometheus :9153 # 被prometheus监控地址
        cache 30
        loop
        reload
        loadbalance
    }
    
    scriptjc.com:53 { # 对特定域名进行指定DNS服务地址,如内网的域名用内网的DNS服务
        errors
        cache 30
        forward . 192.168.1.53
    }


Corefile 配置包括以下 CoreDNS 插件:

errors:错误记录到标准输出。

health:存活性检测,地址 http://localhost:8080/health 

ready:就绪性检测,端口 8181

kubernetes:为k8s的service IP、Pod IP提供反向解析。 ttl 0 表示不缓存。

prometheus:为prometheus提供监控指标,地址:http://localhost:9153/metrics

forward:不在 Kubernetes 集群域内的任何查询都将转发到 预定义的解析器 (/etc/resolv.conf).

cache:启用前端缓存。

loop:向上查询出现循环则中止 CoreDNS 进程。

reload:自动重载配置,改配置无需重启Pod。

loadbalance:这是一个轮转式 DNS 负载均衡器, 它在应答中随机分配 A、AAAA 和 MX 记录的顺序。


详见官方地址:

kubernetes.io/zh/docs/tasks/administer-cluster/dns-custom-nameservers/

更多用法:

support.huaweicloud.com/usermanual-cce/cce_01_0361.html


busybox.yaml

kubectl run pod-test --image=busybox:1.28 --generator='run-pod/v1' -- sleep 3600

测试:

]# kubectl exec busybox -it -- sh
/ # nslookup kube-dns.kube-system
Server:    169.169.0.100
Address 1: 169.169.0.100

Name:      kube-dns.kube-system
Address 1: 169.169.0.100