当前位置: 首页 > 工具软件 > CoreDNS > 使用案例 >

CentOS7中安装、配置CoreDNS

申屠亦
2023-12-01

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上面提供了各种版本的预编译包,我们只需要下载对应的硬件版本即可。
从Git上下载文件https://github.com/coredns/coredns/releases/
将对应的包上传到/usr/coredns,执行以下命令解压缩

tar -zxvf coredns_1.10.1_linux_amd64.tgz

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

 [root@tiny-server coredns]# ./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

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

[root@tiny-server coredns]# ./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模式。

 [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进行解析,

  • lianlianpay.com的意思是将lianlianpay.com的请求都Forward到域控DNS进行解析,并且记录正常的日志和错误的日志。
  • hosts意思是直接配置绑定某个域名到某个IP地址,并直接引用。
  • example.com的意思是在本地创建一个为example.com的域名,然后在本地创建example.com.db的文件用于存储该域名的解析记录。
    #在第3章systemd中配置了启动端口,所以配置文件中就不需要再配置端口信息了,bind用来绑定本机提供服务的IP地址
 [root@tiny-server coredns]# cat corefile.conf
 lianlianpay.com {
        bind 192.168.162.55
        forward . 192.168.170.21 192.168.170.22
        log
        errors
        reload
 }
 
  example.com {
        bind 192.168.162.55
        file /usr/coredns/example.com.db
        log
        errors
        reload
 }

 . {
     bind 192.168.162.55
     forward . 114.114.114.114 223.5.5.5
     log
     errors
     whoami
     cache 30
     reload
     hosts {
        10.20.31.104 www.goo.com
        fallthrough
        }
 }

然后我们再进行测试就发现coredns可以正常解析域名了,需要重启服务,可使用systecmctl restart coredns
example.com.db文件配置示例
#格式
hostname TTL IN TYPE value
#在这个格式中,hostname 表示主机名,TTL 表示 TTL 值,IN 表示 DNS 记录类型,TYPE 表示记录类型,value 表示记录的值。

; example.com.db
$ORIGIN example.com.
@               IN      SOA     ns1.example.com. hostmaster.example.com. (
                        2022010101  ; Serial
                        3600        ; Refresh
                        1800        ; Retry
                        604800      ; Expire
                        86400 )     ; Minimum

                IN      NS      ns1.example.com.
                IN      NS      ns2.example.com.

; A records
www             IN      A       10.0.0.1
mail            IN      A       10.0.0.2

; CNAME records
ftp             IN      CNAME   www

2.1 corefile.conf配置文件说明
暂时无法在飞书文档外展示此内容

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=/usr/coredns/coredns -dns.port=53 -conf /usr/coredns/corefile.conf
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 - CoreDNS
   Loaded: loaded (/usr/lib/systemd/system/coredns.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2023-03-14 13:56:55 CST; 1min 17s ago
     Docs: https://coredns.io/manual/toc/
 Main PID: 1156 (coredns)
   CGroup: /system.slice/coredns.service
           └─1156 /usr/coredns/coredns -dns.port=53 -conf /usr/coredns/corefile

3月 14 13:56:55 office-coredns systemd[1]: Started CoreDNS.
3月 14 13:56:55 office-coredns coredns[1156]: .:53
3月 14 13:56:55 office-coredns coredns[1156]: CoreDNS-1.10.1
3月 14 13:56:55 office-coredns coredns[1156]: linux/amd64, go1.20, 055b2c3

4、coredns日志处理
coredns的日志输出并不如nginx那么完善(并不能在配置文件中指定输出的文件目录,但是可以指定日志的格式),默认情况下不论是log插件还是error插件都会把所有的相关日志输出到程序的standard output中,使用systemd来管理coredns之后,
4.1 StandardOutput
根据网上的参考资料我们可以得知较新版本的systemd是可以直接在systemd的unit文件里面配置StandardOutput和StandardError两个参数来将相关运行日志输出到指定的文件中。

因此对于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 /usr/coredns/logs/coredns.log
 & stop
 
 [root@tiny-server coredns]# systemctl restart rsyslog.service

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

5、多实例配置同步
CoreDNS多个实例使用etcd方法实现配置同步

  1. 安装 etcd:你需要在所有 CoreDNS 实例上安装 etcd,并确保它们都能够连接到 etcd 集群。可以参考 etcd 的官方文档进行安装和配置。
  2. 配置 etcd 插件:在 CoreDNS 的配置文件中,添加 etcd 插件的配置,以便它能够连接到 etcd 集群。以下是一个示例配置:
etcd: {
    endpoints: ["http://etcd1:2379", "http://etcd2:2379", "http://etcd3:2379"],
    path: "/coredns",
    watch: true,
    tls: {
        cert_file: "/etc/coredns/etcd-client.crt",
        key_file: "/etc/coredns/etcd-client.key",
        ca_file: "/etc/coredns/ca.crt"
    }
}

在这个示例中,endpoints 参数指定 etcd 集群的地址,path 参数指定配置存储的路径,watch 参数表示启用 etcd 的 watch 功能,tls 参数表示启用 TLS 加密连接。

  1. 配置 CoreDNS:在 CoreDNS 的配置文件中,使用 etcd 插件来加载配置。以下是一个示例配置:
.: {
    etcd {
        path /coredns
        endpoint http://etcd1:2379 http://etcd2:2379 http://etcd3:2379
        watch true
        tls cert /etc/coredns/etcd-client.crt key /etc/coredns/etcd-client.key ca /etc/coredns/ca.crt
    }
    ...
}

在这个示例中,path 参数和 endpoint 参数的含义和上一步相同,watch 参数表示启用 etcd 的 watch 功能,tls 参数表示启用 TLS 加密连接。

  1. 启动 CoreDNS:启动 CoreDNS 实例,它将自动连接到 etcd 集群并获取配置。当 etcd 中的配置发生变化时,CoreDNS 将自动更新并重新加载配置。

6、用Nginx实现高可用

  1. 安装 Nginx:在所有 CoreDNS 实例上安装 Nginx,并确保它们都能够连接到 Nginx 集群。可以参考 Nginx 的官方文档进行安装和配置。
  2. 配置 Nginx:在 Nginx 集群中,使用 upstream 模块配置 CoreDNS 的负载均衡。以下是一个示例配置:
cssCopy code
upstream coredns {
    server core1:53;
    server core2:53;
    server core3:53;
}

server {
    listen 53 udp;
    proxy_pass coredns;
}

在这个示例中,upstream 模块定义了一个名为 coredns 的服务器池,其中包含了所有 CoreDNS 实例的 IP 地址和端口号。server 模块监听 UDP 端口 53,并将请求转发到 coredns 服务器池中的实例。
3. 配置 CoreDNS:在 CoreDNS 的配置文件中,将 DNS 服务绑定到一个非标准端口,以避免与 Nginx 监听的 53 端口冲突。以下是一个示例配置:

cssCopy code
.:1053 {
    ...
}

在这个示例中,DNS 服务绑定到 1053 端口。
4. 配置客户端:在客户端的 DNS 配置中,将 DNS 服务器设置为 Nginx 集群的 IP 地址。客户端将向 Nginx 集群发送 DNS 查询请求,并由 Nginx 进行负载均衡和转发。

 类似资料: