4

CoreDNS替代dnsmasq

 1 year ago
source link: https://bajie.dev/posts/20221207-cordns_dnsmasq/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CoreDNS替代dnsmasq

2022-12-07 3 分钟阅读

CoreDNS是k8s的御用DNS解析软件

支持很多插件,各种DoH、DoT、gRPC ,各种特性,安装配置也十分简单

替代dnsmasq后,基本dnsmasq的功能都支持,修改劫持域名后也不用重启,会自动刷文件更新

下面说一下装法:

wget https://github.com/coredns/coredns/releases/download/v1.7.0/coredns_1.7.0_linux_amd64.tgz

解压后就一个文件,直接放到/usr/local/bin

做个systemd启动文件

cat /etc/systemd/system/coredns.service 
[Unit]
Description=CoreDNS DNS server
After=network.target

[Service]
PermissionsStartOnly=true
LimitNOFILE=1048576
LimitNPROC=512
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/coredns -conf=/usr/local/bin/coredns.conf
ExecReload=/bin/kill -SIGUSR1 $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
shell

东西全都在/usr/local/bin/coredns.conf配置文件里:

.:5353 https://.:443 {
  any
  log
  debug
  bind 172.18.30.1
  tls /usr/local/bin/full.pem /usr/local/bin/key.pem
  hosts /etc/hosts {
    ttl 60
    reload 1m
    no_reverse
    fallthrough
  }
  cache 120
  reload 6s
  loadbalance
  forward . 114.114.114.114:53 223.5.5.5:53 223.6.6.6:53 {
      policy sequential
  }
  prometheus 172.18.30.1:9253

}
shell

解释一下:

. 表示所有域名,.:5353 和 https://.:443 表示开了5353和443端口来服务所有域名的查询
bind 172.18.30.1 绑定指定ip
tls full.pem和key.pem是lego生成的*.rendoumi.com的证书链和私钥
hosts 表示从文件解析,reload 1m会自动扫描文件变动并加载
    最有用的是fallthrough ,如果在/etc/hosts 找不到记录,那就继续,去下面的配置里找
loadbalance 表示如果查到的记录有多条,那就按round-robin轮询返回结果
forward 这里指定上游服务器,最多可以有15个上游
    policy是严格按顺序来,首先114,114坏了5.5.5,5.5.5坏了6.6.6,如果不指定会从upstream里随机
	挑一个
prometheus 露出端口,供prometheus刮数据用
shell

然后启动,就可以了,这里为了避免跟dnsmasq冲突,用了5353,实际应该是53端口

systemctl start coredns

测一下标准的dns查询

dig -t a m.ddky.com @172.18.30.1 -p5353
2020-07-08_152514.png

下面来解释DoH,DNS-Over-HTTPS,这个特性非常鬼畜 注意,准确的说https提交的是dns-message

先测一下CF

curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=m.ddky.com&type=A' | jq .
2020-07-08_154446.png

json的结果很华丽,但是请注意,上面实际发的是dns-json,coredns不支持,鬼畜吧!

DoH实际提交的是dns-message:

curl -H 'accept: application/dns-message' -v 'https://dns.ddky.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump
curl -H 'accept: application/dns-message' -v 'https://cloudflare-dns.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump

注意那串dns=的字符串,那是个实际的查询,被Base64加码了,但绝不是对 name=www.example.com&type=A 这样简单的base64,这个东西完全无法造出来,有二进制的东西在里面。

所以我们必须用程序来了,必须下载一个土造的DoH的客户端

wget https://github.com/ameshkov/dnslookup/releases/download/v1.3.0/dnslookup-linux-amd64-v1.3.0.tar.gz

解压出来一个dnslookup,用这个来查才行

./dnslookup www.ddky.com https://dns.ddky.com/dns-query
./dnslookup www.ddky.com https://dns.alidns.com/dns-query
2020-07-08_153733.png

这样就ok了,最后补充一下,如果你非要用curl做DoH,也行,得下载最新版本的curl

curl -v --doh-url https://cloudflare-dns.com/dns-query https://m.ddky.com

看过程,是先去CF查了DNS,然后又发了个请求到m.ddky.com


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK