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服务安装

1
yum -y install nscd

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# /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服务:

1
systemctl start nscd

4、查看nscd缓存数:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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、清除指定类型缓存

1
2
3
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 @   人艰不拆_zmc  阅读(5317)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2014-08-03 hduPiggy-Bank(完全背包)
点击右上角即可分享
微信分享提示