好久没来写博客,并不是最近没有总结技术,而是已经习惯用evernote来整理工作中碰到的技术问题,最近公司不是很忙,申请work form home一个月,把以前的技术整理一下发到博客来,今天讲讲如何配置Dnsmasq这款软件来提速局域网内的广告前端,后端服务器。

我们公司是做互联网广告精准投放的,位于华为云上的AD BackEnd Server有60台,AD FrontEnd Server有60台,都是处于一个局域网中,核心软件是俄罗斯大牛开发的分布式软件。AD环境DNS Server有一主一备,用的是传统的bind9。但是由于ADBE 和ADFE之间大量数据的通信,所以在每台AD服务器上又安装的Dnsmasq软件来缓存和加速域名解析。


首先man 一下看看软件的简介。

dnsmasq - A lightweight DHCP and caching DNS server. 一个轻量级的DHCP和DNS缓存服务。

Dnsmasq既能做DNS服务,也能做DHCP服务,我们生产环境用到的只是DNS缓存功能。(这货在ubuntu 桌面版里也有使用。)

首先看一下我们生产环境下用到的配置


## CHANGE-4936.dnsmasq.conf, CCAP adfes
port=53
user=dnsmasq
group=dnsmasq
bind-interfaces                         # slaves must use this option
no-dhcp-interface=
server=10.86.*。* # lb00
server=10.1.*。*# us00pro
resolv-file=          #it uses /etc/resolv.conf as default
strict-order
dns-forward-max=150
cache-size=500       # the size of dnsmasq’s cache. The default is 150 names
neg-ttl=60
local-ttl=300        # [used] When replying with information from /etc/hosts or the DHCP leases file
log-async=20          # Enable  asynchronous logging - for non-blocking logging
log-facility=/var/log/dnsmasq.log
# clear-on-reload                       # Whenever /etc/resolv.conf is re-read, clear the DNS cache



bind-interfaces 默认绑定的是本地lo 地址,如果机器同时运行bind的话,此选项必须打开。


strict-order是指严格按照/etc/resolv.conf文件逐行解析,如下所示,本地服务器会先请求127.0.0.1,如果响应时间内没有结果,就会向8.8.8.8询问。如果配置Dnsmasq服务的话,第一个nameserver必须是127.0.0.1。否则就没有意义了

[root@defcon:/var/log15:41]#cat /etc/resolv.conf

; generated by /sbin/dhclient-script

search ap-southeast-1.compute.internal

nameserver 127.0.0.1

nameserver 8.8.8.8

一下是我在生产环境下从一台ADBE 服务器解析ADFE服务器的所用时间截图,很明显,使用本地dnsmasq的情况下可以节省很多时间。

wKiom1TwJhrD9VwqAAQ46VmVy9U446.jpg



wKioL1TwJyfTkq-LAARzSMcF9_E197.jpg