centos7 如何在用户级对资源进行限制

摘要:
我们已经知道,centos7上建议使用sytemd进行资源限制。本文主要介绍如何使用systemd进行用户级资源限制。方案一临时有效配置如下:#systemctlset-propertyuser-1000.sliceMemoryLimit=200M#systemctldaemon-reload一般情况下,以上设置就可以了。如果进程因为OOM被kill,会在/var/log/message中发现记录。

我们已经知道,centos7上建议使用sytemd进行资源限制。

本文主要介绍如何使用systemd进行用户级资源限制。
以orange用户为例。

方案一 临时有效

配置如下:

# systemctl set-property user-1000.slice MemoryLimit=200M
# systemctl daemon-reload

一般情况下,以上设置就可以了。
但有时还是会遇到以下问题:

# systemctl set-property user-1000.slice MemoryLimit=200M

Failed to set unit properties on user-1000.slice: Unit user-1000.slice is not loaded.

User with id 1007 not logged in. First login as that user then set limits

解决办法
首先使用

# systemctl start user-1000.slice

再进行设置

# systemctl set-property user-1000.slice MemoryLimit=200M
# systemctl daemon-reload

方案二 永久生效

首先,编写slice文件user-1000.slice

其中1000是orange用户的uid,可用命令查看

# id -u username

文件内容如下

[Unit]
Description=orange user.slice

[Slice]
MemoryAccounting=true
MemoryLimit=200M

其次,拷贝到指定位置

# cp user-1000.slice  /usr/lib/systemd/system

启用

# systemctl start user-1000.slice

重新加载配置

# systemctl daemon-reload

查看系统中的slice

# systemctl -t slice
UNIT                                                          LOAD   ACTIVE SUB    DESCRIPTION
-.slice                                                       loaded active active Root Slice
system-getty.slice                                            loaded active active system-getty.slice
system-selinuxx2dpolicyx2dmigratex2dlocalx2dchanges.slice loaded active active system-selinuxx2dpolicyx2dmigratex2dlocalx2dchanges.slice
system.slice                                                  loaded active active System Slice
user-0.slice                                                  loaded active active User Slice of root
user-1000.slice                                               loaded active active orange user.slice
user.slice                                                    loaded active active User and Session Slice

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

7 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

查看某个具体的slice

# systemctl status user-1000.slice -l
● user-1000.slice - orange user.slice
   Loaded: loaded (/usr/lib/systemd/system/user-1000.slice; static; vendor preset: disabled)
  Drop-In: /etc/systemd/system/user-1000.slice.d
           └─50-MemoryLimit.conf
   Active: active since 四 2018-08-02 15:17:01 CST; 1min 40s ago
   Memory: 1.5M (limit: 200.0M)
   CGroup: /user.slice/user-1000.slice
           └─session-14973.scope
             └─56361 /export/servers/orange-agent/orange-guard/orange-guard -c cfg.json

8月 02 15:17:01 A02-R05-I79-201-3V98WK2.ORANGE.LOCAL systemd[1]: Created slice orange user.slice.
8月 02 15:17:01 A02-R05-I79-201-3V98WK2.ORANGE.LOCAL systemd[1]: Starting orange user.slice.
8月 02 15:17:01 A02-R05-I79-201-3V98WK2.ORANGE.LOCAL CROND[56324]: (orange) CMD (`which taskset` -c `source /export/servers/orange-agent/run_cpus.sh && cpus` /export/servers/orange-agent/super_guard.sh >> /export/servers/orange-agent/orange-agent/var/app.log 2>&1 &)
8月 02 15:18:01 A02-R05-I79-201-3V98WK2.ORANGE.LOCAL CROND[56925]: (orange) CMD (`which taskset` -c `source /export/servers/orange-agent/run_cpus.sh && cpus` /export/servers/orange-agent/super_guard.sh >> /export/servers/orange-agent/orange-agent/var/app.log 2>&1 &)

经过以上设置后,orange用户启动的所有进程占用的物理内存之和不能超过200M,如果超过,进程机会被kill。

如果进程因为OOM被kill,会在/var/log/message中发现记录。

如何查看某个进程受哪些资源限制条件

# cat /proc/PID/cgroup
11:memory:/user-1000.slice
10:perf_event:/
9:devices:/user.slice
8:hugetlb:/
7:blkio:/user.slice
6:cpuset:/
5:freezer:/
4:pids:/
3:cpuacct,cpu:/user.slice
2:net_prio,net_cls:/
1:name=systemd:/user.slice/user-1000.slice/session-8569.scope

参考

centos7 user.slice
https://stackoverflow.com/questions/47367886/cgroup-configuration-in-centos-7

centos 7 systemd资源限制举例
https://www.certdepot.net/rhel7-get-started-cgroups/

Table 10.1. Available systemd Unit Types
Table 10.2. Systemd Unit Files Locations
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/chap-managing_services_with_systemd

service文件参考
/usr/lib/systemd/system/ssh.service

免责声明:文章转载自《centos7 如何在用户级对资源进行限制》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇win7安装linux CentOS7双系统实践bash反弹shell检测下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

rpcbind.service启动失败

新装的服务器,启动rpcbind.service通常失败,执行下面的两个命令经常卡死,一直不返回,也不报错 #systemctl start nfs-server.service #systemctl start rpcbind.service 偶尔执行以下命令,却又可以 #systemctl restart rpcbind.service 关机偶尔会卡在...

Rhel6和rhel7的主要区别

内核版本 Rhel6.8的内核版本为2.6.32-642,rhel7使用的是3.10.x-x系列,最新的rhel7.3使用的是3.10.0-514。 文件系统的差别 Rhel7默认使用xfs文件系统,rhel6默认为ext4文件系统,但从rhel6.8起开始支持xfs系统。 设置默认启动级别 Rhel6:vim /etc/inittab 将末行的id:3:...

cetos7 systemd 详解

  CentOS7/RHEL7 systemd详解 目录1. 为什么是systemd(1) 关于Linux服务管理(2) SysV init的优缺点(3) UpStart的改进(4) systemd的诞生(5)为什么systemd能做到启动很快2. SysV init介绍(1) 什么是SystemV(2)SysV init的运行级别(3)SysV in...

lua工具库penlight--02表和数组

类Python的List lua的优美之处在于把数组和关联数组都用table实现了(Python中叫list和dict,C++中叫vector和map)。 一般我们把数字索引的table叫做list。penlight里的List模仿了Python,看起来从Python借鉴是个好主意。 下面是一个List的例子,List实现了__tostirng,因此可以输...

Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向

一、shell介绍 shell脚本是日常Linux系统管理工作中必不可少的,不会shell,就不是一个合格管理员。 shell是系统跟计算机硬件交互使用的中间介质,一个系统工具。实际上在shell和计算机硬件之间还有一层——系统内核。如果吧计算机比作人的躯体,那系统内核就是人的大脑,至于shell,把它比做人的五官更贴切。 其实,用户直接面对的不是计算机硬...

【K8S】基于Docker+K8S+GitLab/SVN+Jenkins+Harbor搭建持续集成交付环境(环境搭建篇)

写在前面 最近在 K8S 1.18.2 版本的集群上搭建DevOps环境,期间遇到了各种坑。目前,搭建环境的过程中出现的各种坑均已被填平,特此记录,并分享给大家! 小伙伴们可以到链接:https://download.csdn.net/download/l1028386804/12579236下载所需要的yaml文件。 服务器规划 IP 主机名 节点...