DevOps チームの うすい と申します。
標題の通り、減らしていきます。
nscdでDNS問い合わせを減らす
当社のレアジョブ英会話の主要なサーバは AWS 上にあるのですが、AWS では このような 制限が存在し、ある程度のアクセスに達すると DNS の問い合わせに失敗してしまいます。 実際にレアジョブ英会話でもレッスンが多い時間帯において、データベース接続エラーが発生しました。
そこで、DNS への問い合わせ件数を減らすために問い合わせ結果を各サーバ内でキャッシュすることとしました。
下記の記事も参考にしてください。
上記記事では dnsmasq を使用していますが、今回は nscd を使いたいと思います。
インストール後、設定ファイルを変更して起動するだけとなります。 設定ファイルの例です。
# # /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 0 # reload-count 5 paranoia yes restart-interval 60 enable-cache passwd yes positive-time-to-live passwd 600 negative-time-to-live passwd 20 suggested-size passwd 211 check-files passwd yes persistent passwd yes shared passwd yes max-db-size passwd 33554432 auto-propagate passwd yes enable-cache group yes positive-time-to-live group 3600 negative-time-to-live group 60 suggested-size group 211 check-files group yes persistent group yes shared group yes max-db-size group 33554432 auto-propagate group yes enable-cache hosts yes positive-time-to-live hosts 3600 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 enable-cache services yes positive-time-to-live services 28800 negative-time-to-live services 20 suggested-size services 211 check-files services yes persistent services yes shared services yes max-db-size services 33554432 enable-cache netgroup no positive-time-to-live netgroup 28800 negative-time-to-live netgroup 20 suggested-size netgroup 211 check-files netgroup yes persistent netgroup yes shared netgroup yes max-db-size netgroup 33554432
定期的に nscd 自身には再起動してもらいたいので、paranoia
をyes
にしてrestart-interval
を設定しています。
netgroup は使用していないのでenable-cache
をno
にしています。/etc/netgroup
を nscd が見に行ってエラー吐いちゃいますので。
あと nscd が再起動されるとその度にログが吐かれるので、/var/log/messages
的なのがまぁまぁ増えますのでご注意ください。
また、AWS を使用しているとあるあるだと思いますが、各 Endpoint の後ろにいるインスタンスなりは AWS 側のタイミングで切り替わるので、再起動の間隔やキャッシュ保持時間などは調整する必要があるかと思います。
結果
tcpdump dst port 53
などしてみた結果です。
nscd 有効
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 3624 packets captured 3636 packets received by filter
無効
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 10444 packets captured 10449 packets received by filter
減らせました。
レアジョブ英会話は PHP で動いており、ABI が変更されたときに出るようなgetaddrinfo failed
の発生にびっくりしたのですが、今回の設定で落ち着いた次第です。