前言: 前段在公司内部弄了一台DNS缓存服务器,但跑了一段时间后总感觉有点慢,希望加快缓存,记得上家公司的同事说过使用nscd可以加速DNS缓存速度,nscd会缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd, /etc/hosts 和 /etc/resolv.conf每个库保存两份缓存,一份是找到记录的,一份是没有找到记录的。每一种缓存都保存有生存时间(TTL)。其作用就是在本当中增加cache ,加快DNS的解析速度
一、nscd安装和配置
- 我这边使用的是CentOS,安装时候就一条命令
yum install nscd
- 安装完成,现在就进行配置,配置文件位置
/etc/nscd.conf
,配置如下
# /etc/nscd.conf
logfile /var/log/nscd.log
threads 10
max-threads 128
server-user nscd
debug-level 0
paranoia no
enable-cache passwd no
enable-cache group no
enable-cache hosts yes
positive-time-to-live hosts 60
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
我们只是开启DNS cache 缓存,所以最重要的一条配置是
enable-cache hosts yes
二、nscd启用和验证
- 进行设置开机自启和手动进行启动
CentOS 7 以上的的系统可以使用以下命令
systemctl enable nscd
systemctl start nscd
CentOS 7以下的系统可以使用以下命令
chkconfig nscd on
service nscd start
- 进行查看缓存信息,缓存DB文件在/var/db/nscd下,可以使用命令
nscd -g
进行查看
[root@gz--vm-dnscache-0001 ~]# nscd -g
nscd 配置:
0 服务器调试级别
91d 21h 49m 4s server runtime
10 current number of threads
128 maximum number of threads
0 number of times clients had to wait
no paranoia mode enabled
3600 restart internal
5 reload count
passwd cache:
no cache is enabled
no cache is persistent
no cache is shared
0 suggested size
0 total data pool size
0 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/passwd for changes
......
- 清除缓存
nscd -i passwd
nscd -i group
nscd -i hosts
三、 nscd 部分配置说明
相关参数的解释如下:
logfile debug-file-name
指定调试信息写入的文件名。
debug-level value
设置希望的调试级别。
threads number
这是启动的等待请求的线程数。最少将创建5个线程。
server-user user
如果设置了该选项,nscd将作为该用户运行,而不是作为root。如果每个用户都使用一个单独的缓存(-S参数),将忽略该选项。
enable-cache service <yes|no>
启用或禁用制定的 服务 缓存。
positive-time-to-live service value
设置 service 在指定缓存中正的项目(成功的请求)的TTL(存活时间)。 Value 以秒为单位。较大的值将增加缓存命中率从而减低平均响应时间,但是将增加缓存的一致性问题。
negative-time-to-live service value
设置 service 在指定缓存中负的项目(失败的请求)的TTL(存活时间)。 Value 以秒为单位。如果存在由不在系统数据库中的uid(用户ID)(例如在以root身份解包linux 内核源代码时)所拥有的文件将明显改善性能;应该维持较小的值以降低缓存一致性问题。
suggested-size service value
这是内部散列表的大小, value 应该保持一个素数以达到优化效果。
check-files service <yes|no>
启用或禁用检查属于指定 服务 的文件的改变。这些文件是 /etc/passwd, /etc/group, 以及 /etc/hosts。
四、为什么配置 nscd 加速缓存
- DNS 解析快慢严重影响了上网的体验,我们公司所有的办公设备DNS服务器都是使用内部DNS Cache服务器,两百台电脑不停向内部DNS Cache 发起请求解析,算不上特别繁忙,但是在高峰时候进行这个缓存确实是有些影响,提高一点
网友评论