导读 coredns之所以如此名声大噪,就是因为从kubernetes1.9开始引入,作为kubernetes内部服务发现的默认dns。毫无疑问kubernetes是coredns的后端之一,所以我们讲coredns,就从kubernetes作为其后端开始。

coredns的诸多特性网上很多文章都有提及,在这里不再赘述。简单对比下其相对于bind和skydns的优势:

bind可以将解析存储到mysql或者文件中,coredns也可以将解析存储到etcd或者文件中,也支持将kubernetes作为其后端,直接调用kubernetes的api获取解析数据,然后缓存到本地内存。coredns支持插件扩展,目前在第三方插件中还同时支持将powerdns及amazondns作为其后端,后续还会支持越来越来的后端。bind在kubernetes的应用场景下,基本无用武之地。

coredns本身就是skydns的继任者,支持skydns的所有特性,而且性能更好,更易于扩展。其插件式特性无论是bind还是skydns都无法比拟。

coredns官方网站地址:https://coredns.io

coredns代码地址: https://github.com/coredns/coredns

coredns官方插件地址:https://coredns.io/plugins

coredns第三方插件地址:https://coredns.io/explugins/

配置kubernetes后端存储
配置说明

其实官方有kubernetes插件的相关示例及配置说明,地址如下:https://coredns.io/plugins/kubernetes/

我这里就以官方的配置示例作说明:

kubernetes [ZONES...] {
    resyncperiod DURATION
    endpoint URL [URL...]
    tls CERT KEY CACERT
    namespaces NAMESPACE...
    labels EXPRESSION
    pods POD-MODE
    endpoint_pod_names
    upstream [ADDRESS...]
    ttl TTL
    fallthrough [ZONES...]
}

下面对一些常用参数作下说明:

resyncperiod: 用于从kubernetes的api同步数据的时间间隔

endpoint: 指定kubernetes的api地址,coredns会自动对其执行健康检查并将请求代理到健康的节点上。示例如下:

endpoint https://10.1.61.129:6443 https://10.1.61.130:6443

tls: 用于指定连接远程kubernetes api的相关证书。示例:

tls admin.pem admin-key.pem ca.pem

pods: 指定POD-MODE,有以下三种:

disabled:默认

insecure:返回一个A记录对应的ip,但并不会检查这个ip对应的Pod当前是否存在。这个选项主要用于兼容kube-dns

verified:推荐的方式,返回A记录的同时会确保对应ip的pod存在。比insecure会消耗更多的内存。

upstream: 定义外部域名解析转发的地址,可以是一个ip地址,也可以是一个resolv.conf文件。示例:

upstream 8.8.8.8:53 8.8.4.4:53

ttl: 默认5s,最大3600s

示例

一个完整的配置示例:

# /opt/coredns/cfg/Corefile
.:53 {
    kubernetes wh01 {
        resyncperiod 10s
        endpoint https://10.1.61.175:6443
        tls admin.pem admin-key.pem ca.pem
        pods verified
        endpoint_pod_names
        upstream /etc/resolv.conf
    }
    health
    log /var/log/coredns.log
    prometheus :9153
    proxy . /etc/resolv.conf
    cache 30
    reload 10s
}

也可以使用如下写法:

wh01 {
    kubernetes {
        resyncperiod 10s
        endpoint https://10.1.61.129:6443
        tls admin.pem admin-key.pem ca.pem
        pods verified
        endpoint_pod_names
        upstream /etc/resolv.conf
    }
    health
    log
    errors
    prometheus :9153
    proxy . /etc/resolv.conf
    cache 30
    reload 10s
}

其他配置也简单作下说明:

health:插件,用于检测当前配置是否存活,默认监听http 8080端口,可配置

log: 插件,将日志打印到标准输出

errors:将错误打印到标准输出

prometheus: 插件,用于prometheus监控

proxy: wh01之外的域名解析都通过proxy指定的地址实现代理

cache: 插件,用于在内存中缓存dns解析,单位为s

reload: 插件,单位为s,如果配置文件发生变更,自动reload的间隔

启动coredns
nohup /opt/coredns/bin/coredns -conf /opt/coredns/cfg/Corefile &
使用systemd启动coredns
# cat /lib/systemd/system/coredns.service

[Unit]
Description=CoreDNS
Documentation=https://coredns.io

[Service]
ExecStart=/opt/coredns/bin/coredns \
  -conf /opt/coredns/cfg/Corefile

Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.targe
# systemctl start coredns
# systemctl enable coredns

原文来自:https://www.cnblogs.com/breezey/p/9074383.html

本文地址:https://www.linuxprobe.com/coredns-kubernetes.html编辑:向云艳,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/