Cgroups最初的目标是为资源管理提供的一个统一的框架,既整合现有的cpuset等子系统,也为未来开发新的子系统提供接口。现在的cgroups适用于多种应用场景,从单个进程的资源控制,到实现操作系统层次的虚拟化(OS Level Virtualization)。Cgroups提供了以下功能:
1.限制进程组可以使用的资源数量(Resource limiting )。比如:memory子系统可以为进程组设定一个memory使用上限,一旦进程组使用的内存达到限额再申请内存,就会出发OOM(out of memory)。
2.进程组的优先级控制(Prioritization )。比如:可以使用cpu子系统为某个进程组分配特定cpu share。
3.记录进程组使用的资源数量(Accounting )。比如:可以使用cpuacct子系统记录某个进程组使用的cpu时间
4.进程组隔离(Isolation)。比如:使用ns子系统可以使不同的进程组使用不同的namespace,以达到隔离的目的,不同的进程组有各自的进程、网络、文件系统挂载空间。
5.进程组控制(Control)。比如:使用freezer子系统可以将进程组挂起和恢复。
blkio -- 这个子系统为块设备设定输入/输出限制,比如物理设备(磁盘,固态硬盘,USB 等等)。
cpu -- 这个子系统使用调度程序提供对 CPU 的 cgroup 任务访问。
cpuacct -- 这个子系统自动生成 cgroup 中任务所使用的 CPU 报告。
cpuset -- 这个子系统为 cgroup 中的任务分配独立 CPU(在多核系统)和内存节点。
devices -- 这个子系统可允许或者拒绝 cgroup 中的任务访问设备。
freezer -- 这个子系统挂起或者恢复 cgroup 中的任务。
memory -- 这个子系统设定 cgroup 中任务使用的内存限制,并自动生成由那些任务使用的内存资源报告。
net_cls -- 这个子系统使用等级识别符(classid)标记网络数据包,可允许 Linux 流量控制程序(tc)识别从具体 cgroup 中生成的数据包。
ns -- 名称空间子系统。
若系统未安装则进行安装,若已安装则进行更新。
[root@localhost ~]# yum install libcgroup查看运行状态,并启动服务
[root@localhost ~]# service cgconfig status Stopped [root@localhost ~]# service cgconfig start Starting cgconfig service: [ OK ] [root@localhost ~]# service cgconfig status Running
6.1.1 cgroup配置文件所在位置
/etc/cgconfig.conf6.1.2 默认配置文件内容
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; }相当于执行命令
mkdir /cgroup/cpuset mount -t cgroup -o cpuset red /cgroup/cpuset …… mkdir /cgroup/blkio mount -t cgroup -o cpuset red /cgroup/blkio6.1.3 cgroup section的语法格式如下
group <name> { [<permissions>] <controller> { <param name> = <param value>; … } …}name: 指定cgroup的名称
前提:mysql数据库已在机器上安装
7.1.1 修改cgconfig.conf文件
mount { cpuset = /cgroup/cpuset; cpu = /cgroup/cpu; cpuacct = /cgroup/cpuacct; memory = /cgroup/memory; blkio = /cgroup/blkio; } group mysql_g1 { cpu { cpu.cfs_quota_us = 50000; cpu.cfs_period_us = 100000; } cpuset { cpuset.cpus = "3"; cpuset.mems = "0"; } cpuacct{ } memory { memory.limit_in_bytes=104857600; memory.swappiness=0; # memory.max_usage_in_bytes=104857600; # memory.oom_control=0; } blkio { blkio.throttle.read_bps_device="8:0 524288"; blkio.throttle.write_bps_device="8:0 524288"; } }
7.1.2 配置文件的部分解释。
cpu:cpu使用时间限额。
cpu.cfs_period_us和cpu.cfs_quota_us来限制该组中的所有进程在单位时间里可以使用的cpu时间。这里的cfs是完全公平调度器的缩写。cpu.cfs_period_us就是时间周期(微秒),默认为100000,即百毫秒。cpu.cfs_quota_us就是在这期间内可使用的cpu时间(微秒),默认-1,即无限制。(cfs_quota_us是cfs_period_us的两倍即可限定在双核上完全使用)。
cpuset:cpu绑定
我们限制该组只能在0一共1个超线程上运行。cpuset.mems是用来设置内存节点的。
本例限制使用超线程0上的第四个cpu线程。
其实cgconfig也就是帮你把配置文件中的配置整理到/cgroup/cpuset这个目录里面,比如你需要动态设置mysql_group1/ cpuset.cpus的CPU超线程号,可以采用如下的办法。
[root@localhost ~]# echo "0" > mysql_group1/ cpuset.cpus
cpuacct:cpu资源报告
memory:内存限制
内存限制我们主要限制了MySQL可以使用的内存最大大小memory.limit_in_bytes=256M。而设置swappiness为0是为了让操作系统不会将MySQL的内存匿名页交换出去。
blkio:BLOCK IO限额
blkio.throttle.read_bps_device="8:0 524288"; #每秒读数据上限
blkio.throttle.write_bps_device="8:0 524288"; #每秒写数据上限
其中8:0对应主设备号和副设备号,可以通过ls -l /dev/sda查看
[root@localhost /]# ls -l /dev/sda brw-rw----. 1 root disk 8, 0 Sep 15 04:19 /dev/sda7.1.3 拓展知识
现在较新的服务器CPU都是numa结构<非一致内存访问结构(NUMA:Non-Uniform Memory Access)>,使用numactl --hardware可以看到numa各个节点的CPU超线程号,以及对应的节点号。
本例结果如下:
[root@localhost /]# numactl --hardware available: 1 nodes (0) node 0 cpus: 0 1 2 3 node 0 size: 1023 MB node 0 free: 68 MB node distances: node 0 0: 10以下是较高端服务器的numa信息,仅作参考。
[root@localhost ~]# numactl --hardware available: 4 nodes (0-3) node 0 cpus: 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 node 0 size: 16338 MB node 0 free: 391 MB node 1 cpus: 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 node 1 size: 16384 MB node 1 free: 133 MB node 2 cpus: 2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 node 2 size: 16384 MB node 2 free: 137 MB node 3 cpus: 3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 node 3 size: 16384 MB node 3 free: 186 MB node distances: node 0 1 2 3 0: 10 20 30 20 1: 20 10 20 30 2: 30 20 10 20 3: 20 30 20 10
7.1.4 修改cgrules.conf文件
[root@localhost ~]# vi /etc/cgrules.conf # /etc/cgrules.conf #The format of this file is described in cgrules.conf(5) #manual page. # # Example: #<user> <controllers> <destination> #@student cpu,memory usergroup/student/ #peter cpu test1/ #% memory test2/ *:/usr/local/mysql/bin/mysqld * mysql_g1注:共分为3个部分,分别为需要限制的实例,限制的内容(如cpu,memory),挂载目标。
[root@localhost ~]# /etc/init.d/cgconfig restart Stopping cgconfig service: [ OK ] Starting cgconfig service: [ OK ] [root@localhost ~]# /etc/init.d/cgred restart Stopping CGroup Rules Engine Daemon... [ OK ] Starting CGroup Rules Engine Daemon: [ OK ]注:重启顺序为cgconfig -> cgred ,更改配置文件后两个服务需要重启,且顺序不能错。
[root@localhost ~]# ps -eo pid,cgroup,cmd | grep -i mysqld 29871 blkio:/;net_cls:/;freezer:/;devices:/;memory:/;cpuacct:/;cpu:/;cpuset:/ /bin/sh ./bin/mysqld_safe --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 30219 blkio:/;net_cls:/;freezer:/;devices:/;memory:/;cpuacct:/;cpu:/;cpuset:/mysql_g1 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/usr/local/mysql/data//localhost.localdomain.err --pid-file=/usr/local/mysql/data//localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306 30311 blkio:/;net_cls:/;freezer:/;devices:/;memory:/;cpuacct:/;cpu:/;cpuset:/ grep -i mysqld
使用mysqlslap对mysql进行压力测试,看mysql使用资源是否超过限制。
7.4.1 在shell窗口1用mysqlslap对mysql进行压力测试。
[root@localhost /]# /usr/local/mysql/bin/mysqlslap --defaults-file=/etc/my.cnf --concurrency=150 --iterations=1 --number-int-cols=8 --auto-generate-sql --auto-generate-sql-load-type=mixed --engine=innodb --number-of-queries=100000 -ujesse -pjesse --number-char-cols=35 --auto-generate-sql-add-autoincrement --debug-info -P3306 -h127.0.0.17.4.2 在shell窗口2查看mysql对cpu,内存的使用
可见:cpu限制在了第四个核心上,且对第四个核心的使用限制在50%。
7.4.3 在shell窗口3查看io的消耗
可见:mysql对io的读及写消耗均限制在2M每秒以内。
还原配置文件/etc/cgconfig.conf及/etc/cgrules.conf 为默认配置。测试实例依然为mysql,测试工具为mysqlslap。
开启cgconfig及cgrules 服务。
[root@localhost /]# /etc/init.d/cgconfig restart Stopping cgconfig service: [ OK ] Starting cgconfig service: [ OK ] [root@localhost /]# /etc/init.d/cgred restart Stopping CGroup Rules Engine Daemon... [ OK ] Starting CGroup Rules Engine Daemon: [ OK ]
开启mysqlslap压力测试程序。
[root@localhost /]# /usr/local/mysql/bin/mysqlslap --defaults-file=/etc/my.cnf --concurrency=150 --iterations=1 --number-int-cols=8 --auto-generate-sql --auto-generate-sql-load-type=mixed --engine=innodb --number-of-queries=100000 -ujesse -pjesse --number-char-cols=35 --auto-generate-sql-add-autoincrement --debug-info -P3306 -h127.0.0.1通过htop查看资源消耗。
限制mysql使用一个核,如第2个核,且对该核的使用不超过50%
[root@localhost /]# mkdir -p /cgroup/cpu/foo/ [root@localhost /]# mkdir -p /cgroup/cpuset/foo/ [root@localhost /]# echo 50000 > /cgroup/cpu/foo/cpu.cfs_quota_us [root@localhost /]# echo 100000 > /cgroup/cpu/foo/cpu.cfs_period_us [root@localhost /]# echo "0" > /cgroup/cpuset/foo/cpuset.mems [root@localhost /]# echo "1" > /cgroup/cpuset/foo/cpuset.cpus [root@localhost /]# echo 28819 > /cgroup/cpu/foo/tasks
其中:28819为mysqld的进程号。
限制mysql使用内存为不超过512M
跑一个消耗内存脚本
x='a' while [ True ];do x=$x$x done;
内存的消耗在不断增加,对其进行限制,使其使用内存在500M以内
[root@localhost /]# mkdir -p /cgroup/memory/foo [root@localhost /]# echo 524288000 > /cgroup/memory/foo/memory.limit_in_bytes [root@localhost /]# echo 44476 > /cgroup/memory/foo/tasks
跑一个消耗IO的测试
[root@localhost ~]# dd if=/dev/sda of=/dev/null通过iotop看io占用情况,磁盘读取速度到了50M/s
限制读取速度为10M/S
[root@localhost ~]# mkdir -p /cgroup/blkio/foo [root@localhost ~]# echo '8:0 10485760' > /cgroup/blkio/foo/blkio.throttle.read_bps_device [root@localhost ~]# echo 45033 > /cgroup/blkio/foo/tasks注1:45033为dd的进程号
注2:8:0对应主设备号和副设备号,可以通过ls -l /dev/sda查看
[root@localhost ~]# ls -l /dev/sda brw-rw----. 1 root disk 8, 0 Sep 15 04:19 /dev/sda
使用cgroup临时对进程进行调整,直接通过命令即可,如果要持久化对进程进行控制,即重启后依然有效,需要写进配置文件/etc/cgconfig.conf及/etc/cgrules.conf
****************************************************************************************
原文地址:http://blog.csdn.net/jesseyoung/article/details/39077829
博客主页:http://blog.csdn.net/jesseyoung
****************************************************************************************