Linux 管理员 - 使用 crgoups 进行资源管理
cgroups 或控制组是 Linux 内核的一项功能,允许管理员为服务和组分配或限制系统资源。
要列出正在运行的活动 控制组,我们可以使用以下 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 密集型任务,这些任务并不像其他密集程度较低的任务那样重要
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 paramute=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 中创建自定义 cgroups,我们需要先安装服务并对其进行配置。
步骤 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 已使用 everything 安装程序安装了 libcgroup。使用最小安装程序将要求我们安装 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]#