域名解析緩存的必要性
在部署服務的時候,很多程序需要使用域名解析的功能,一般配置/etc/resovl.conf去指定DNS服務器的IP,但是如果程序發起的請求量較大,那么服務器就容易被DNS服務器禁止訪問(至於為什么會被禁止,你懂得),同事每次去外網請求DNS解析的話,時延也特別大,乃至發生請求超時的情況,這個是,我們就要配置一個透明的DNS解析緩存服務器,達到的效果如下:
- 優化DNS響應速度 通過緩存DNS的請求結果,后續相同的DNS請求不需要在訪問公網的DNS請求便可獲得結果,減少了網絡訪問的延時。
- 減少DNS服務器對公網的依賴 在緩存周期內,相同的DNS請求不再發生到外網的網絡通信行為,可以減少短暫的外部網絡不可用導致的影響
NSCD安裝配置方法
NSCD(Name server caching Daemon 名稱服務緩存進程)不需要對應用程序或者解析器做任何修改,/etc/resovl.conf 也不需要做任何變化,對於系統部署的影響最小。因此NSCD成為linux環境最為廣泛的域名緩存軟件,本次使用該軟件作為域名緩存服務,在CentOS6.6上,安裝NSCD的方法比較簡單,使用yum安裝即可,命令如下:
[root@localhost ~]# yum -y install nscd
[root@localhost ~]# ls /etc/nscd.conf
/etc/nscd.conf #nscd的配置文件
我們看看配置文件,我們主要關心下面這幾段,yum安裝好后在63行-70行:
63 enable-cache hosts yes
64 positive-time-to-live hosts 3600
65 negative-time-to-live hosts 20
66 suggested-size hosts 211
67 check-files hosts yes
68 persistent hosts yes
69 shared hosts yes
70 max-db-size hosts 33554432
參數解釋
- enable-cache 指定對DNS解析進行緩存
- positive-time-to-live 對解析成功的DNS結果進行緩存時間
- negative-time-to-live 指對解析成功的DNS結果進行緩存的時間,例如網絡故障導致DNS解析失敗或者請求的DNS條目沒有配置等
- suggested-size 是NSCD內部哈希表的大小,如果緩存條目數量大於默認的211,如大於10倍,那么修改此值。
- check-files 指是否檢查/etc/hosts文件的變化
- persistent 重啟NSCD進程時是否保留已緩存的條目
- shared 是否允許客戶端直接查詢NSCD的內存鏡像以獲得結果
- max-db-size 是指DNS的緩存大小,以字節為單位
啟動服務
[root@localhost store]# /etc/init.d/nscd start
驗證域名緩存
[root@localhost store]# nscd -g
'''''
hosts cache:
yes cache is enabled
yes cache is persistent
yes cache is shared
211 suggested size
216064 total data pool size
520 used data pool size
3600 seconds time to live for positive entries
20 seconds time to live for negative entries
0 cache hits on positive entries
0 cache hits on negative entries
0 cache misses on positive entries
0 cache misses on negative entries
0% cache hit rate
0 current number of cached values
0 maximum number of cached values
0 maximum chain length searched
0 number of delays on rdlock
0 number of delays on wrlock
0 memory allocations failed
yes check /etc/hosts for changes
[root@localhost ~]# wget -SO /dev/null http://www.baidu.com/ #換不同的域名多wget幾次,就發現這數值上去了。
[root@localhost store]# nscd -g
'''''
hosts cache:
yes cache is enabled
yes cache is persistent
yes cache is shared
211 suggested size
216064 total data pool size
520 used data pool size
3600 seconds time to live for positive entries
20 seconds time to live for negative entries
0 cache hits on positive entries
0 cache hits on negative entries
15 cache misses on positive entries
0 cache misses on negative entries
0% cache hit rate
3 current number of cached values
5 maximum number of cached values
0 maximum chain length searched
0 number of delays on rdlock
0 number of delays on wrlock
0 memory allocations failed
yes check /etc/hosts for changes
[root@localhost ~]# nscd -i hosts # 清空hosts當前緩存條目的
