VPS环境Centos/Debian等系统通过使用cgroup限制磁盘io读写速率-主机进行大流量读写期间,很可能会超过服务提供商限制的额度,为了不被ban,可以先在自己的系统上安装设置,限制磁盘的读写。

谷子猫博客

一个简单爱分享的小站!^_^!
首页>> 网文转载 >>VPS环境Centos/Debian等系统通过使用cgroup限制磁盘io读写速率
主机进行大流量读写期间,很可能会超过服务提供商限制的额度,为了不被ban,可以先在自己的系统上安装设置,限制磁盘的读写。

20221220170426.jpg

在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读写的限制测试

1. 安装和启动cgroup
【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

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:谷子博客 » VPS环境Centos/Debian等系统通过使用cgroup限制磁盘io读写速率
标签: 网络 debian 运营商

发表评论

路人甲

网友评论(0)