linux下nproc的作用(docker相关)

采集环境

[root@ht5 limits.d]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@ht5 limits.d]# uname -a
Linux ht5.node 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

linux的实现其实是一个轻量级的进程,对应的术语是:light weight process(LWP)。怎么知道一个用户创建了多少个进程呢,默认的ps是不显示全部进程的,需要‘-L' 才能看到所有的进程。

注意区别nproc命令,显示cpu数量

复制代码
[root@ht5 bin]# nproc
8

 [root@ht5 bin]# nproc --help
 Usage: nproc [OPTION]...
 Print the number of processing units available to the current process,
 which may be less than the number of online processors

 --all print the number of installed processors
 --ignore=N if possible, exclude N processing units
 --help display this help and exit
 --version output version information and exit


GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'nproc invocation'


复制代码

查看系统文件打开情况

查看当前系统打开的文件数量: lsof | wc -l  
查看当前进程的打开文件数量:lsof -p pid | wc -l  
举例: lsof -p 1892 | wc -l 
查看当前进程的最大可以打开的文件数:cat /proc/PID/limits  
  (如果通过ulimit -n 设置或者修改/etc/security/limits.conf,看看进程是否生效)  
查看系统总限制打开文件的最大数量:cat /proc/sys/fs/file-max

举例1:查看所有用户创建的进程数,使用命令:

复制代码
[root@ht5 bin]# ps h -Led -o user | sort | uniq -c | sort -n
      1 dbus
      1 libstoragemgmt
      1 rpc
      2 postfix
      7 polkitd
      8 100
     25 nfsnobody
     41 mockbuild
    163 rabbitmq
    567 root
复制代码

举例2.查看docker应用

复制代码
[root@ht5 limits.d]# cat /etc/systemd/system/multi-user.target.wants/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
复制代码

当日志出现以下情况中的一种时,需要考虑这个nproc:

1. Cannot create GC thread. Out of system resources
2. java.lang.OutOfMemoryError: unable to create new native thread

查看下nproc设置情况

复制代码
[root@ht5 limits.d]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 64014
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 655350  //nproc
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

[root@ht5 limits.d]# ulimit -u
655350

复制代码

Linux 下监控用户最大进程数参数(nproc)是否到达上限,并修改:

1.需要先看linux操作系统内核版本,通过uname -a查看内核版本,

[root@ht5 bin]# uname -a
Linux ht5.node 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

因为2.6版本的内核默认是在/etc/security/limits.d/90-nproc.conf里的配置会覆盖/etc/security/limits.conf的配置

centos6 是 90-nproc.conf, centos7是20-nproc.conf

复制代码
[root@ht5 bin]# cd /etc/security/limits.d/
[root@ht5 limits.d]# ls
20-nproc.conf

 [root@ht5 limits.d]# cat /etc/security/limits.d/20-nproc.conf
 # Default limit for number of user's processes to prevent
 # accidental fork bombs.
 # See rhbz #432903 for reasoning.

 * soft nproc 4096
 root soft nproc unlimited

注:

设置限制数量,第一列表示用户,*表示所有用户
soft 软限制,hard硬限制。当数量达到软限制的时候会出现报警,达到硬限制的时候才不会增加
soft nproc: 单个用户可用的最大进程数量(软限制)
hard nproc: 单个用户可用的最大进程数量(硬限制)
soft nofile:可打开的文件描述符的最大数(软限制)
hard nofile:可打开的文件描述符的最大数(硬限制)

复制代码

2. vi /etc/security/limits.d/20-nproc.conf 

[root@ht5 limits.d]# cat /etc/security/limits.d/20-nproc.conf 
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     4096
root       soft    nproc     unlimited
注释掉 * soft nproc ,改成
unlimited

3. vi /etc/security/limits.conf

  //limits.conf是Linux资源限制配置文件
 添加 
 *soft nproc 665530 或不设置

4.在控制台执行(修改这个不需要重启)

复制代码
[root@ht5 limits.d]# vi /etc/security/limits.conf

  echo "* soft nproc 65535"  >> /etc/security/limits.conf

  echo "* hard nproc 65535"  >> /etc/security/limits.conf

[root@ht5 limits.d]#  ulimit -u
64014
[root@ht5 limits.d]# ulimit -u 655350
//注意: 普通用户nproc值是不生效的,需要修改/etc/security/limits.d/20-nproc.conf 文件中的值(centos7下)

  系统总限制

  其实上面的nproc的值也只是表象,普通用户最大进程数无法达到 655350,因为用户的 max user processes 的值,最后是受全局的 kernel.pid_max 的值限 制的。
  也就是说 kernel.pid_max=131072,那么你用户的nproc的值是131072的话,用户能打开的最大进程数还是131072.

  查看pid_max方法:

 [root@ht5 limits.d]# cat /proc/sys/kernel/pid_max
 131072

 [root@ht5 limits.d]# sysctl kernel.pid_max
 kernel.pid_max = 131072

 修改这个值方法:

