本文最后更新于:May 9, 2021 am
                
              
            
            
              本文主要对coredns的原理和特性进行介绍,同时会对其二进制的安装方法进行尝试。
 
1、coredns简介 coredns是一个用go语言编写的开源的DNS服务,它的官网可以点击这里 ,github页面可以点击这里 。需要额外注意的是,coredns是首批加入CNCF 组织的云原生开源项目,并且作为已经在CNCF毕业的项目,coredns还是目前kubernetes中默认的dns服务。同时,由于coredns可以集成插件,它还能够实现服务发现的功能。
coredns和其他的诸如bind、knot、powerdns、unbound等DNS服务不同的是:coredns非常的灵活,并且几乎把所有的核心功能实现都外包给了插件。比如说如果你想要在coredns中加入Prometheus的监控支持,那么只需要安装对应的prometheus插件并且启用即可,因此官方也说coredns是由插件驱动的。
CoreDNS is powered by plugins. 
 
对于coredns插件的定义,官网是这样表示的:插件是能够单独或者共同实现一个“DNS的功能(DNS function)” 。
Plugins can be stand-alone or work together to perform a “DNS function”.
So what’s a “DNS function”? For the purpose of CoreDNS, we define it as a piece of software that implements the CoreDNS Plugin API. The functionality implemented can wildly deviate. There are plugins that don’t themselves create a response, such as metrics  or cache , but that add functionality. Then there are plugins that do  generate a response. These can also do anything: There are plugins that communicate with Kubernetes  to provide service discovery, plugins that read data from a file  or a database .
 
2、coredns安装 和大多数的软件一样,coredns提供了源码编译、预编译包和docker镜像三种安装方式 。这里我们使用预编译包的方式进行安装。coredns在github 上面提供了各种版本的预编译包,我们只需要下载对应的硬件版本即可。
解压对应的版本后可以得到一个二进制文件,直接执行就可以使用。
$ ./coredns --help  Usage of ./coredns:   -conf  string          Corefile to  load (default "Corefile" )   -dns.port string          Default port (default "53" )   -pidfile string          Path to  write  pid file    -plugins         List installed plugins   -quiet         Quiet mode  (no  initialization output)   -version          Show version 
 
需要注意的是,对于预编译的版本,会内置全部官方认证的插件,也就是官网的插件页面 列出来的全部插件
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 $ ./coredns -plugins Server types:   dns Caddyfile loaders:   flag   default Other plugins:   dns.acl    dns.any    dns.auto    dns.autopath    dns.azure    dns.bind    dns.bufsize    dns.cache    dns.cancel    dns.chaos    dns.clouddns    dns.debug    dns.dns64    dns.dnssec    dns.dnstap    dns.erratic    dns.errors    dns.etcd    dns.file    dns.forward    dns.grpc    dns.health    dns.hosts    dns.k8s_external    dns.kubernetes    dns.loadbalance    dns.local    dns.log    dns.loop    dns.metadata    dns.nsid    dns.pprof    dns.prometheus    dns.ready    dns.reload    dns.rewrite    dns.root    dns.route53    dns.secondary    dns.sign    dns.template    dns.tls    dns.trace    dns.transfer    dns.whoami    on
 
coredns的运行也非常简单,直接运行二进制文件即可,默认情况下可以添加的参数不多,主要是指定配置文件,指定运行端口和设置quiet模式。
$ ./coredns .:53 CoreDNS-1.8.3 linux/amd64, go1.16, 4293992
 
默认情况下会直接监听53端口,并且读取和自己在相同目录下的Corefile配置文件。但是在这种情况下,虽然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  $ ./coredns -dns.port 30053 .:30053 CoreDNS-1.8.3 linux/amd64, go1.16, 4293992 [INFO] 127.0.0.1:47910 - 63992 "A IN tinychen.com. udp 53 false 4096"  NOERROR qr,aa,rd 94 0.000162476s [INFO] 127.0.0.1:48764 - 26598 "A IN tinychen.com. udp 53 false 4096"  NOERROR qr,aa,rd 94 0.000135895s $ dig tinychen.com @127.0.0.1 -p30053 ; <<>> DiG 9.11.20-RedHat-9.11.20-5.el8_3.1 <<>> tinychen.com @127.0.0.1 -p30053 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26598 ;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 3 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ; COOKIE: 4429aa454c031afe (echoed) ;; QUESTION SECTION: ;tinychen.com.                  IN      A ;; ADDITIONAL SECTION: tinychen.com.           0       IN      A       127.0.0.1 _udp.tinychen.com.      0       IN      SRV     0 0 48764 . ;; Query time: 0 msec ;; SERVER: 127.0.0.1#30053(127.0.0.1) ;; WHEN: Tue May 11 11:39:47 CST 2021 ;; MSG SIZE  rcvd: 117 
 
这里我们简单编写一个Corefile配置文件就能够先让coredns正常解析域名,这个配置文件的意识是对所有域的请求都forward到114DNS进行解析,并且记录正常的日志和错误的日志。
$ cat  Corefile . {     forward . 114.114.114.114 223.5.5.5     log      errors     whoami  }
 
