在之前,介绍了基于 Bind 的高可用架构。这里介绍基于 coreDNS 的高可用架构。coredns 的高可用架构使用的是 coredns + etcd + keepalived。不同于 Bind 使用了 NGINX 做了负载均衡,coredns 直接使用了 Keepalived 提供高可用是由于 coredns 使用 NGINX + keepalived 性能会远低于直接使用 Keepalived 的情况。
整体架构
coreDNS 的简洁的高可用架构图如下所示。

这里解释一下相应的工作流程(按照图中的数据依次解释)。
1、使用 ETCD 作为存储,存储域名的记录信息。所有的记录使用 ETCD 做存储。coreDNS 从 ETCD 读取域名解析记录。
2、用户通过访问 CoreDNS集群获取 DNS 解析服务。
服务器信息
| IP |
主机名 |
计算能力 |
应用 |
备注 |
| 192.168.1.11 |
coredns-01 |
2c4g 200G |
coredns |
dns 服务 |
| 192.168.1.12 |
coredns-02 |
2c4g 200G |
coredns |
dns 服务 |
| 192.168.1.13 |
etcd-01 |
2c4g 200G |
etcd |
etcd 数据库 |
| 192.168.1.14 |
etcd-02 |
2c4g 200G |
etcd |
etcd 数据库 |
| 192.168.1.15 |
etcd-03 |
2c4g 200G |
etcd |
etcd 数据库 |
NTP 时间同步
时间不同步会会对高可用的使用有着影响。所以这里对所有机器安装 ntp 并同步阿里云时间。
1 2 3 4 5 6 7 8 9 10
| # 安装 ntpdate yum -y install ntpdate # 修改时区为中国上海 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo 'Asia/Shanghai' >/etc/timezone # 同步阿里云时间 ntpdate time2.aliyun.com # 加入到 crontab echo "" >> /var/spool/cron/root crontab -l > crontabtmp && echo "*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com" >> crontabtmp && crontab crontabtmp && rm -f crontabtmp
|
Etcd
这里生成了证书,所以在进行写入库的时候需要coredns服务器有证书权限。如果能确保安全的情况下,可以选择http方式,。或者使用账号密码方式验证。
证书生成
证书生成工具
证书生成工具选择的是 cfssl。在 CentOS7 中可能出现,无法下载的情况,是由于 https 证书问题。可以本地下载
1 2 3 4 5 6
| [root@etcd-01 ~]# wget https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssl_1.6.1_linux_amd64 [root@etcd-01 ~]# mv cfssl_1.6.1_linux_amd64 /bin/cfssl [root@etcd-01 ~]# chmod 755 /bin/cfssl [root@etcd-01 ~]# wget https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssljson_1.6.1_linux_amd64 [root@etcd-01 ~]# mv cfssljson_1.6.1_linux_amd64 /bin/cfssljson [root@etcd-01 ~]# chmod 755 /bin/cfssljson
|
证书生成
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 56 57 58 59 60 61 62 63 64 65
| [root@etcd-01 ~]# mkdir /etc/etcd/ssl [root@etcd-01 ~]# cat > ca-config.json << EOF { "signing": { "default": { "expiry": "876000h" }, "profiles": { "coredns": { "usages": [ "signing", "key encipherment", "server auth", "client auth" ], "expiry": "876000h" } } } } EOF [root@etcd-01 ~]# cat > etcd-ca-csr.json << EOF { "CN": "etcd", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "Zhejiang", "L": "Hangzhou", "O": "etcd", "OU": "Etcd Security" } ], "ca": { "expiry": "876000h" } } EOF [root@etcd-01 ~]# cat > etcd-csr.json << EOF { "CN": "etcd", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "Zhejinag", "L": "Hangzhou", "O": "etcd", "OU": "Etcd Security" } ] } EOF [root@etcd-01 ~]# cfssl gencert -initca etcd-ca-csr.json | cfssljson -bare /etc/etcd/ssl/ca [root@etcd-01 ~]# cfssl gencert -ca=/etc/etcd/ssl/ca.pem -ca-key=/etc/etcd/ssl/ca-key.pem \ -config=ca-config.json -profile=coredns \ -hostname=192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14,192.168.1.15 \ etcd-csr.json | cfssljson -bare /data/etcd/ssl/etcd
|
这里考虑到日后服务的增长,可以多使用配几个IP。
下载安装
1 2 3 4 5
| [root@master-01 ~]# mkdir -p /data/etcd/{data,wal} [root@master-01 ~]# wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz [root@master-01 ~]# tar -zxvf etcd-v3.5.0-linux-amd64.tar.gz [root@master-01 ~]# chown -R root.root etcd-v3.5.0-linux-amd64 [root@master-01 ~]# mv etcd-v3.5.0-linux-amd64/{etcd,etcdctl,etcdutl} /bin/
|
配置文件
etcd 的完整路径是/etc/etcd/conf/etcd.yaml。
etcd-01
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
| name: 'coredns-01' data-dir: /data/etcd/data wal-dir: /data/etcd/wal snapshot-count: 5000 heartbeat-interval: 100 election-timeout: 1000 quota-backend-bytes: 0 listen-peer-urls: 'https://192.168.1.13:2380' listen-client-urls: 'https://192.168.1.13:2379,http://127.0.0.1:2379' max-snapshots: 3 max-wals: 5 cors: initial-advertise-peer-urls: 'https://192.168.1.13:2380' advertise-client-urls: 'https://192.168.1.13:2379' discovery: discovery-fallback: 'proxy' discovery-proxy: discovery-srv: initial-cluster: 'coredns-01=https://192.168.1.13:2380,coredns-02=https://192.168.1.14:2380,coredns-03=https://192.168.1.15:2380' initial-cluster-token: 'coredns-cluster' initial-cluster-state: 'new' strict-reconfig-check: false enable-v2: true enable-pprof: true proxy: 'off' proxy-failure-wait: 5000 proxy-refresh-interval: 30000 proxy-dial-timeout: 1000 proxy-write-timeout: 5000 proxy-read-timeout: 0 client-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true peer-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' peer-client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true debug: false log-package-levels: log-outputs: [default] force-new-cluster: false
|
etcd-02
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
| name: 'coredns-02' data-dir: /data/etcd/data wal-dir: /data/etcd/wal snapshot-count: 5000 heartbeat-interval: 100 election-timeout: 1000 quota-backend-bytes: 0 listen-peer-urls: 'https://192.168.1.14:2380' listen-client-urls: 'https://192.168.1.14:2379,http://127.0.0.1:2379' max-snapshots: 3 max-wals: 5 cors: initial-advertise-peer-urls: 'https://192.168.1.14:2380' advertise-client-urls: 'https://192.168.1.14:2379' discovery: discovery-fallback: 'proxy' discovery-proxy: discovery-srv: initial-cluster: 'coredns-01=https://192.168.1.13:2380,coredns-02=https://192.168.1.14:2380,coredns-03=https://192.168.1.15:2380' initial-cluster-token: 'coredns-cluster' initial-cluster-state: 'new' strict-reconfig-check: false enable-v2: true enable-pprof: true proxy: 'off' proxy-failure-wait: 5000 proxy-refresh-interval: 30000 proxy-dial-timeout: 1000 proxy-write-timeout: 5000 proxy-read-timeout: 0 client-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true peer-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' peer-client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true debug: false log-package-levels: log-outputs: [default] force-new-cluster: false
|
etcd-03
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
| name: 'coredns-03' data-dir: /data/etcd/data wal-dir: /data/etcd/wal snapshot-count: 5000 heartbeat-interval: 100 election-timeout: 1000 quota-backend-bytes: 0 listen-peer-urls: 'https://192.168.1.15:2380' listen-client-urls: 'https://192.168.1.15:2379,http://127.0.0.1:2379' max-snapshots: 3 max-wals: 5 cors: initial-advertise-peer-urls: 'https://192.168.1.15:2380' advertise-client-urls: 'https://192.168.1.15:2379' discovery: discovery-fallback: 'proxy' discovery-proxy: discovery-srv: initial-cluster: 'coredns-01=https://192.168.1.13:2380,coredns-02=https://192.168.1.14:2380,coredns-03=https://192.168.1.15:2380' initial-cluster-token: 'coredns-cluster' initial-cluster-state: 'new' strict-reconfig-check: false enable-v2: true enable-pprof: true proxy: 'off' proxy-failure-wait: 5000 proxy-refresh-interval: 30000 proxy-dial-timeout: 1000 proxy-write-timeout: 5000 proxy-read-timeout: 0 client-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true peer-transport-security: cert-file: '/etc/etcd/ssl/etcd.pem' key-file: '/etc/etcd/ssl/etcd-key.pem' peer-client-cert-auth: true trusted-ca-file: '/etc/etcd/ssl/ca.pem' auto-tls: true debug: false log-package-levels: log-outputs: [default] force-new-cluster: false
|
systemd管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [root@etcd-01 ~]# cat /usr/lib/systemd/system/etcd.service [Unit] Description=Etcd Service Documentation=https://coreos.com/etcd/docs/latest/ After=network.target
[Service] Type=notify ExecStart=/usr/bin/etcd --config-file=/etc/etcd/conf/etcd.yaml Restart=on-failure RestartSec=10 LimitNOFILE=65536
[Install] WantedBy=multi-user.target Alias=etcd.service [root@etcd-01 ~]# systemctl daemon-reload [root@etcd-01 ~]# systemctl enable --now etcd.service
|
coredns
下载安装
coredns 在文章 《coredns 和 bind 的简单使用及性能对比》 的安装已经介绍过了。这里就不做详细介绍了。
coredns 选择的版本是 1.8.6。注意:如果无法通过 wget 下载,请本地下载再传到服务器上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| [root@coredns-01 ~]# wget https://github.com/coredns/coredns/releases/download/v1.8.6/coredns_1.8.6_linux_amd64.tgz [root@coredns-01 ~]# tar -zxvf coredns_1.8.6_linux_amd64.tgz -C /usr/bin/ [root@coredns-01 ~]# chown root.root /usr/bin/coredns [root@coredns-01 ~]# mkdir /etc/coredns [root@coredns-01 ~]# cat > /usr/lib/systemd/system/coredns.service << EOF [Unit] Description=CoreDNS Documentation=https://coredns.io/manual/toc/ After=network.target StartLimitBurst=1 StartLimitIntervalSec=15s
[Service] Type=simple User=root ExecStart=/usr/bin/coredns -conf=/etc/coredns/coredns.conf Restart=on-failure
[Install] WantedBy=multi-user.target EOF
|
在 CentOS7 及以下版本中,systemd 中无法指定 StandardOutput/StandardError=append 将日志追加到指定日志文件上去。从而日志会输出到 /var/log/messages 中。这里需要通过使用 rsyslog 来实现将日志输出到指定的日志目录。
1 2 3 4 5
| [root@coredns ~]# cat >> /etc/rsyslog.conf << EOF if \$programname == 'coredns' then /home/coredns/logs/coredns.log & stop EOF [root@coredns ~]# systemctl restart rsyslog.service
|
在 CentOS8 及以上版本中,systemd 通过 StandardOutput/StandardError=append 将日志追加到指定日志文件上去。
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| [root@coredns-01 ~]# cat /etc/coredns/coredns.conf .:53 { errors health { lameduck 5s } prometheus 192.168.1.11:9153 etcd { path /coredns endpoint https://192.168.1.13:2379 https://192.168.1.14:2379 https://192.168.1.15:2379 tls /etc/etcd/etcd.pem /etc/etcd/etcd-key.pem /etc/etcd/ca.pem } log . { class error } forward . 8.8.8.8:53 8.8.4.4:53 /etc/resolv.conf cache 30 loop reload loadbalance }
|
上述文件使用 etcd 做存储文件。并启动 prometheus 监控的相关配置。
启动
1 2
| [root@coredns-01 ~]# systemctl daemon-reload [root@coredns-01 ~]# systemctl enable --now coredns
|