本文主要对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上面提供了各种版本的预编译包,我们只需要下载对应的硬件版本即可。

解压对应的版本后可以得到一个二进制文件,直接执行就可以使用。

[root@tiny-server coredns]# ./coredns --help
Usage of ./coredns:-conf stringCorefile to load (default "Corefile")-dns.port stringDefault port (default "53")-pidfile stringPath to write pid file-pluginsList installed plugins-quietQuiet mode (no initialization output)-versionShow version

需要注意的是,对于预编译的版本,会内置全部官方认证的插件,也就是官网的插件页面列出来的全部插件

[root@tiny-server coredns]# ./coredns -plugins
Server types:dnsCaddyfile loaders:flagdefaultOther plugins:dns.acldns.anydns.autodns.autopathdns.azuredns.binddns.bufsizedns.cachedns.canceldns.chaosdns.clouddnsdns.debugdns.dns64dns.dnssecdns.dnstapdns.erraticdns.errorsdns.etcddns.filedns.forwarddns.grpcdns.healthdns.hostsdns.k8s_externaldns.kubernetesdns.loadbalancedns.localdns.logdns.loopdns.metadatadns.nsiddns.pprofdns.prometheusdns.readydns.reloaddns.rewritedns.rootdns.route53dns.secondarydns.signdns.templatedns.tlsdns.tracedns.transferdns.whoamion

coredns的运行也非常简单,直接运行二进制文件即可,默认情况下可以添加的参数不多,主要是指定配置文件,指定运行端口和设置quiet模式。

[root@tiny-server coredns]# ./coredns
.:53
CoreDNS-1.8.3
linux/amd64, go1.16, 4293992

默认情况下会直接监听53端口,并且读取和自己在相同目录下的Corefile配置文件。但是在这种情况下,虽然coredns正常运行了,但是由于没有配置文件,是无法正常解析任何域名请求的。

# 直接运行coredns让其监听30053端口
[root@tiny-server coredns-test]# ./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命令进行测试,发现能够正常返回请求但是解析的结果不正确
[root@tiny-server coredns-test]# 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进行解析,并且记录正常的日志和错误的日志。

[root@tiny-server coredns]# cat Corefile
. {forward . 114.114.114.114 223.5.5.5logerrorswhoami
}

然后我们再进行测试就发现coredns可以正常解析域名了:

[root@tiny-server coredns-test]# 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[root@tiny-server coredns-test]# ./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文件即可:

[root@tiny-server coredns]# cat /usr/lib/systemd/system/coredns.service
[Unit]
Description=CoreDNS
Documentation=https://coredns.io/manual/toc/
After=network.target[Service]
# Type设置为notify时,服务会不断重启
# 关于type的设置,可以参考https://www.freedesktop.org/software/systemd/man/systemd.service.html#Options
Type=simple
User=root
# 指定运行端口和读取的配置文件
ExecStart=/home/coredns/coredns -dns.port=53 -conf /home/coredns/Corefile
Restart=on-failure[Install]
WantedBy=multi-user.target

编写完成之后我们依次reload配置文件并且设置开机启动服务和开启服务,即可看到服务正常运行

[root@tiny-server coredns]# systemctl daemon-reload
[root@tiny-server coredns]# systemctl enable coredns.service
[root@tiny-server coredns]# systemctl start coredns.service
[root@tiny-server coredns]# systemctl status coredns.service
● coredns.service - CoreDNSLoaded: 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 agoDocs: https://coredns.io/manual/toc/Main PID: 131287 (coredns)Tasks: 10 (limit: 49835)Memory: 27.3MCGroup: /system.slice/coredns.service└─131287 /home/coredns/coredns -dns.port=53 -conf /home/coredns/CorefileMay 11 11:29:53 tiny-server systemd[1]: Started CoreDNS.

4、coredns日志处理

coredns的日志输出并不如nginx那么完善(并不能在配置文件中指定输出的文件目录,但是可以指定日志的格式),默认情况下不论是log插件还是error插件都会把所有的相关日志输出到程序的standard output中。使用systemd来管理coredns之后,默认情况下基本就是由rsyslogsystemd-journald这两个服务来管理日志。

4.1 StandardOutput

根据网上的参考资料我们可以得知较新版本的systemd是可以直接在systemd的unit文件里面配置StandardOutputStandardError两个参数来将相关运行日志输出到指定的文件中。

因此对于centos8等较新的系统,我们的unit文件可以这样编写:

[Unit]
Description=CoreDNS
Documentation=https://coredns.io/manual/toc/
After=network.target
# StartLimit这两个相关参数也是centos8等systemd版本较新的系统才支持的
StartLimitBurst=1
StartLimitIntervalSec=15s[Service]
# Type设置为notify时,服务会不断重启
Type=simple
User=root
# 指定运行端口和读取的配置文件
ExecStart=/home/coredns/coredns -dns.port=53 -conf /home/coredns/Corefile
# append类型可以在原有文件末尾继续追加内容,而file类型则是重新打开一个新文件
# 两者的区别类似于 echo >> 和 echo >
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.

修改完成之后我们再重启服务就可以看到日志已经被重定向输出到我们指定的文件中

[root@tiny-server coredns]# systemctl daemon-reload
[root@tiny-server coredns]# systemctl restart coredns.service

4.2 rsyslog

