DNS(二)之構建域名解析緩存

域名解析緩存的必要性

在部署服務的時候,不少程序須要使用域名解析的功能,通常配置/etc/resovl.conf去指定DNS服務器的IP,可是若是程序發起的請求量較大,那麼服務器就容易被DNS服務器禁止訪問(至於爲何會被禁止,你懂得),同事每次去外網請求DNS解析的話,時延也特別大,乃至發生請求超時的狀況,這個是,咱們就要配置一個透明的DNS解析緩存服務器,達到的效果以下:linux

  1. 優化DNS響應速度 經過緩存DNS的請求結果,後續相同的DNS請求不須要在訪問公網的DNS請求即可得到結果,減小了網絡訪問的延時。
  2. 減小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當前緩存條目的
相關文章
相關標籤/搜索