Docker IO资源控制

1 背景知识

Block IO 指的是磁盘的读写,docker 可通过设置权重、限制 bps 和 iops 的方式控制容器读写磁盘的带宽。

编者注:目前 Block IO 限额只对 direct IO(不使用文件缓存)有效。

1.1 block IO的选项参数

1、查询block IO的选项参数。

docker run --help|grep -E 'bps|IO'
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)

      --blkio-weight-device list       Block IO weight (relative device weight) (default [])

      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])

      --device-read-iops list          Limit read rate (IO per second) from a device (default [])

      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])

      --device-write-iops list         Limit write rate (IO per second) to a device (default [])

2、参数简介

参数 简介
--blkio-weight 容器默认磁盘IO的加权值,有效值范围为10-1000。
--blkio-weight-device 针对特定设备的IO加权控制。其格式为DEVICE_NAME:WEIGHT。
--device-read-bps 限制此设备上的读速度(bytes per second),单位可以是KB、MB或者GB。
--device-read-iops 通过每秒读IO次数来限制指定设备的读速度。
--device-write-bps 限制此设备上的写速度(bytes per second),单位可以是KB、MB或者GB。
--device-write-iops 通过每秒写IO次数来限制指定设备的写速度。

1.2 block IO 权重

1、默认情况下,所有容器平等地读写磁盘。
2、可以通过设置 --blkio-weight 参数来改变容器 block IO 的优先级
编者注:--blkio-weight 与 --cpu-shares 类似,设置的是相对权重值,默认为 500。

参数 简介
--blkio-weight 容器默认磁盘IO的加权值,有效值范围为10-1000。
--blkio-weight-device 针对特定设备的IO加权控制。其格式为DEVICE_NAME:WEIGHT。

1.3 限制 bpsiops

1、默认情况下,所有容器平等地读写磁盘。
2、可通过以下参数控制容器的 bps 和 iops:

参数 简介
--device-read-bps 限制此设备上的读速度(bytes per second),单位可以是KB、MB或者GB。
--device-read-iops 通过每秒读IO次数来限制指定设备的读速度。
--device-write-bps 限制此设备上的写速度(bytes per second),单位可以是KB、MB或者GB。
--device-write-iops 通过每秒写IO次数来限制指定设备的写速度。

2 限制容器的 BPS

2.1 创建并登录容器

docker run -it --rm -h c002 --name=c02 --device-write-bps /dev/sda:30MB centos:7.2.1511

docker run -it --rm -h c002 --name=c02 --device-write-bps /:30MB centos:7.2.1511

2.2 在容器内部测试写入速率

time dd if=/dev/zero of=test bs=1M count=200 oflag=direct
200+0 records in

200+0 records out

209715200 bytes (210 MB) copied, 6.64163 s, 31.6 MB/s

real    0m6.643s

user    0m0.002s

sys     0m0.289s

从上面可知,由于使用了 --device-write-bps 参数,磁盘的 BPS 被限制为 30MB/s