cgroup限制内存、cup的使用
使用cgroup可以实现用户组级别的限制,把CPU占用大头扔进限制组就能解决问题
服务器环境为centos7,使用yum安装cgroup
| yum install libcgroup-tools libcgroup-pam libcgroup
|
设置cgroup服务开机启动
| systemctl enable cgconfig
systemctl enable cgred
|
/etc/cgconfig.conf
| group cpu_limit1{
cpu{
# 限制CPU使用最大限度为60个核心,核心数自行修改
cpu.cfs_period_us = 100000;
cpu.cfs_quota_us = 6000000;
# 相对比例限制cgroup的cpu
cpu.shares = 8000;
}
} # 限制器1
|
/etc/cgrules.conf
第一列为用户(组)名,第二列为限制类型,第三列为限制器名
| @user1 cpu cpu_limit1 # 只限制 user1
* cpu cpu_limit1 # 全部限制
|
重启服务器
| systemctl restart cgconfig
systemctl restart cgred
|
其他说明
| cpu.cfs_period_us
cfs_period_us表示一个cpu带宽,单位为微秒。系统总CPU带宽: cpu核心数 * cfs_period_us
cpu.cfs_quota_us
cfs_quota_us表示Cgroup可以使用的cpu的带宽,单位为微秒。cfs_quota_us为-1,表示使用的CPU不受cgroup限制。cfs_quota_us的最小值为1ms(1000),最大值为1s。
结合cfs_period_us,就可以限制进程使用的cpu。例如配置cfs_period_us=10000,而cfs_quota_us=2000。那么该进程就可以可以用2个cpu core。
cpu.shares
通过cfs_period_us和cfs_quota_us可以以绝对比例限制cgroup的cpu使用,即cfs_quota_us/cfs_period_us 等于进程可以利用的cpu cores,不能超过这个数值。
而cpu.shares以相对比例限制cgroup的cpu。例如:在两个 cgroup 中都将 cpu.shares 设定为 1 的任务将有相同的 CPU 时间,但在 cgroup 中将 cpu.shares 设定为 2 的任务可使用的 CPU 时间是在 cgroup 中将 cpu.shares 设定为 1 的任务可使用的 CPU 时间的两倍。
cpu.rt_runtime_us
以微秒(µs,这里以“us”代表)为单位指定在某个时间段中 cgroup 中的任务对 CPU 资源的最长连续访问时间。建立这个限制是为了防止一个 cgroup 中的任务独占 CPU 时间。如果 cgroup 中的任务应该可以每 5 秒中可有 4 秒时间访问 CPU 资源,请将 cpu.rt_runtime_us 设定为 4000000,并将 cpu.rt_period_us 设定为 5000000。
cpu.rt_period_us
以微秒(µs,这里以“us”代表)为单位指定在某个时间段中 cgroup 对 CPU 资源访问重新分配的频率。如果某个 cgroup 中的任务应该每 5 秒钟有 4 秒时间可访问 CPU 资源,则请将 cpu.rt_runtime_us 设定为 4000000,并将 cpu.rt_period_us 设定为 5000000。
|