前言

CoreDNS 是一个从 Caddy 中Fork出来的项目(同时继承了它的链式中间件风格),作为CNCF项目中的一员,它的目标是提供一个快速且灵活的DNS服务。

CoreDNS - DNS and Service Discovery

更新历史

2022年05月06日 - 增加CoreDNS Deployment
2018年07月10日 - 初稿

阅读原文 - https://wsgzao.github.io/post/coredns/

扩展阅读

CoreDNS - https://coredns.io/


CoreDNS 简介

In Kubernetes 1.11, CoreDNS is the default DNS server.

CoreDNS is a DNS server. It is written in Go. It can be used in a multitude of environments because of its flexibility. CoreDNS is licensed under the Apache License Version 2, and completely open source.
Development takes place on Github. Most devs hang out on Slack on the #coredns channel.

CoreDNS 安装

直接在Github上下载对应执行文件压缩包
https://github.com/coredns/coredns/releases

Linux上下载安装(以官方新版本为基准)

1
2
3
wget https://github.com/coredns/coredns/releases/download/v1.1.4/coredns_1.1.4_linux_amd64.tgz
tar xzf coredns_1.1.4_linux_amd64.tgz
mv coredns /usr/local/bin

CoreDNS 配置

参考 QuickStart 中的配置
https://coredns.io/2017/07/24/quick-start/

配置文件Corefile示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
. {
proxy . 223.5.5.5:53 {
except example.org
protocol dns
}
prometheus # enable metrics
errors stdout # show errors
log stdout # show query logs
}

example.org {
file /etc/coredns/zones/example.org
prometheus # enable metrics
errors stdout # show errors
log stdout # show query logs
}

具体Corefile配置说明请参考文档
https://coredns.io/2017/07/23/corefile-explained/

而/etc/coredns/zones/example.org的配置文件示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ORIGIN example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
2017042745 ; serial
7200 ; refresh (2 hours)
3600 ; retry (1 hour)
1209600 ; expire (2 weeks)
3600 ; minimum (1 hour)
)

3600 IN NS a.iana-servers.net.
3600 IN NS b.iana-servers.net.

www IN A 127.0.0.1
IN AAAA ::1

tt IN A 192.168.2.4
IN AAAA ::1
IN TXT HelloExampleTest

CoreDNS 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 编辑本地 nameserver
vi /etc/resolv.conf
nameserver 8.8.8.8

# 编辑 corefile 文件
vi /etc/coredns/corefile

coredns:53 {
errors # show errors
log # enable query logs
zookeeper up_timeout=5 zk_timeout=10 zk_ttl=5 zk_addrs=10.65.200.36:2181,10.65.200.37:2181,10.65.200.138:2181 zk_znode=/dns
loadbalance round_robin
cache 1
}

.:53 {
errors # show errors
log # enable query logs
proxy . /etc/resolv.conf
loadbalance round_robin
cache 1
}

# 运行 CoreDNS 很简单,命令如下
coredns -conf /etc/coredns/Corefile

# 使用 dig 命令测试
https://www.diggui.com/

dig +short @10.65.200.105 google.com
dig google.com

CoreDNS Deployment

https://github.com/coredns/deployment

建议使用systemd方式部署,方便后续管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 构建coredns.deb安装包
git clone https://github.com/coredns/deployment
cd deployment
dpkg-buildpackage -us -uc -b

# 安装coredns
dpkg -i coredns_1.9.1-0~100_amd64.deb

# 将/etc/coredns/Corefile改为以下内容
xxx:53 {
errors
forward . xxx
cache 30
log
}
.:53 {
errors
forward . /etc/resolv.conf
cache 30
log
}

# 重启服务
systemctl restart coredns

# coredns日志
tail -f /var/log/syslog


参考文档

CoreDNS Manual

https://coredns.io/manual/toc/

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. CoreDNS 简介
  4. 4. CoreDNS 安装
  5. 5. CoreDNS 配置
  6. 6. CoreDNS 测试
  7. 7. CoreDNS Deployment
  8. 8. 参考文档