• cpu,disk,mem 针对用户限制使用率


    Cgroup限制用户的CPU、内存和磁盘IO使用率

    jues 提交于 2019/07/04 - 10:25 周四
    内容

    昨天使用Linux系统,不小心操作导致系统内存用完,直接卡死,等了许久也没恢复过来,只能强制重启。

    一个普通用户也能直接导致死机,实在太尴尬了,而且当时硬盘一直在高速读写,强制重启太伤硬盘了。

    找了网上不少方法:

    1. ulimit

    2. nice/renice

    3. Cgroup

    后来还是觉得Cgroup最合适我,下面是一个基于CentOS7的测试demo。

    1. 安装Cgroup

    yum install libcgroup-tools libcgroup-pam libcgroup

    2. /etc/cgconfig.conf

    group users_normal_limit{
        cpu{
            # 限制CPU使用率为10%
            cpu.cfs_period_us=100000;
            cpu.cfs_quota_us=10000;
        }
    
        memory{
            # 最高总使用内存为100M
            memory.limit_in_bytes = 100M;
            memory.soft_limit_in_bytes = 80M;
            memory.swappiness = 0;
        }
        blkio{
            # /dev/sda 最高磁盘读写IO为1M/秒 (8:0可通过ls -l /dev/sda命令查看得到)
            blkio.throttle.read_bps_device="8:0 1024000";
            blkio.throttle.write_bps_device="8:0 1024000";
        }
    }

    检查配置是否正确

    cgconfigparser -l /etc/cgconfig.conf

    3.  /etc/cgrules.conf

    # 用户组users    
    @users          cpu,memory,blkio              users_normal_limit

    4. 加入用户组

    # 把jues加入受Cgroup限制的users组
    usermod -a -G users jues

    5. 使用配置生效

    # 设置Cgroup服务开机启动
    systemctl enable cgconfig
    systemctl enable cgred
    
    # 重启Cgroup服务
    systemctl restart cgconfig
    systemctl restart cgred

    测试:

    cpu测试:

    [jues@vir-centos7 cgroup]$ matho-primes 0 999999999 > /dev/null 
      PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                               
    14666 jues      20   0    9256   2536    488 R   9.6  0.2   0:04.26 matho-primes                                          
        1 root      20   0  193700   4244   1692 S   0.0  0.4   0:06.18 systemd                                               
        2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd                                              
        3 root      20   0       0      0      0 S   0.0  0.0   0:00.49 ksoftirqd/0                                           
        5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                          
        7 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/0                                           
        8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                                
        9 root      20   0       0      0      0 S   0.0  0.0   0:00.68 rcu_sched                                             
       10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain                                         
       11 root      rt   0       0      0      0 S   0.0  0.0   0:00.08 watchdog/0              

    内存测试:

    [jues@vir-centos7 cgroup]$ dd if=/dev/zero of=1.dat bs=80M count=1
    10+0 records in
    10+0 records out
    838860800 bytes (839 MB) copied, 22.8394 s, 36.7 MB/s
    [jues@vir-centos7 cgroup]$ dd if=/dev/zero of=1.dat bs=100M count=1
    Killed
    [jues@vir-centos7 cgroup]$ 

    磁盘IO测试:

    # oflag=direct 为跳过内存缓存
    [jues@vir-centos7 cgroup]$ dd if=1.dat of=2.dat bs=10M count=1 oflag=direct
    1+0 records in
    1+0 records out
    10485760 bytes (10 MB) copied, 11.641 s, 901 kB/s
    注意事项

    1. CentOS7中默认没有安装mathomatic工具,需要自己下载编译:

    # 下载mathomatic源码包
    wget https://launchpad.net/mathomatic/15/15.6.5/+download/mathomatic-15.6.5.tar.bz2
    
    # 安装解压tar.bz2工具
    yum install epel-release
    yum install bzip2
    
    # 解压mathomatic
    tar xvf mathomatic-15.6.5.tar.bz2
    cd mathomatic-15.6.5/primes
    
    # 安装编译工具
    yum install gcc
    
    # 编译
    make 
    
    # 安装
    make install

    2. Fedora现在没有cgred

    systemctl start cgred
    Failed to start cgred.service: Unit cgred.service not found.
  • 相关阅读:
    开源爬虫综述
    html页面工具-htmlUnit
    Html JavaScript网页制作与开发完全学习手册
    移动互联网之路-李晓斌
    算法图解
    游戏编程算法与技巧
    一秒解决CentOS下service 功能 不能使用 bash: service: command not found
    LINUX企业应用案例精解 第2版 李晨光
    深入分析GCC
    嵌入式LINUX基础教程 第2版
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12755188.html
Copyright © 2020-2023  润新知