dns,即域名解析。是把域名指向网站空间 IP,让人们通过注册的域名可以方便地访问到网站的一种服务。

建设企业内网 DNS 的原因

  在企业内部,开发、测试以及预生产都会有一套供开发以及测试人员使用的网络环境。运维人员会为每套环境的相关项目配置单独的服务,然后开放一个端口,以 IP+Port 的形式访问。然而随着项目的增多,对于开发和测试人员记住如此多的内网地址,无疑是一件头疼的事情(当然你也可以使用浏览器书签管理器或者记录在某个地方)。同时如果因为网络规划、或者其他原因导致服务器 IP 更改,然后响应的应用就需要重新更改配置,这无疑会是日常工作量大大增加。因为内网域名系统建设就变得很重要。

常见的 DNS

  目前常用的 DNS 工具主要有 bind、dnsmasq、PowerDNS、skydns、coreDNS 等。这里主要介绍 bind 和 coreDNS 的基本使用。

  • coreDNS

  coreDNS 是 2018 年 8 月份发起的一个开源项目。前身是 skyDNS,已经纳入 CNCF,一种快速灵活的 DNS 服务器。coreDNS 的目标是成为云原生的 DNS 服务器和服务发现的一个解决方案。

  • bind

  bind 是目前最为广泛使用的 DNS 服务器软件之一,支持 Windows,Unix,Linux,Mac OS 等各种常用的操作系统。bind 的进程名是 named,默认使用 UDP 协议,端口 53,同时提供一个 TCP 控制端口 953,主要用来远程控制使用。

服务器信息

  本次测试,采用单机模式,虚拟机部署。服务器信息为如下

IP 计算能力
192.168.0.200 4C8G

coreDNS

  coreDNS官网 是 https://coredns.io/。代码托管在 github 中,仓库地址为 https://github.com/coredns/coredns。当前版本最近版本 1.8.6。

  • 下载 coreDNS

  coreDNS 的代码托管在 github 上,目前安装包的下载也是从 github 下载。点击 下载coreDNS 版本 1.8.6。

1
[root@coredns ~]# wget https://github.com/coredns/coredns/releases/download/v1.8.6/coredns_1.8.6_linux_amd64.tgz
  • 解压并安装

  下载的 coredns_1.8.6_linux_amd64.tgz 包只有一个可执行文件。所以直接解压后可获得可执行文件 coredns,将其移动到/usr/bin/中。则 coredns 的安装已经完成。

1
2
[root@coredns ~]# tar -zxvf coredns_1.8.6_linux_amd64.tgz
[root@coredns ~]# mv coredns /usr/bin/
  • 配置文件配置

  在/etc 下创建 coredns 文件夹,用作 coredns 的相关配置文件存储目录。并新增主配置文件 coredns.conf。这里由于是测试,所以使用的 hosts 插件。coredns 支持使用 hosts,file,etcd 等方式存储解析信息。

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
33
34
35
36
37
[root@coredns ~]# mkdir -p /etc/coredns/
[root@coredns ~]# cat > /etc/coredns/coredns.conf << EOF
.:53 {
errors
health {
lameduck 5s
}
hosts {
# ttl 60
ttl 60
# 重载 hosts 里面
reload 1m
# 自定义域名解析
# 因为解析的域名少我们这里直接用 hosts 插件即可完成需求
# 如果有大量自定义域名解析那么建议用 file 插件使用 符合 RFC 1035 规范的 DNS 解析配置文件
192.168.0.191 191.zxzmcode.cn
192.168.0.192 192.zxzmcode.cn
192.168.0.193 193.zxzmcode.cn
192.168.0.194 194.zxzmcode.cn
192.168.0.195 195.zxzmcode.cn
192.168.0.196 196.zxzmcode.cn
192.168.0.197 197.zxzmcode.cn
192.168.0.198 198.zxzmcode.cn
192.168.0.199 199.zxzmcode.cn
fallthrough
}
log . {
class error
}
forward . 8.8.8.8:53 8.8.4.4:53 /etc/resolv.conf
# 缓存时间 ttl
cache 30
loop
reload
loadbalance
}
EOF
  • 配置 Service 服务

  这里配置 coredns.service 对 coredns 的启停进行管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@coredns ~]# cat > /usr/lib/systemd/system/coredns.service << EOF
[Unit]
Description=CoreDNS
Documentation=https://coredns.io/manual/toc/
After=network.target
StartLimitBurst=1
StartLimitIntervalSec=15s

