linux ulimit 调优
linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为进程打开了太多的文件,导致open files数目不够。这就需要修改参数进行调优。特别是有大量文件访问的应用,如elasticsearch、filebeat等, 更要注意这个问题。
网上的很多教程,都只是简单的说明如何设置,但很多东西没讲明白,而且有时候设置后却不能生效,关于其验证也比较模糊。这里对使用的一些经验进行整理。 查看进程的limits和打开的文件数
查看filebeat打开的文件数
root@filebeatserver:~# ll /proc/$(pidof filebeat)/fd | wc -l
23
root@filebeatserver:~# cat /proc/3108/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 4096 4096 processes
Max open files 65536 65536 files
Max locked memory 16777216 16777216 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 128421 128421 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=655360Specifies the maximum number of processes
LimitNPROC=40960
# 查看filebeat进程打开的文件数
root@filebeatserver:~# ll /proc/$(pidof filebeat)/fd | wc -l
23查看filebeat进程的limits配置,其中包含'max open files'
root@filebeatserver:~# cat /proc/$(pidof filebeat)/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 4096 4096 processes
Max open files 65536 65536 files
Max locked memory 16777216 16777216 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 128421 128421 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
QA: limits配置后不生效
确保配置正确,ubuntu一定要使用兼容写法,即指定用户,不要使用*。
/etc/security/limits.conf其实是 Linux PAM模块的配置文件,检查pam 模块被正确加载