Thanos错误:Accept error: accept tcp [::]:19194: accept4: too many open files; retrying in 1s

问题简述:Centos7下修改系统的最大文件打开数的时候,对系统启动的进程不生效

问题详述:Centos7下需修改系统最大文件打开数为100000,进程数为50000,于是做了如下操作

说 明:此问题只出现在centos7下,centos6版本不存在此问题

1:记录未修改之前的ulimit值

img

2:修改配置文件

vim /etc/security/limits.conf 在后面添加

1
2
3
4
*      soft    nofile  100000
* hard nofile 100000
* soft nproc 65535
* hard nproc 65535

重启机器,修改 /etc/security/limits.conf 里的配置后,需重启机器才能生效

3:查看修改后的ulimit值

img

4:在这里看起来一切都很正常,也许一不小心就入坑了。为了对比现象,需要安装两个nginx,一个源码安装,一个yum安装

源码安装:(简单安装,只是为了验证和测试,不指定安装路径,默认是在/usr/local/nginx下)

1
2
3
4
5
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure
make && make install

yum安装

1
yum -y install nginx

5:现象

1)先启动源码编译的nginx,并查看进程号及limit值,如下:

启动nginx: /usr/local/nginx/sbin/nginx

查看进程: ps -ef |grep nginx

查看某个进程的limit值: cat /proc/进程号/limits

img

2)yum安装nginx启动

启动方法:systemctl start nginx.service ,然后查看进程号及其limit值,如下:

img

到这里问题就来了,为什么通过systemctl启动的nginx对limit的设置不生效 ?????

然后查看了1号进程的limit值( cat /proc/1/limits ),发现也是对修改/etc/security/limits.conf文件里的最大文件打开数和最大进程数没有生效

6:原因

仔细查看/etc/security/limits.conf文件的注释,说明了对系统服务不生效

img

7:解决办法

在Centos7系统中,使用Systemd替代了之前的SysV。/etc/security/limits.conf文件的配置作用域缩小了。/etc/security/limits.conf的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。因此登录用户的限制,通过/etc/security/limits.conf与/etc/security/limits.d下的文件设置即可。

对于systemd service的资源设置,则需修改全局配置,全局配置文件放在/etc/systemd/system.conf和/etc/systemd/user.conf,同时也会加载两个对应目录中的所有.conf文件/etc/systemd/system.conf.d/.conf和/etc/systemd/user.conf.d/.conf。system.conf是系统实例使用的,user.conf是用户实例使用的。

vim /etc/systemd/system.conf

1
2
DefaultLimitNOFILE=100000
DefaultLimitNPROC=65535

方法有二

  • 重启主机
  • 执行 systemctl daemon-reexec

使用daemon-reload无效原因

单纯使用 systemctl daemon-reload 是不会对/etc/systemd/system.conf刷新的,引用下serverfault.com对systemd-system.conf的说明:

No, daemon-reload will reload all unit files, not the configuration for systemd itself. However, # systemctl daemon-reexec will re-execute systemd and cause it to digest its new configuration in the process.

From the systemctl man page:

1
2
3
4
5
6
7
8
daemon-reexec
Reexecute the systemd manager. This will serialize the manager
state, reexecute the process and deserialize the state again. This
command is of little use except for debugging and package upgrades.
Sometimes, it might be helpful as a heavy-weight daemon-reload.
While the daemon is being reexecuted, all sockets systemd listening
on behalf of user configuration will stay accessible.
12345678

When the man page says daemon-reexec is useful for package upgrades, it in large part means that this command executes whatever new binaries there are and re-processes its configs. HOWEVER, the RPM that we use to upgrade systemd already contains a script to do this, so it is usually never needed in the case of a normal upgrade.

Or you can reboot. Either will do.

也就是说,daemon-reexec 会重新执行systemd管理器,重新读取系统配置文件,而daemon-reload只会去读service部分的配置,不包含全局配置/systemd/system.conf,相当于重量级的daemon-reload.

重启后,systemctl start nginx启动,然后根号进程号查看资源限制,得到

img

8:用到的相关命令

1)查看当前进程的最大可以打开的文件数

1
cat /proc/进程ID/limits

2)查看当前进程实时打开的文件数

1
lsof -p PID |wc -l 

3)查看系统总限制打开文件的最大数量

1
cat /proc/sys/fs/file-max

注:若设置不生效,查看包含的目录下的配置文件是否覆盖,如/etc/security/limits.d/下的文件是否覆盖了/etc/security/limits.conf设置的值


Thanos错误:Accept error: accept tcp [::]:19194: accept4: too many open files; retrying in 1s
https://johnnysxy.github.io/2023/08/16/Thanos错误-Accept-error-accept-tcp-19194-accept4-too-many-open-files-retrying-in-1s/
作者
Johnny Song
发布于
2023年8月16日
许可协议