使用cgroup限制磁盘io读写速率 1. 安装和启动cgroup 2. 设置测试的磁盘盘符和读写速率限制 3. 创建读写控制组 4. 将io读写限制策略绑定在指定的磁盘驱动号上 5. 确认配置的限制策略 6. 读测试对比 7. 写测试对比 8. 将正在执行的进程添加到限制策略里
在MySQL innobackupex全备期间,磁盘io基本被打满,设置过 --throttle效果不明显,
--throttle=# This option specifies a number of I/O operations (pairs of read+write) per second. It accepts an integer argument. It is passed directly to xtrabackup's --throttle option.
以下是使用cgroup方式进行io读写的限制测试
【centos6】 yum install libcgroup service cgconfig start 【centos7】 yum install -y libcgroup-tools.x86_64 systemctl start cgconfig
说明:
centos6的cgroup挂载在/cgroup对应的路径下
centos7的cgroup挂载在/sys/fs/cgroup对应的路径下
可使用lssubsys -M 或者lssubsys -am命令查看
2. 设置测试的磁盘盘符和读写速率限制
io_read_limit=1048576 io_write_limit=1024 #比如我测试的读写文件都发生在/data文件系统里 filesystem_mounted='/data' lsblk -d -n | awk '{print $1}' >all_disks while read line do aaa=$(df -h | grep -w ${filesystem_mounted} | grep $line) if [[ ! -z aaa ]];then disk_name=$line fi done < all_disks #echo $disk_name
说明:以上主要是为了获取到裸盘的盘符,而非分区后的盘符,例如/data 文件系统对应的盘符是vdb1,这里获取的是vdb
3. 创建读写控制组
cgcreate -g blkio:test_read cgcreate -g blkio:test_write
4. 将io读写限制策略绑定在指定的磁盘驱动号上
disk_id=$(ls -l ${disk_name} | awk '{print $5,$6}' | sed 's/ //g' | tr ',' ':') cgset -r blkio.throttle.read_bps_device="${disk_id} ${io_read_limit}" test_read cgset -r blkio.throttle.write_bps_device="${disk_id} ${io_write_limit}" test_write
5. 确认配置的限制策略
cgget -r blkio.throttle.read_bps_device test_read cgget -r blkio.throttle.write_bps_device test_write
6. 读测试对比
dd if=/dev/vdb of=/dev/null cgexec -g blkio:test_read dd if=/dev/vdb of=/dev/null
7. 写测试对比
dd if=/dev/zero of=/data/testfile bs=512 count=100000 oflag=dsync cgexec -g blkio:test_write dd if=/dev/zero of=/data/testfile bs=1024 count=1000 oflag=dsync
8. 将正在执行的进程添加到限制策略里
#比如正在执行的dd命令对应的pid是5306 cgclassify -g blkio:test_write 5306