一、問題
因業務調整,需重新部署監控服務器,選擇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的配置文件中設置資源限制。