Linux管理员 - 使用crgoups的资源管理

cgroups 或控制组是Linux内核的一项功能,允许管理员为服务和组分配或限制系统资源.

To列出活动控制组正在运行,我们可以使用以下 ps 命令 :

[root@localhost]# ps xawf -eo pid,user,cgroup,args 
8362 root     -                            \_ [kworker/1:2] 
1 root        -                           /usr/lib/systemd/systemd --switched-
   root --system --    deserialize 21 
507 root     7:cpuacct,cpu:/system.slice  /usr/lib/systemd/systemd-journald 
527 root     7:cpuacct,cpu:/system.slice  /usr/sbin/lvmetad -f 
540 root     7:cpuacct,cpu:/system.slice  /usr/lib/systemd/systemd-udevd 
715 root     7:cpuacct,cpu:/system.slice  /sbin/auditd -n 
731 root     7:cpuacct,cpu:/system.slice   \_ /sbin/audispd 
734 root     7:cpuacct,cpu:/system.slice       \_ /usr/sbin/sedispatch 
737 polkitd  7:cpuacct,cpu:/system.slice  /usr/lib/polkit-1/polkitd --no-debug 
738 rtkit    6:memory:/system.slice/rtki  /usr/libexec/rtkit-daemon 
740 dbus     7:cpuacct,cpu:/system.slice  /bin/dbus-daemon --system --
   address=systemd: --nofork --nopidfile --systemd-activation

资源管理,自CentOS 6.X起,已使用 systemd init 实现重新定义.在考虑服务的资源管理时,要关注的主要是 cgroups . cgroups 在功能和简单性方面都先进了 systemd .

cgroups在资源管理方面的目标是 - 没有一项服务可以采取整个系统,整个系统.或者,没有任何一个服务流程(可能编写得很糟糕的PHP脚本)会因消耗太多资源而削弱服务器功能.

cgroups 允许资源控制单位以下资源 :

  • CPU : 限制cpu密集型任务,这些任务对于其他不太密集的任务并不重要

  • 内存 : 限制服务可以消耗多少内存

  • 磁盘 : 限制磁盘I/O

** CPU时间:**

需要较少CPU的任务优先级可以有自定义配置的CPU切片.

让我们看一下以下两个服务.

礼貌的CPU服务1

[root@localhost]# systemctl cat polite.service 
# /etc/systemd/system/polite.service 
[Unit] 
Description = Polite service limits CPU Slice and Memory 
After=remote-fs.target nss-lookup.target

[Service] 
MemoryLimit = 1M 
ExecStart = /usr/bin/sha1sum /dev/zero 
ExecStop = /bin/kill -WINCH ${MAINPID} 
WantedBy=multi-user.target

# /etc/systemd/system/polite.service.d/50-CPUShares.conf 
[Service] 
CPUShares = 1024 
[root@localhost]#

邪恶的CPU服务2

[root@localhost]# systemctl cat evil.service 
# /etc/systemd/system/evil.service 
[Unit] 
Description = I Eat You CPU 
After=remote-fs.target nss-lookup.target

[Service] 
ExecStart = /usr/bin/md5sum /dev/zero 
ExecStop = /bin/kill -WINCH ${MAINPID} 
WantedBy=multi-user.target

# /etc/systemd/system/evil.service.d/50-CPUShares.conf 
[Service] 
CPUShares = 1024 
[root@localhost]#

让我们设置礼貌服务使用较低的CPU优先级 :

systemctl set-property polite.service CPUShares = 20  
/system.slice/polite.service
1   70.5   124.0K        -        -  

/system.slice/evil.service
1   99.5   304.0K        -        -

正如我们所看到的,在正常系统空闲时间段内,两个恶意进程仍在使用CPU周期.但是,设置为具有较少时间片的那个使用较少的CPU时间.考虑到这一点,我们可以看到如何使用较短的时间片段来允许基本任务更好地访问系统资源.

要为每个资源设置服务,设置 - property 方法定义以下参数 :

 
 systemctl set-property name parameter = value


CPU SlicesCPUShares
内存限制MemoryLimit
软内存限制MemorySoftLimit
阻止IO重量BlockIOWeight
阻止设备限制(在/volume/path中指定))BlockIODeviceWeight
读取IOBlockIOReadBandwidth
磁盘写入IOBlockIOReadBandwidth

大多数情况下,服务将受到 CPU使用内存限制读/写的限制IO .

更改每个后,需要重新加载systemd并重启服务 :

 
 systemctl set-property foo.service CPUShares = 250 
 systemctl daemon-reload 
 systemctl restart foo.ser

在CentOS Linux中配置CGroup

要在CentOS Linux中制作自定义cgroup,我们需要先安装服务和配置它们.

第1步 : 安装libcgroup(如果尚未安装).

[root@localhost]# yum install libcgroup 
Package libcgroup-0.41-11.el7.x86_64 already installed and latest version 
Nothing to do 
[root@localhost]#

我们可以看到,默认情况下CentOS 7的libcgroup安装了 everything 安装程序.使用最小安装程序将要求我们安装 libcgroup 实用程序以及任何依赖项.

步骤2 : 启动并启用cgconfig服务.

[root@localhost]# systemctl enable cgconfig 
Created symlink from /etc/systemd/system/sysinit.target.wants/cgconfig.service 
to /usr/lib/systemd/system/cgconfig.service. 
[root@localhost]# systemctl start cgconfig 
[root@localhost]# systemctl status cgconfig 
● cgconfig.service - Control Group configuration service 
Loaded: loaded (/usr/lib/systemd/system/cgconfig.service; enabled; vendor 
preset: disabled) 
Active: active (exited) since Mon 2017-01-23 02:51:42 EST; 1min 21s ago 
Main PID: 4692 (code=exited, status = 0/SUCCESS) 
Memory: 0B 
CGroup: /system.slice/cgconfig.service  

Jan 23 02:51:42 localhost.localdomain systemd[1]: Starting Control Group 
configuration service... 
Jan 23 02:51:42 localhost.localdomain systemd[1]: Started Control Group 
configuration service. 
[root@localhost]#