[root@ht5 limits.d]# echo 65535 > /proc/sys/kernel/pid_max

[root@ht5 limits.d]# sysctl -p //立即生效

复制代码

5.检查下是否生效,在控制台切到该用户下

[root@ht5 limits.d]#  ulimit -u
655350

6.pam_limits   
  限制用户在会话过程中对系统资源的使用

复制代码
[root@ht5 limits.d]# locate pam_limits
/usr/lib/security/pam_limits.so  //兼容32位
/usr/lib64/security/pam_limits.so //内核级64位
/usr/share/doc/pam-1.1.8/html/sag-pam_limits.html
/usr/share/doc/pam-1.1.8/txts/README.pam_limits
/usr/share/man/man8/pam_limits.8.gz
/var/lib/docker/overlay2/1a1191d92763bd8957f86381e2c8cc4266a6421d4a6c18fc577e0915151e337f/diff/lib/x86_64-linux-gnu/security/pam_limits.so
/var/lib/docker/overlay2/1a1191d92763bd8957f86381e2c8cc4266a6421d4a6c18fc577e0915151e337f/diff/usr/share/man/man8/pam_limits.8.gz
/var/lib/docker/overlay2/4bf969fa1b9da48aa98c78a228d9c8db398b28e832bebc85788d9af355a4530f/diff/lib/x86_64-linux-gnu/security/pam_limits.so
/var/lib/docker/overlay2/4bf969fa1b9da48aa98c78a228d9c8db398b28e832bebc85788d9af355a4530f/diff/usr/share/man/man8/pam_limits.8.gz
/var/lib/docker/overlay2/64bbbf1da98fcb698692d0dafd4f58f13f74b734a42e92476e7b8072e14368fc/diff/usr/lib64/security/pam_limits.so
/var/lib/docker/overlay2/d45b815884f804f151452ac8df2425f8ce6701507c85a949ad97ac00a07f11a8/diff/lib/x86_64-linux-gnu/security/pam_limits.so
/var/lib/docker/overlay2/d45b815884f804f151452ac8df2425f8ce6701507c85a949ad97ac00a07f11a8/diff/usr/share/man/man8/pam_limits.8.gz
/var/lib/docker/overlay2/ddedd80a43db6cbca935712ae606a68048cbfee18dfc10c7902ccce4bbfb2478/diff/lib/x86_64-linux-gnu/security/pam_limits.so
他的默认配置文件是/etc/security/limits.conf,可以用conf=/path/to/limits.conf来制定另外一个配置文件
limits.conf的格式:
<domain>  <type>  <item>  <value>
domain: 可以使一个用户,一个组(@group)或者是通配符*(任何用户)、%(只限制maxlogins,可以用%group这样的语法)。
type: soft或者hard(软限制,硬限制)--资源类型比如:nproc等
item:
   core:         限制核心文件的大小 (KB)--limits the core file size(kb)
  data:         最大的数据包大小(KB)
  fsize:         最大的文件大小(KB)
  memlock:       最大可用的内存空间(KB)
  nofile:        最大可以打开的文件数量--max number of open file descriptors
  rss:         最大的可驻留空间(KB)--max resident set size(kb)
  stack:         最大的堆栈空间(KB)--max stack size (kb)
  cpu:         最大的CPU占用时间(分钟)--max cpu time(MIN)
  nproc:         最大用户进程数--max user of processes
  as:        地址空间限制(KB)--address space limit(kb)
  maxlogins:     该用户可以登录到系统的最多次数
   maxsyslogins:  系统能够登陆的最大用户数
   priority:      优先运行的用户进程(负值越高的进程优先)---max nice priority allowed to raise to values :[-20,19]
   locks:         最大可锁定文件的数目(Linux2.4及以上内核的系统)
   sigpending:    最大数量的等待信号(Linux2.6及以上内核)
   msqqueue:      POSIX信息队列的最大可使用的内存(bytes)(Linux2.6及以上内核)
value: 和item是成对的。对应着加就可以了。

想看代码可以到 http://www.linux-pam.org/library/

  [root@ht5 security]# rpm -qa | grep pam
  pam-1.1.8-23.el7.x86_64  //el7即centos7
  pam-1.1.8-23.el7.i686
  fprintd-pam-0.8.1-2.el7.x86_64

复制代码

 

posted @   jinzi  阅读(1297)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示