然后我们再进行测试就发现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 $ dig tinychen.com @127.0.0.1 -p30053 ; <<>> DiG 9.11.20-RedHat-9.11.20-5.el8_3.1 <<>> tinychen.com @127.0.0.1 -p30053 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49732 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;tinychen.com.                  IN      A ;; ANSWER SECTION: tinychen.com.           35      IN      A       47.107.188.168 ;; Query time: 29 msec ;; SERVER: 127.0.0.1#30053(127.0.0.1) ;; WHEN: Tue May 11 14:02:41 CST 2021 ;; MSG SIZE  rcvd: 69 $ ./coredns -dns.port 30053 .:30053 CoreDNS-1.8.3 linux/amd64, go1.16, 4293992 [INFO] 127.0.0.1:42293 - 51799 "A IN tinychen.com. udp 53 false 4096" NOERROR qr,rd,ra 58 0.244014828s 
 
3、systemd管理 coredns作为一个二进制执行文件,并没有向其他的如nginx、bind等服务提供种类繁多的进程控制(reload stop restart等等)选项,因此为了方便我们管理和在后台一直运行coredns,这里我们使用systemd对其进行管理,只需要编写一个systemd的unit文件即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ cat  /usr/lib/systemd/system/coredns.service [Unit] Description=CoreDNS Documentation=https://coredns.io/manual/toc/ After=network.target [Service] Type=simple User=root ExecStart=/home/coredns/coredns -dns.port=53 -conf /home/coredns/Corefile Restart=on-failure [Install] WantedBy=multi-user.target
 
编写完成之后我们依次reload配置文件并且设置开机启动服务和开启服务,即可看到服务正常运行
$ systemctl daemon-reload $ systemctl enable  coredns.service $ systemctl start coredns.service $ systemctl status coredns.service ● coredns.service - CoreDNS    Loaded: loaded (/usr/lib/systemd/system/coredns.service; enabled; vendor preset: disabled)    Active: active (running) since Tue 2021-05-11 11:29:53 CST; 2h 37min ago      Docs: https://coredns.io/manual/toc/  Main PID: 131287 (coredns)     Tasks: 10 (limit : 49835)    Memory: 27.3M    CGroup: /system.slice/coredns.service            └─131287 /home/coredns/coredns -dns.port=53 -conf /home/coredns/Corefile May 11 11:29:53 tiny-server systemd[1]: Started CoreDNS.
 
4、coredns日志处理 coredns的日志输出并不如nginx那么完善(并不能在配置文件中指定输出的文件目录,但是可以指定日志的格式),默认情况下不论是log插件还是error插件都会把所有的相关日志输出到程序的standard output中。使用systemd来管理coredns之后,默认情况下基本就是由rsyslog和systemd-journald这两个服务来管理日志。
4.1 StandardOutput 根据网上的参考资料 我们可以得知较新版本的systemd是可以直接在systemd的unit文件里面配置StandardOutput和StandardError两个参数来将相关运行日志输出到指定的文件中。
因此对于centos8等较新的系统,我们的unit文件可以这样编写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [Unit] Description=CoreDNS Documentation=https://coredns.io/manual/toc/ After=network.target StartLimitBurst=1 StartLimitIntervalSec=15s [Service] Type=simple User=root ExecStart=/home/coredns/coredns -dns.port=53 -conf /home/coredns/Corefile StandardOutput=append:/home/coredns/logs/coredns.log StandardError=append:/home/coredns/logs/coredns_error.log Restart=on-failure [Install] WantedBy=multi-user.target
 
参考链接:systemd.exec (www.freedesktop.org) 
The file:*path* option may be used to connect a specific file system object to standard output. The semantics are similar to the same option of StandardInput=, see above. If path  refers to a regular file on the filesystem, it is opened (created if it doesn’t exist yet) for writing at the beginning of the file, but without truncating it. If standard input and output are directed to the same file path, it is opened only once, for reading as well as writing and duplicated. This is particularly useful when the specified path refers to an AF_UNIX socket in the file system, as in that case only a single stream connection is created for both input and output.
append:*path* is similar to file:*path* above, but it opens the file in append mode.
 
修改完成之后我们再重启服务就可以看到日志已经被重定向输出到我们指定的文件中
$ systemctl daemon-reload $ systemctl restart coredns.service
 
4.2 rsyslog 对于centos7等系统而言,是不支持上面的append和file两个参数的,那么在开启了rsyslog.service服务的情况下,日志就会输出到/var/log/messages文件中,或者可以使用journalctl -u coredns命令来查看全部的日志。
如果想要将coredns的日志全部集中到一个文件进行统一管理,我们可以对负责管理systemd的日志的rsyslog服务的配置进行修改:
if  $programname  == 'coredns'  then  /home/coredns/logs/coredns.log & stop $ systemctl restart rsyslog.service
 
从上图我们可以看到两种方式打出来的日志稍微有些不同,对于StandardOutput这种方式输出的日志缺少了前面的时间和主机名等信息,相对而言还是修改rsyslog的方式要更加的可靠。