本地 dns 更新: dnsmasq 的使用情况 原始版本
###背景
最近各种ad服务挂掉的情况连连出现,一个域名解析需要花上3秒钟,业务上黄花菜都凉了,有的/etc/resolv.conf里面就配置一个nameserver,一点用都没有,dns服务出现问题之后整个应用服务都跟着受损,现在的ad服务大多企业全部用的商业软件,微软这上面真是霸道,简直是受制于人,windows上的服务说没就没了,全看脸,ldap dns 用户验证统统就见如来了,还全是底层的核心系统。
###配置和启动
listen-address=127.0.0.1
interface=lo
bind-interfaces
resolv-file=/etc/dnsmasq.resolv.conf
addn-hosts=/etc/hosts
cache-size=150
local-ttl=30
clear-on-reload
no-negcache
启动方式
dnsmasq --all-servers --conf-file=/etc/dnsmasq.conf --pid-file=/var/run/dnsmasq.pid
启动脚本
#!/bin/sh
case $1 in
start)
dnsmasq --all-servers --conf-file=/etc/dnsmasq.conf --pid-file=/var/run/dnsmasq.pid
;;
stop)
kill $(cat /var/run/dnsmasq.pid)
;;
test)
dnsmasq --test --conf-file=/etc/dnsmasq.conf
;;
reload)
cat /var/run/dnsmasq.pid|xargs kill -SIGHUP
;;
*)
echo "$0 {start|stop|reload|test}"
exit 4
;;
esac
将你所有的nameserver地址全部写到/etc/dnsmasq.resolv.conf,dnsmasq每次会全量发起域名解析请求,取最快的解析地址(不过我没有测试如果nameserver返回的不一致会怎么样)。
###测试和容器 ####1. /etc/hosts修改测试 容器中启动之后的/etc/hosts和/etc/resolve和主机中保持一致
修改了容器中的/etc/hosts
bash-4.1# ping nvwa.com
PING nvwa.com (192.168.1.184) 56(84) bytes of data.
64 bytes from nvwa.com (192.168.1.184): icmp_seq=1 ttl=52 time=27.9 ms
64 bytes from nvwa.com (192.168.1.184): icmp_seq=2 ttl=52 time=27.9 ms
主机中的记录
[root@myserver ~]# ping nvwa.com
PING nvwa.com (192.168.1.181) 56(84) bytes of data.
64 bytes from 192.168.1.181: icmp_seq=1 ttl=52 time=27.2 ms
64 bytes from 192.168.1.181: icmp_seq=2 ttl=52 time=27.1 ms
可以看到容器中的/etc/hosts修改会立即生效,且不会影响主机对此域名的解析
再次修改容器中的/etc/hosts域名指向也是立即生效
bash-4.1# ping nvwa.com
PING nvwa.com (192.168.1.182) 56(84) bytes of data.
64 bytes from nvwa.com (192.168.1.182): icmp_seq=1 ttl=52 time=27.8 ms
####2. 容灾测试 增加防火墙模拟深圳的dns挂掉
iptables -A OUTPUT -d 8.8.8.8 -j DROP
当所有的dns禁用之后,dnsmasq的缓存还可以保留解析,但是如果重启之后dnsmasq之后,缓存消失,容器内和主机的dns均出现问题 主机中
[root@myserver ~]# nslookup bbs.com
Server: 127.0.0.1
Address: 127.0.0.1#53
** server can't find bbs.com: REFUSED
容器内
bash-4.1# ping bbs.com
ping: unknown host bbs.com
当放通其中一个地区的dns服务之后,域名解析立即恢复
####3. 主机/etc/hosts测试 注意,如果使用nslookup命令,以下配置全部都会按照缓存来,如果是ping命令,会首先使用hosts的解析 主机中如果新增hosts,域名会立即生效 删除/etc/hosts, 域名更改也会立即生效 修改也会立即生效,如果出现缓存的情况,可以/etc/init.d/dnsmasq reload处理刷新
dig nslookup ping 这几个命令很好玩
####4. 剩余一个dns cache刷新时间的点没测试
###注意点:
- dnsmasql不单单是做dns解析的,他跟tftp dhcp ldap都有关系,老版本的安全漏洞很多,好像有的还还严重,尽量不要开到公网
- 开始不懂这个版本的配置listen-address老是不起左右,后来才看到解释原来要绑定设备
http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq.conf.example
上一篇: 100 个有趣的网站
下一篇: 趣味网站,创意网站:Playha.com