Linux Admin教程

Linux Admin crgoups

cgroups 或 Control Groups 是 Linux 内核的一项功能,允许管理员为服务和组分配或限制系统资源。
列出活动e 控制组运行,我们可以使用下面的 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 实现重新定义。在考虑服务的资源管理时,主要关注的是 cgroupscgroups 在功能和简单性方面都比 systemd 先进。
cgroups 在资源管理方面的目标是——没有一个服务可以让整个系统瘫痪。或者,没有任何一个服务进程(可能是写得不好的 PHP 脚本)会消耗过多资源而削弱服务器功能。
cgroups 允许对以下资源的单元进行资源控制-
CPU-限制与其他不那么密集的任务不重要的 CPU 密集型任务 Memory-限制服务可以消耗多少内存 Disks-限制磁盘 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 时间。考虑到这一点,我们可以看到如何使用较短的时间片来让基本任务更好地访问系统资源。
要为每个资源设置服务, set-property 方法定义了以下参数-
systemctl set-property name parameter=value
CPU Slices CPUShares
Memory Limit MemoryLimit
Soft Memory Limit MemorySoftLimit
Block IO Weight BlockIOWeight
Block Device Limit (specified in /volume/path) ) BlockIODeviceWeight
Read IO BlockIOReadBandwidth
Disk Write IO BlockIOReadBandwidth
大多数情况下,服务会受到 CPU使用内存限制读/写IO的限制。
更改后,需要重新加载systemd并重启服务-
systemctl set-property foo.service CPUShares = 250 
systemctl daemon-reload 
systemctl restart foo.service

在 CentOS Linux 中配置 CGroups

要在 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]#
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4