对于centos7等系统而言,是不支持上面的append和file两个参数的,那么在开启了rsyslog.service服务的情况下,日志就会输出到/var/log/messages文件中,或者可以使用journalctl -u coredns命令来查看全部的日志。

如果想要将coredns的日志全部集中到一个文件进行统一管理,我们可以对负责管理systemd的日志的rsyslog服务的配置进行修改:

# vim /etc/rsyslog.conf
if $programname == 'coredns' then /home/coredns/logs/coredns.log
& stop[root@tiny-server coredns]# systemctl restart rsyslog.service

从上图我们可以看到两种方式打出来的日志稍微有些不同,对于StandardOutput这种方式输出的日志缺少了前面的时间和主机名等信息,相对而言还是修改rsyslog的方式要更加的可靠。

CoreDNS篇1-简介和安装相关推荐

  1. [RHEL7基础篇-1] 简介及安装

    [RHEL7基础篇-1] 简介及安装 Linux历史简介 Redhat Linux CentOS Fedora Ubuntu与Debian Unix家族及类Unix系统 Linux版本 Linux实验 ...

  2. 码云 Git 入门篇 ---简介、安装、使用

    码云 Git 入门篇 -简介.安装.使用 这篇文章可以对刚入门gitee的朋友起到一点帮助的作用,也是我自己的一点点总结,能力一般,水平有限,如有不正确的地方请大家指正,如果觉得有帮助,请点一个赞,谢 ...

  3. Dataset:fetch_20newsgroups(20类新闻文本)数据集的简介、安装、使用方法之详细攻略

    Dataset:fetch_20newsgroups(20类新闻文本)数据集的简介.安装.使用方法之详细攻略 目录 fetch_20newsgroups(20类新闻文本)数据集的简介 1.数据集信息 ...

  4. NLP:NLP领域没有最强,只有更强的模型——GPT-3的简介、安装、使用方法之详细攻略

    NLP:NLP领域没有最强,只有更强的模型--GPT-3的简介.安装.使用方法之详细攻略 导读:NLP领域的模型,没有最大,只有更大.博主这几天也仔仔细细看了这篇长达75页的论文,光署名作者都有二三十 ...

  5. Java:Java的jar包之POI的简介、安装、使用方法(基于POI将Word、Excel、PPT转换为html)之详细攻略

    Java:Java的jar包之POI的简介.安装.使用方法(基于POI的转换-Word.Excel.Ppt等转html)之详细攻略 目录 POI的简介 1.基本功能 POI的安装 POI的使用方法(基 ...

  6. Py之textrank4zh:textrank4zh的简介、安装、使用方法之详细攻略

    Py之textrank4zh:textrank4zh的简介.安装.使用方法之详细攻略 目录 textrank4zh的简介 原理 关键词提取 关键短语提取 摘要生成 textrank4zh的安装 tex ...

  7. TF之DeepDream:DeepDream前世今生之简介、安装、使用方法之详细攻略

    TF之DeepDream:DeepDream前世今生之简介.安装.使用方法之详细攻略 目录 DeepDream前世今生之简介 DD的目的 DD特点 DD工作原理 DeepDream前世今生之安装 De ...

  8. 【KVM系列01】KVM简介及安装

    第一章 KVM简介及安装 1. KVM 介绍 1.0 虚拟化简史 1.1 KVM 架构 2. KVM 的功能列表 3. KVM 工具集合 4. RedHat Linux KVM 安装 4.1 在安装  ...

  9. python安装绘图库matplotlib_Python基础教程:Python 2D绘图库 Matplotlib 简介和安装

    原标题:Python基础教程:Python 2D绘图库 Matplotlib 简介和安装 来自:Linux迷https://www.linuxmi.com/python-2d-matplotlib.h ...

最新文章

  1. nginx下的session一致性
  2. Linux安装Hbase并验证
  3. 嵌套 移动端_360PC端小程序全面开放使用
  4. linux下初次安装mysql使用指南
  5. Redis的LRU算法
  6. 多选框向后台传值,多选框的回显,对多选框的各种操作
  7. Delphi关于多线程同步的一些方法
  8. mysql中b树是什么_MySQL优化中B树索引知识点总结
  9. 质数——埃式筛法和欧式筛法
  10. Linux之dirname与basename命令
  11. CentOS 6.7安装gcc4.8.2
  12. 2019年7月Github上最热门的Java开源项目
  13. 5、【办公自动化】Python实现PDF转Word
  14. 基于transformer的车辆识别
  15. 线性查找与二分查找的平均查找次数的比较
  16. 各自然带代表植被_植被带气候
  17. 利用windbg简单调试dump文件
  18. 3D人体骨架检测(mediapipe)
  19. 测试小兵成长记:守业容易创业难
  20. succi - 接小球游戏

热门文章

  1. 黎明者 pat basic 练习八十九 狼人杀-简单版
  2. Vue2.x + element ui 导入导出excel
  3. Vitamio 依赖导入 步骤
  4. 树莓派+USB摄像头+Yeelink--5分钟内DIY你自己的家庭监控
  5. All about Datacenter Networking Interconnection 数据中心网络互联
  6. 因为一个小功能,我对微信手机号转账的好感度加了10分
  7. PL/pgSQL之入门
  8. C++描述 5645. 找到最高海拔
  9. 【电机】使用Simulink建立BLDC反电动势测量的简单模型
  10. 青海湖 游走遇见一路风景