Centos7开启nscd缓存服务以加快DNS解析速度

1、介绍:

  nscd(Name Service Cache Daemon)是一种能够缓存 passwd、group、hosts 的本地缓存服务,分别对应三个源 /etc/passwd/etc/hosts/etc/resolv.conf。其最为明显的作用就是加快 DNS 解析速度,在接口调用频繁的内网环境建议开启。默认Linux服务器是不会对DNS解析记录进行缓存的,系统安装了nscd之后,几乎所有的DNS查询都会经过nscd,可以避免很多重复的DNS查询。

2、开启NSCD DNS缓存服务的优点和缺点:

  • 优点
  1. 本地缓存DNS解析信息,提供解析速度。
  2. DNS服务挂了也没有问题,在缓存服务时间范围内,解析依旧正常。
  • 缺点
  1. DNS解析信息会滞后,如域名解析更改需要手动刷新缓存,NSCD不适合做实时的切换的应用,目前对于依赖DNS切换的服务,建议不要开启DNS缓存。
  2. DNS Cache作为普通的DNS解析Cache那是没问题的,如果你使用RDS云服务器,也不建议使用DNS缓存服务。

3、nscd安装配置:

默认centos7服务器没有安装nscd服务,需要使用以下命令进行nscd服务安装

yum -y install nscd

修改/etc/nscd.conf配置文件,只进行dns缓存,内容如下:

#
# /etc/nscd.conf
#
# An example Name Service Cache config file.  This file is needed by nscd.
#
# Legal entries are:
#
#       logfile                 <file>
#       debug-level             <level>
#       threads                 <initial #threads to use>
#       max-threads             <maximum #threads to use>
#       server-user             <user to run server as instead of root>
#               server-user is ignored if nscd is started with -S parameters
#       stat-user               <user who is allowed to request statistics>
#       reload-count            unlimited|<number>
#       paranoia                <yes|no>
#       restart-interval        <time in seconds>
#
#       enable-cache            <service> <yes|no>
#       positive-time-to-live   <service> <time in seconds>
#       negative-time-to-live   <service> <time in seconds>
#       suggested-size          <service> <prime number>
#       check-files             <service> <yes|no>
#       persistent              <service> <yes|no>
#       shared                  <service> <yes|no>
#       max-db-size             <service> <number bytes>
#       auto-propagate          <service> <yes|no>
#
# Currently supported cache names (services): passwd, group, hosts, services
#
#   logfile                 /var/log/nscd.log
    threads                 4
    max-threads             32
    server-user             nscd
    stat-user               somebody
    debug-level             5
    reload-count            5
    paranoia                no
    restart-interval        3600


    enable-cache            hosts           yes
    enable-cache            passwd          no
    enable-cache            group           no
    enable-cache            services        no
    positive-time-to-live   hosts           5
    negative-time-to-live   hosts           20
    suggested-size          hosts           211
    check-files             hosts           yes
    persistent              hosts           yes
    shared                  hosts           yes
    max-db-size             hosts           33554432

启动nscd服务:

systemctl start nscd

4、查看nscd缓存数:

nscd缓存DB文件在/var/db/nscd下。可以通过nscd -g查看统计的信息,这里列出dns缓存部分:

hosts cache:

            yes  cache is enabled
            yes  cache is persistent
            yes  cache is shared
            211  suggested size
         216064  total data pool size
            432  used data pool size
              5  seconds time to live for positive entries
             20  seconds time to live for negative entries
              1  cache hits on positive entries
              0  cache hits on negative entries
            209  cache misses on positive entries
            483  cache misses on negative entries
              0% cache hit rate
              4  current number of cached values
            140  maximum number of cached values
              4  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

5、清除指定类型缓存

nscd -i passwd
nscd -i group
nscd -i hosts

除了上面的方法,重启NSCD服务同样可以达到清理Cache的目的。

参考:https://docs.ucloud.cn/uhost/public/dns_setting?id=step-1-%e9%85%8d%e7%bd%ae%e5%86%97%e4%bd%99dns-server%e5%9c%b0%e5%9d%80

参考:http://www.361way.com/linux-nscd-dns-cache/4265.html

posted @ 2020-08-03 18:38  人艰不拆_zmc  阅读(5261)  评论(1编辑  收藏  举报