安装服务
centos 6
yum install -y libcgroup
centos 7
yum install -y libcgroup libcgroup-tools
[root@vm10-65-12-88 ~]# cat /etc/cgconfig.conf
#
# Copyright IBM Corporation. 2007
#
# Authors: Balbir Singh <balbir@linux.vnet.ibm.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See man cgconfig.conf for further details.
#
# By default, mount all controllers to /cgroup/<controller>
mount {
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
}
启动服务
/etc/init.d/cgconfig start
chkconfig --level 3 cgconfig on
查看cgroup组
ls /cgroup
创建自定义cgroup组
cgcreate -g cpu:/filebeat
cgdelete -g cpu:/filebeat
设置比值
cgset -r cpu.cfs_period_us=100000 filebeat
cgset -r cpu.cfs_quota_us=50000 filebeat
cpu.cfs_quota_us 和 cpu.cfs_period_us 是控制 cpu 的两个属性,可以通过设置它们的比值来设置某个组群的 cpu 使用率。在此,我们将 cpu 的使用率限制到 50%。
cpu.cfs_period_us: 单位是微秒,最大值是1s,最小值是1毫秒(ms),取值范围为1000-1000000
cpu.cfs_quota_us 单位是微秒,意思是在 cpu.cfs_period_us的时间内,用户可以占用的时间。对于单核来说,最大等于 cpu.cfs_period_us的值,对于多核来说,可以理解为最多可使用的cpu核数
----- 1.限制只能使用1个CPU,每100ms能使用100ms的CPU时间
# echo 100000 > cpu.cfs_quota_us
# echo 100000 > cpu.cfs_period_us
------ 2.限制使用2个CPU核,每100ms能使用200ms的CPU时间,即使用两个内核
# echo 200000 > cpu.cfs_quota_us
# echo 100000 > cpu.cfs_period_us
------ 3.限制使用1个CPU的50%,每100ms能使用50ms的CPU时间,即使用一个CPU核心的50%
# echo 50000 > cpu.cfs_quota_us
# echo 100000 > cpu.cfs_period_us
测试脚本
./example.sh 文件如下
x=0
while [ True ];do
x=$x+1
done;
绑定进程pid
echo $pid > /cgroup/cpu/filebeat/tasks
查看进程cgroup
ps -eo pid,cgroup,cmd
限制前:
限制后:
参考:
https://blog.csdn.net/weixin_33834137/article/details/91211819
https://www.jianshu.com/p/31cefb4a8e8d
https://www.sohu.com/a/252263523_609423
http://blog.sina.com.cn/s/blog_da4487c40102v3wz.html
https://segmentfault.com/q/1010000002451481
https://www.centos.bz/2018/08/%E5%AE%B9%E5%99%A8%E4%B9%8B-cgroup/