CoreDNS的基本使用
1 CoreDNS的基本使用
1.1 查看CoreDNS的插件
一旦有了coredns二进制文件,你可以使用-plugins标志列出所有已编译的插件。
sh
[root@localhost ~]# coredns --plugins|nl
1 acl
2 any
3 auto
4 autopath
5 azure
6 bind
7 bufsize
8 cache
9 cancel
10 chaos
11 clouddns
12 debug
13 dns64
14 dnssec
15 dnstap
16 erratic
17 errors
18 etcd
19 file
20 forward
21 geoip
22 grpc
23 header
24 health
25 hosts
26 k8s_external
27 kubernetes
28 loadbalance
29 local
30 log
31 loop
32 metadata
33 minimal
34 multisocket
35 nomad
36 nsid
37 pprof
38 prometheus
39 quic
40 ready
41 reload
42 rewrite
43 root
44 route53
45 secondary
46 sign
47 template
48 timeouts
49 tls
50 trace
51 transfer
52 tsig
53 view
54 whoami
55 on
[root@localhost ~]#可以看到,默认用55个插件!插件非常多。
1.2 启动CoreDNS
如果没有Corefile(请参见配置),CoreDNS 将加载whoami插件,该插件将使用客户端的 IP 地址和端口号做出响应。因此,要进行测试,我们将启动 CoreDNS 以在端口 1053 上运行,然后使用dig向其发送查询 。
1.2.1 不指定启动端口
如果不指定端口,则默认监听端口53。
在一个终端直接执行coredns。则会启动CoreDNS服务:
sh
[root@localhost ~]# coredns
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined
.:53
CoreDNS-1.13.1
linux/amd64, go1.25.2, 1db4568重新打开一个终端,查看coredns进程和端口信息:
sh
[root@localhost ~]# ps -ef|grep -v grep|grep coredns
root 1575 1492 0 13:42 pts/0 00:00:00 coredns
[root@localhost ~]# netstat -tunlp|grep coredns
tcp6 0 0 :::53 :::* LISTEN 1575/coredns
udp6 0 0 :::53 :::* 1575/coredns
[root@localhost ~]#当我使用dig来检查域名时:
sh
[root@localhost ~]# dig @localhost whoami.example.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.16 <<>> @localhost whoami.example.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46712
;; 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
;; QUESTION SECTION:
;whoami.example.com. IN A
;; ADDITIONAL SECTION:
whoami.example.com. 0 IN AAAA ::1
_udp.whoami.example.com. 0 IN SRV 0 0 44891 .
;; Query time: 0 msec
;; SERVER: ::1#53(::1)
;; WHEN: Sun Nov 16 13:46:37 CST 2025
;; MSG SIZE rcvd: 135
[root@localhost ~]# dig @localhost whoami.example.org
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.16 <<>> @localhost whoami.example.org
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45295
;; 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
;; QUESTION SECTION:
;whoami.example.org. IN A
;; ADDITIONAL SECTION:
whoami.example.org. 0 IN AAAA ::1
_udp.whoami.example.org. 0 IN SRV 0 0 58662 .
;; Query time: 0 msec
;; SERVER: ::1#53(::1)
;; WHEN: Sun Nov 16 13:46:58 CST 2025
;; MSG SIZE rcvd: 135
[root@localhost ~]#此时在coredns运行的终端有新的日志显示出来:
sh
[root@localhost ~]# coredns
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined
.:53
CoreDNS-1.13.1
linux/amd64, go1.25.2, 1db4568
[INFO] [::1]:44891 - 46712 "A IN whoami.example.com. udp 47 false 4096" NOERROR qr,aa,rd 124 0.00025989s
[INFO] [::1]:58662 - 45295 "A IN whoami.example.org. udp 47 false 4096" NOERROR qr,aa,rd 124 0.000107252s