随笔 - 137  文章 - 1  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔分类

随笔档案

收藏夹

调试技巧

搜索

  •  

最新评论

阅读排行榜

评论排行榜

一、概述

 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等。 当然,除了这些显而易见的改变之外,有些变化并不是那么引人注目,例如这里要介绍的你可能发现曾经配置了正确的 /etc/security/limits.conf 在 CentOS 7 中却没有生效了。

  如果遇到上面的问题,很可能已经导致了你的应用异常了,例如:以前配置了服务的 nproc 为一个非常大的数值, 结果现在发现服务不能打开更多的文件。

  惊讶之余,查看服务的 /proc/<pid>/limits 却发现:

1 Max open files            1024                1024                files

  那么,为什么服务使用 systemd 启动就没有生效呢?

  根据 redhat bugzilla 754285, systemd 实际上是会忽略 /etc/security/limits.conf,下面是 systemd 2 号人物的回答:

  而这个 bug 也标记为已解决,在 pam 包中解决,这个包包含了 /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. #

注意,这里的 system services 指的是 system wide service,对于 CentOS 7 来说也就是系统的 service。

  systemd 还可以对普通用户启动,使用 --user 参数。

二、解决之道

 在CentOS 7 / RHEL 7的系统中,使用Systemd替代了之前的SysV,因此 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf这里的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。登录用户的限制,与上面讲的一样,通过 /etc/security/limits.conf 和 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用户实例使用的。一般的sevice,使用system.conf中的配置即可。systemd.conf.d/*.conf中配置会覆盖system.conf。

1 DefaultLimitCORE=infinity 2 DefaultLimitNOFILE=100000 3 DefaultLimitNPROC=100000

  然后运行如下命令,才能生效。

1 sudo systemctl daemon-reload 2 sudo systemctl restart nginx.service

查看一个进程的limit设置:cat /proc/YOUR-PID/limits
例如我的一个nginx service的配置效果:

复制代码
 1 $cat /proc/$(cat /var/run/nginx.pid)/limits  2 Limit                     Soft Limit           Hard Limit           Units  3 Max cpu time              unlimited            unlimited            seconds  4 Max file size             unlimited            unlimited            bytes  5 Max data size             unlimited            unlimited            bytes  6 Max stack size            8388608              unlimited            bytes  7 Max core file size        unlimited            unlimited            bytes  8 Max resident set          unlimited            unlimited            bytes  9 Max processes             100000               100000               processes 10 Max open files            100000               100000               files 11 Max locked memory         65536                65536                bytes 12 Max address space         unlimited            unlimited            bytes 13 Max file locks            unlimited            unlimited            locks 14 Max pending signals       1030606              1030606              signals 15 Max msgqueue size         819200               819200               bytes 16 Max nice priority         0                    0 17 Max realtime priority     0                    0 18 Max realtime timeout      unlimited            unlimited            us
复制代码

  顺便提一下,我还被CentOS7自带的/etc/security/limits.d/20-nproc.conf文件坑过,里面默认设置了非root用户的最大进程数为4096,难怪我上次在limits.conf中设置了没啥效果,原来被limit.d目录中的配置覆盖了。

posted on 2021-03-01 20:30 长戟十三千 阅读(277) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理