第 4 章 容器

限制容器的 Block IO

  • Block IO 是另一种可以限制容器使用的资源。
  • Block IO 指的是磁盘的读写,docker 可通过设置权重、限制 bps 和 iops 的方式控制容器读写磁盘的带宽。
  • 注:目前 Block IO 限额只对 direct IO(不使用文件缓存)有效。

block IO 权重

默认情况下,所有容器能平等地读写磁盘,可以通过设置 --blkio-weight 参数来改变容器 block IO 的优先级。

--blkio-weight 与 --cpu-shares 类似,设置的是相对权重值,默认为 500。

举例:container_A 读写磁盘的带宽是 container_B 的两倍。

1 docker run -it --name container_A --blkio-weight 600 ubuntu   
2 docker run -it --name container_B --blkio-weight 300 ubuntu

限制 bps 和 iops

  • bps 是 byte per second,每秒读写的数据量。
  • iops 是 io per second,每秒 IO 的次数。

可通过以下参数控制容器的 bps 和 iops:

  • --device-read-bps,限制读某个设备的 bps。
  • --device-write-bps,限制写某个设备的 bps。
  • --device-read-iops,限制读某个设备的 iops。
  • --device-write-iops,限制写某个设备的 iops。

举个例子:

限制容器写 /dev/sda 的速率为 30 MB/s

docker run -it --device-write-bps /dev/sda:30MB ubuntu

我们来看看实验结果:

 1 root@ubuntu:~# docker run -it --device-write-bps /dev/sda:30MB ubuntu
 2 root@e323015f73ab:/# 
 3 root@e323015f73ab:/# time dd if=/dev/zero of=test bs=1M count=800 oflag=direct
 4 800+0 records in
 5 800+0 records out
 6 838860800 bytes (839 MB, 800 MiB) copied, 39.4232 s, 21.3 MB/s
 7 
 8 real    0m39.426s
 9 user    0m0.000s
10 sys    0m0.224s
11 root@e323015f73ab:/# 

通过 dd 测试在容器中写磁盘的速度。因为容器的文件系统是在 host /dev/sda 上的,在容器中写文件相当于对 host /dev/sda 进行写操作。另外,oflag=direct 指定用 direct IO 方式写文件,这样 --device-write-bps 才能生效。

结果表明,bps 21.3 MB/s 没有超过 30 MB/s 的限速。

作为对比测试,如果不限速,结果如下:

 1 root@ubuntu:~# docker run -it ubuntu
 2 root@4e971c7d99b9:/# 
 3 root@4e971c7d99b9:/# time dd if=/dev/zero of=test bs=1M count=800 oflag=direct
 4 800+0 records in
 5 800+0 records out
 6 838860800 bytes (839 MB, 800 MiB) copied, 16.1607 s, 51.9 MB/s
 7 
 8 real    0m16.163s
 9 user    0m0.008s
10 sys    0m0.200s
11 root@4e971c7d99b9:/# 

---------------------引用来自-----------------------

https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587668&idx=1&sn=13eddc2889e336393adc365f0f184f0a&chksm=8d3080cdba4709db1603d5c6c3f41801641b23f09d80f3f339e95c66b18e2078b056fa7235e2&scene=21#wechat_redirect

原文地址:https://www.cnblogs.com/gsophy/p/10335675.html