[Service]
Type=simple
User=root
ExecStart=/usr/bin/coredns -conf=/etc/coredns/coredns.conf
# StandardOutput=append:/data/logs/coredns/coredns.log
# StandardError=append:/data/logs/coredns/error.log
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

  在 CentOS7 及以下版本中,systemd 中无法指定 StandardOutput/StandardError=append 将日志追加到指定日志文件上去。从而日志会输出到 /var/log/messages 中。这里需要通过使用 rsyslog 来实现将日志输出到指定的日志目录。

1
2
3
4
5
[root@coredns ~]# cat >> /etc/rsyslog.conf << EOF
if \$programname == 'coredns' then /home/coredns/logs/coredns.log
& stop
EOF
[root@coredns ~]# systemctl restart rsyslog.service

  在 CentOS8 及以上版本中,systemd 通过 StandardOutput/StandardError=append 将日志追加到指定日志文件上去。

  • 启动 coredns

  使用下面命令启动 coredns,并配置开机启动

1
2
3
[root@coredns ~]# systemctl daemon-reload
[root@coredns ~]# systemctl start coredns.service
[root@coredns ~]# systemctl enable coredns.service

Bind

  Bind 是一款开源的 DNS 服务器软件,Bind 由美国加州大学 Berkeley 分校开发和维护的,全名为 Berkeley Internet Name Domain 它是目前世界上使用最为广泛的 DNS

  • 安装 bind
1
[root@dns ~]# yum -y install bind-utils bind bind-devel bind-libs
  • 配置/etc/named.conf

  bind 的主配置文件 named.conf

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
33
34
35
36
37
38
39
[root@dns ~]# cat /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { any; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
bindkeys-file "/etc/named.root.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};

logging {
channel default_debug {
file "data/named.run";
severity error;
};
category queries {
default_debug;
};
};

zone "." IN {
type hint;
file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
[root@dns ~]# # 主配置检查,如果没有错误,返回为空。即不显示任何信息。
[root@dns ~]# named-checkconf /etc/named.conf
  • 添加测试域名 zxzmcode.cn 的解析

    • 添加域名入口配置 named.rfc1912.zones
1
2
3
4
5
[root@dns ~]# cat /etc/named.rfc1912.zones
zone "zxzmcode.cn" IN {
type master;
file "zxzmcode.cn.zone";
};
  • 配置单独的解析文件
    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
    [root@dns ~]# cd /var/named/
    [root@dns named]# cat zxzmcode.cn.zone
    $TTL 86400
    $ORIGIN zxzmcode.cn.
    @ IN SOA ns1.zxzmcode.cn. admin.zxzmcode.cn (
    2017112601
    1H
    5M
    7D
    1D )
    IN NS ns1
    IN MX 10 mx1
    ns1 IN A 192.168.0.200
    mx1 IN A 192.168.0.200
    191 IN A 192.168.0.191
    192 IN A 192.168.0.192
    193 IN A 192.168.0.193
    194 IN A 192.168.0.194
    195 IN A 192.168.0.195
    196 IN A 192.168.0.196
    197 IN A 192.168.0.197
    198 IN A 192.168.0.198
    199 IN A 192.168.0.199
    [root@dns named]# # 检查 zxzmcode.cn.zone 文件,配置文件错误会有提醒,无错误会提示 OK
    [root@dns named]# named-checkzone zxzmcode.cn zxzmcode.cn.zone
    zone zxzmcode.cn/IN: loaded serial 2017112601
    OK
  • 启动 bind

  Bind 服务名为 named.service,进程名为 named。

1
[root@dns named]# systemctl enable named.service --now

性能测试

  dns 压力测试工具有挺多的,个人比较喜欢 dnsperf。这里就采用 dnsperf 对 DNS 进行压力测试,安装教程在测试工具中已经进行阐述。

  本次性能测试相关软件均开启查询日志查询日志级别为 Error(日常使用也是基本上也是这个级别)。CoreDNS 和 Bind 都是这个级别,一般开启日志后 QPS 会下降很多,大概不开启的 1/3 左右,但也要看日志级别。

dnsperf 的命令格式如下

1
2
3
4
5
6
[root@dnsperf ~]# dnsperf -d dns.txt -s 192.168.0.200 -l 120
-d 用来指定要压测的域名列表文件
-s 用来指定要压测的 DNS 服务器 IP 地址
-l 120 用来指定压测的时间,单位是秒
-Q 限制每秒查询数量
-c 指定客户端数量

  对应的压测文件如下

1
2
3
4
5
6
7
8
9
10
[root@dnsperf ~]# cat dns.txt
191.zxzmcode.cn A
192.zxzmcode.cn A
193.zxzmcode.cn A
194.zxzmcode.cn A
195.zxzmcode.cn A
196.zxzmcode.cn A
197.zxzmcode.cn A
198.zxzmcode.cn A
199.zxzmcode.cn A

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
[root@dnsperf ~]# dnsperf -d dns.txt -s 192.168.0.200 -l 120
DNS Performance Testing Tool
Version 2.7.1

[Status] Command line: dnsperf -d dns.txt -s 192.168.0.200 -l 120
[Status] Sending queries (to 192.168.0.200:53)
[Status] Started at: Tue Dec 21 14:54:32 2021
[Status] Stopping after 120.000000 seconds
...
[Status] Testing complete (time limit)

Statistics:

Queries sent: 16035067
Queries completed: 16035032 (100.00%)
Queries lost: 35 (0.00%)

Response codes: NOERROR 16035032 (100.00%)
Average packet size: request 31, response 60
Run time (s): 120.001453
Queries per second: 133623.648707

Average Latency (s): 0.000686 (min 0.000038, max 0.024050)
Latency StdDev (s): 0.000375

  本次 CoreDNS 进行压测,使 DNS 文本中的域名进行测试,单客户端,用压测 2 分钟,CoreDNS 的 QPS 在 13.3W。

多客户端
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
[root@dnsperf ~]# dnsperf -d dns.txt -s 192.168.0.200 -l120 -c1000
DNS Performance Testing Tool
Version 2.7.1

[Status] Command line: dnsperf -d dns.txt -s 192.168.0.200 -l120 -c1000
[Status] Sending queries (to 192.168.0.200:53)
[Status] Started at: Tue Dec 21 15:01:25 2021
[Status] Stopping after 120.000000 seconds
...
[Status] Testing complete (time limit)

Statistics:

Queries sent: 12049389
Queries completed: 12049363 (100.00%)
Queries lost: 26 (0.00%)

Response codes: NOERROR 12049363 (100.00%)
Average packet size: request 31, response 60
Run time (s): 120.000604
Queries per second: 100410.852932

Average Latency (s): 0.000870 (min 0.000053, max 0.030399)
Latency StdDev (s): 0.000464

  本次 CoreDNS 进行压测,使 DNS 文本中的域名进行测试,客户端为 1000 个,用压测 2 分钟,CoreDNS 的 QPS 在 10W。

bind

单客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@dnsperf ~]# dnsperf -d dns.txt -s 192.168.0.200 -l 120
DNS Performance Testing Tool
Version 2.7.1

