安装CoreDns
下载二进制版本:
# https://github.com/coredns/coredns/releases
wget https://github.com/coredns/coredns/releases/download/v1.6.0/coredns_1.6.0_linux_amd64.tgz
tar zxvf coredns_1.6.0_linux_amd64.tgz
mv coredns /usr/bin
mkdir /etc/coredns
创建用户
useradd coredns -s /sbin/nologin
配置CoreDns
vi /etc/coredns/Corefile
配置文件中添加以下内容
- loadbalance:提供基于dns的负载均衡功能
- loop:检测在dns解析过程中出现的简单循环问题
- cache:提供前端缓存功能
- health:对Endpoint进行健康检查
- kubernetes:从kubernetes中读取zone数据
- etcd:从etcd读取zone数据,可以用于自定义域名记录
- file:从文件中读取zone数据
- hosts:使用/etc/hosts文件或者其他文件读取zone数据,可以用于自定义域名记录
- auto:从磁盘中自动加载区域文件
- reload:定时自动重新加载Corefile配置文件的内容
- forward:转发域名查询到上游dns服务器
- proxy:转发特定的域名查询到多个其他dns服务器,同时提供到多个dns服务器的负载均衡功能
- prometheus:为prometheus系统提供采集性能指标数据的URL
- pprof:在URL路径/debug/pprof下提供运行是的西能数据
- log:对dns查询进行日志记录
- errors:对错误信息镜像日志记录
.:53 {
#绑定interface ip(注意更换)
bind 172.19.197.138
# 先走本机的hosts
# https://coredns.io/plugins/hosts/
hosts {
# 自定义sms.service search.service 的解析
# 因为解析的域名少我们这里直接用hosts插件即可完成需求
# 如果有大量自定义域名解析那么建议用file插件使用 符合RFC 1035规范的DNS解析配置文件
10.6.6.2 sms.service
10.6.6.3 search.service
# ttl
ttl 60
# 重载hosts配置
# reload 5m
reload 60s
# 继续执行
fallthrough
}
# file enables serving zone data from an RFC 1035-style master file.
# https://coredns.io/plugins/file/
# file service.signed service
# 最后所有的都转发到系统配置的上游dns服务器去解析
forward . /etc/resolv.conf
# 缓存时间ttl
cache 60
# 自动加载配置文件的间隔时间
reload 6s
# DNS自动负载(多RS记录)
loadbalance
# 输出日志
log
# 输出错误
errors
}
开防火墙端口
注意开启防火墙服务
firewall-cmd --zone=public --add-port=53/udp --permanent
firewall-cmd --zone=public --add-port=53/tcp --permanent
命令启动CoreDns
ps -ef |grep coredns |awk {'print $2'} | sed -e "s/^/kill -9 /g" |sh -
nohup /usr/bin/coredns -conf /etc/coredns/Corefile > /tmp/coredns.log 2>&1 &
设置使用CoreDNS
设置DNS
vi /etc/resolv.conf
设置DNS服务器(Linux)
nameserver 172.19.197.138
使用etcd
安装etcd
# 1.下载
# https://github.com/etcd-io/etcd/releases/
wget https://github.com/etcd-io/etcd/releases/download/v3.4.6/etcd-v3.4.6-linux-amd64.tar.gz
# 2.解压
tar -zxf etcd-v3.4.6-linux-amd64.tar.gz
# 3.移动到bin目录
mv etcd-v3.4.6-linux-amd64 /usr/local/etcd
# 4.开启端口 2379 -需要外部访问的话可以
firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
# 5.启动
ps -ef |grep etcd |awk {'print $2'} | sed -e "s/^/kill -9 /g" |sh -
nohup /usr/local/etcd/etcd --data-dir=/usr/local/etcd/default.etcd/ --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 > /tmp/etcd.log 2>&1 &
CoreDNS配置文件
.:53 {
# service表示域名,防止所有域名进来导致无法解析
etcd service {
stubzones
# 如果区域匹配但不能生成记录,则将请求传递给下一个插件
#fallthrough
# coredns命名空间
path /coredns
endpoint http://127.0.0.1:2379
upstream /etc/resolv.conf
}
forward . /etc/resolv.conf
# 缓存时间ttl
cache 60
# 自动加载配置文件的间隔时间
reload 6s
# DNS自动负载(多RS记录)
loadbalance
# 输出日志
log
# 输出错误
errors
}
维护DNS数据
# coredns命名空间增加域名
/usr/local/etcd/etcdctl put /coredns/service/test '{"host":"172.0.1.2","ttl":10}'
/usr/local/etcd/etcdctl get /coredns/service/test
/usr/local/etcd/etcdctl get / --prefix --keys-only
# 添加多条记录,让coredns轮询
etcdctl put /coredns/service/test/a '{"host":"1.1.1.2","ttl":10}'
etcdctl put /coredns/service/test/b '{"host":"1.1.1.3","ttl":10}'
# V3 Restful API
curl -L http://127.0.0.1:2379/v3/kv/put -X POST -d '{"key": "/coredns/service/test", "value": "{"host":"192.168.16.220","ttl":10}"}'
#base64转码
curl -L http://127.0.0.1:2379/v3/kv/put -X POST -d '{"key": "L2NvcmVkbnMvc2VydmljZS90ZXN0", "value": "eyJob3N0IjoiMTkyLjE2OC4xNi4yMjAiLCJ0dGwiOjEwfQ=="}'