docker 搭建本地 coredns 服务器

coredns简介

CoreDNS是一款灵活可扩展的 DNS 服务器,并且基于插件架构,具有强大的模块化能力。其快速,简单,可扩展等特性也非常受人欢迎。
在这里插入图片描述

CoreDNS 最初是一个单独的项目,由Miek Gieben用 Google 的 Go 语言编写,与 Kubernetes 一样,CoreDNS 项目由CNCF托管,于 2017 年被 CNCF 采用,并成为CNCF毕业项目,从 Kubernetes 的 1.13 版开始,作为其默认 DNS 服务器发布,取代了之前的“kube-dns”。除了可以作为 Kubernetes 集群 DNS,也可以单独作为本地dns服务器运行。

官网: https://coredns.io/

项目地址: https://github.com/coredns/coredns

docker 搭建本地 coredns 服务器

前提条件:

  • 操作系统: Ubuntu server 22.04 LTS
  • 容器环境: Docker-CE 20.10.14

使用forward插件

编写一个CoreDns的配置文件Corefile,这里使用最简单的配置进行测试,以下配置是指将DNS请求转发到8.8.8.8进行解析。

mkdir -p /etc/coredns
cat >/etc/coredns/Corefile<<EOF
.:53 {
    forward . 8.8.8.8:53
    log
}
EOF

运行容器命令启动coredns服务,对于ubuntu系统,默认已占用53端口,需要指定coredns端口绑定到本机IP192.168.72.15

docker run -d --name coredns \
  --restart=always \
  -v /etc/coredns/:/etc/coredns/ \
  -p 192.168.72.15:53:53/udp \
  coredns/coredns:1.9.1 -conf /etc/coredns/Corefile

查看容器运行状态

root@ubuntu:~# docker ps 
CONTAINER ID   IMAGE                   COMMAND                  CREATED        STATUS        PORTS                              NAMES
b6b5e7246e83   coredns/coredns:1.9.1   "/coredns -conf /etc…"   18 hours ago   Up 16 hours   53/tcp, 192.168.72.15:53->53/udp   coredns

修改ubuntu网络配置文件,指定nameserver为本机IP地址,使本机作为dns服务器

root@ubuntu:~# cat /etc/netplan/00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      addresses:
      - 192.168.72.15/24
      gateway4: 192.168.72.8
      nameservers:
        addresses:
        - 192.168.72.15
        search: [example.com]
  version: 2

# 使配置生效
root@ubuntu:~# netplan apply

dig验证coredns默认域名whoami.example.org

root@ubuntu:~# dig whoami.example.org

; <<>> DiG 9.16.1-Ubuntu <<>> whoami.example.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23231
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;whoami.example.org.            IN      A

;; ANSWER SECTION:
whoami.example.org.     0       IN      A       192.168.72.15

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Apr 26 19:28:50 CST 2022
;; MSG SIZE  rcvd: 63


root@ubuntu:~# dig @192.168.72.15 -p 53 whoami.example.org

如果需要在同宿主机的容器中查询DNS,启动容器需要指定--net=host参数,如果指定了该项,则无须指定-p选项。

使用hosts插件

下面可以尝试自己添加一些自定义域名。 CoreDNS使用插件的方式实现一系列的功能。 例如上面例子中的forward插件,实现了DNS转发的功能。如果我们只是想添加一个域名,能够解析成我们指定的IP,可以尝试使用host插件。将上面的Corefile修改一下:

cat >/etc/coredns/Corefile<<EOF
.:53 {
    hosts {
        192.168.72.15 example.com
        192.168.72.16 example1.com
        fallthrough
    }
    
    forward . 114.114.114.114 8.8.8.8
    log
    errors
}
EOF

然后重启CoreDNS的容器,重新加载Corefile,

docker restart coredns

测试地址解析:

root@ubuntu:~# dig +short example1.com
192.168.72.16

也可以使用文件的形式去保存和管理hosts,这样更加方便。

# cat Corefile
.:53 {
    
    
    hosts /etc/coredns/hostsfile {
    
    
        fallthrough
    }
    forward . 8.8.8.8:53
    log
}

# cat hostsfile
192.168.72.16 example1.org

使用区域配置文件

创建coredns配置文件,使用区域配置文件

cat >/etc/coredns/Corefile<<EOF
.:53 {
    forward . 114.114.114.114 8.8.8.8
    log
    errors
}
example.com:53 {
    file /etc/coredns/db.example.com
    log
    errors
}
192.168.72.0/24:53 {
    file /etc/coredns/db.192.168.72
    log
    errors
}
EOF

Corefile配置中,每个带括号的部分表示一个 DNS 区域,它根据正在解析的内容设置 CoreDNS 的行为。

首先,请注意最初的括号部分。它以 .:53 开头,表示该区域是全局的(带有“.”表示所有流量),并且它正在侦听端口 53(默认为 udp)。我们在此处设置的参数将适用于所有未指定特定区域的传入 DNS 查询,例如要解析github.com的查询。我们在下一行看到,将此类请求转发到辅助 DNS 服务器进行解析;在这种情况下,对该区域的所有请求都将简单地转发到位于114.114.114.114和的 Google 的 DNS 服务器8.8.8.8

这里指定了一个example.com区域,它也在侦听 UDP 端口 53。对属于该区域的主机的任何查询都将引用一个文件数据库(类似于 bind 所做的)以在那里进行查找。例如,对server.example.com的查询将绕过.的全局区域。并落入为example.com提供服务的区域,并使用该file指令引用数据库文件以找到正确的记录。

对于一个简单的转发DNS服务器来说,它也为本地客户端提供主机名。现在我们必须创建引用的DNS数据库文件db.example.example,并用我们的主机填充它。

1、创建正向解析配置文件

cat >/etc/coredns/db.example.com<<'EOF'
$ORIGIN example.com.
@   3600 IN SOA dns.example.com. mail.example.com. (
                2017042745 ; serial
                7200       ; refresh (2 hours)
                3600       ; retry (1 hour)
                1209600    ; expire (2 weeks)
                3600       ; minimum (1 hour)
                )

    3600 IN NS a.iana-servers.net.
    3600 IN NS b.iana-servers.net.

dns      IN     A    192.168.72.15
vcenter  IN     A    192.168.72.12
esxi11   IN     A    192.168.72.11
EOF

2、创建反向解析配置文件

cat >/etc/coredns/db.192.168.72<<'EOF'
$TTL    604800
@       IN  SOA     dns.example.com. mail.example.com. (
                    2         ; Serial
                    604800    ; Refresh
                    86400     ; Retry
                    2419200   ; Expire
                    604800 )  ; Negative Cache TTL
;
; name servers - NS records
        IN      NS      dns.example.com.

;
; PTR Records
15      IN      PTR     dns.example.com.        ; 192.168.72.15
12      IN      PTR     vcenter.example.com.    ; 192.168.72.12
11      IN      PTR     esxi11.example.com.     ; 192.168.72.11
EOF

重启coredns容器,测试正向dns解析

root@ubuntu:~# dig +noall +answer @192.168.72.15  vcenter.example.com
vcenter.example.com.    3600    IN      A       192.168.72.12

测试反向dns解析

root@ubuntu:~# dig +noall +answer @192.168.72.15 -x 192.168.72.12
12.72.168.192.in-addr.arpa. 604800 IN   PTR     vcenter.example.com.

猜你喜欢

转载自blog.csdn.net/networken/article/details/124433583
今日推荐