[Status] Command line: dnsperf -d dns.txt -s 192.168.0.200 -l 120
[Status] Sending queries (to 192.168.0.200:53)
[Status] Started at: Tue Dec 21 14:29:57 2021
[Status] Stopping after 120.000000 seconds
[Status] Testing complete (time limit)

Statistics:

Queries sent: 9640710
Queries completed: 9640618 (100.00%)
Queries lost: 92 (0.00%)

Response codes: NOERROR 9640618 (100.00%)
Average packet size: request 31, response 121
Run time (s): 120.004652
Queries per second: 80335.368999

Average Latency (s): 0.001177 (min 0.000066, max 1.420708)
Latency StdDev (s): 0.004046

  本次 Bind 进行压测,使 DNS 文本中的域名进行测试,单客户端情况下,用压测 2 分钟,Bind 的 QPS 在 8W。

多客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@dnsperf ~]# dnsperf -d dns.txt -s 192.168.0.200 -l120 -c1000
DNS Performance Testing Tool
Version 2.7.1

[Status] Command line: dnsperf -d dns.txt -s 192.168.0.200 -l120 -c1000
[Status] Sending queries (to 192.168.0.200:53)
[Status] Started at: Tue Dec 21 14:38:28 2021
[Status] Stopping after 120.000000 seconds
[Status] Testing complete (time limit)

Statistics:

Queries sent: 9408765
Queries completed: 9408765 (100.00%)
Queries lost: 0 (0.00%)

Response codes: NOERROR 9408765 (100.00%)
Average packet size: request 31, response 121
Run time (s): 120.000854
Queries per second: 78405.817012

Average Latency (s): 0.001156 (min 0.000171, max 0.008900)
Latency StdDev (s): 0.000197

  本次 Bind 进行压测,使 DNS 文本中的域名进行测试,客户端为 1000 个,用压测 2 分钟,Bind 的 QPS 在 7.85W。

结论

  由此可见,在日志级别为 error 的情况下。coredns 无论在单客户端和多客户端的情况下,QPS 均高于 Bind。

特别注意

  本人在计算能力为 16C32G 的虚拟机上做个测试,coredns 的 QPS 在 14.5W 左右,而 bind 在 20W 左右。由此,本人推断,随着计算能力的变得越强,bind 的性能越好。