Loading

CentOS8环境下ulimit最大打开文件数不生效问题

一、问题

因业务调整,需重新部署监控服务器,选择zabbix5.2 rpm安装。安装完成后,在调整系统打开文件数的过程中,遇到如下情况。

系统打开文件数设置
[root@centos ~]# ulimit  -n
65535
[root@centos ~]# tail -n 12  /etc/security/limits.conf 
zabbix               soft    nofile            40960
zabbix               hard    nofile            65535
zabbix               soft    nproc            20480
zabbix               hard    nproc            40960
... ... 忽略了一些文本内容

查看zabbix服务的打开文件数限制,找到进程号
 └─zabbix_server(1816)

[root@centos ~]# cat /proc/1816/limits 
Limit                     Soft Limit           Hard Limit           Units     
... ... 忽略了一些文本内容
Max open files            1024                 262144               files    
... ... 忽略了一些文本内容

查看后,发现没有生效。
二、找到原因,解决问题

因为ulimit和limits.conf的配置只针对登录用户,而对systemd管理的服务不起作用,服务的limit要在service文件中单独指定
服务目录路径 /usr/lib/systemd/system/

修改/usr/lib/systemd/system/zabbix-server.service文件, [Service]下追加一行LimitNOFILE=65535

[root@centos ~]# cat /usr/lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
... ... 忽略了一些文本内容

[Service]
... ... 忽略了一些文本内容
TimeoutSec=0
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

重新加载systemd配置
[root@centos ~]# systemctl  daemon-reload

重启服务
[root@centos ~]# systemctl  restart zabbix-server.service 

三、验证问题是否解决

查看zabbix服务的打开文件数限制,找到进程号找到进程号
─zabbix_server(2641)

[root@centos ~]# cat /proc/2641/limits 
Limit                     Soft Limit           Hard Limit           Units     
... ... 忽略了一些文本内容
Max open files            65535                65535                files     
... ... 忽略了一些文本内容

四、引用参考链接及部分内容

感谢知乎作者路人甲的世界 https://zhuanlan.zhihu.com/p/111364906

对于systemd,到底是否依旧沿用PAM模块实现资源限制呢?
我在RedHat Bugzilla找到了Systemd最初被引入时的一篇Ticket:Bug 754285 - Hint that /etc/security/limits.conf does not apply to systemd services。
帖子中提到了一模一样的问题。Systemd的作者之一Kay Sievers当时给与了以下回复:
Systemd does not support global limits, the file is intentionally ignored. 
LimitNOFILE= in the service file can be set to specify the number of open file descriptors for a specific service.

也就是说,Systemd设计的时候故意忽略了全局限制,转而在配置文件中配置对每个服务的资源限制,结合/etc/security/limits.conf文件开头的注释来看,果然如此:

# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
...
既然了解了Systemd不会遵循PAM模块的配置,那么接下来要做的就是思考如何在Systemd的配置文件中设置资源限制。
posted @ 2021-04-24 10:54  后边跟上。  阅读(1152)  评论(0编辑  收藏  举报