路漫漫其修远兮
吾将上下而求索

k8s学习:自定义cgroup脚本

#!/bin/bash

if [ $# -ne 3 ];then
        echo "usage: $0 [cpu个数] [mem大小] [shell命令,双引号添加] "
        exit
fi

cpu=`echo "${1}*100000" |bc`
if [ $? -ne 0 ];then
        exit 1  
fi
mem="${2}"
shell="${@: -1}"
group="$(date +%Y%m%d-%H%M%S)"

myCgroup(){
        if [ ! -e /sys/fs/cgroup/cpu/${group} ];then
                cgcreate -g cpu:${group}
                if [ $? -ne 0 ];then
                        exit 1
                fi
                cgset -r cpu.cfs_quota_us=$(echo ${cpu} |awk -F'.' '{print$1}') ${group}
                if [ $? -ne 0 ];then
                        cgdelete -g cpu:${group}
                        exit 1
                fi
        fi

        if [ ! -e /sys/fs/cgroup/cpu/memory/${group} ];then
                cgcreate -g memory:${group}
                if [ $? -ne 0 ];then
                        exit 1
                fi
                echo 1 > /sys/fs/cgroup/memory/${group}/memory.oom_control
                if [ $? -ne 0 ];then
                        exit 1
                fi
                cgset -r memory.limit_in_bytes=${mem}  ${group}
                if [ $? -ne 0 ];then
                        cgdelete -g memory:${group}
                        exit 1
                fi
        fi

        cgexec  -g "cpu:/${group}" -g "memory:/${group}" ${shell}
        cgdelete -g cpu:${group}
        cgdelete -g memory:${group}

}

myCgroup

未经允许不得转载:江哥架构师笔记 » k8s学习:自定义cgroup脚本

分享到:更多 ()

评论 抢沙发

评论前必须登录!