Docker IO资源限制

    • Runtime constraints on resources
    • 关于 IO 的限制
      • --blkio-weight=0Block IO weight (relative weight) accepts a weight value between 10 and 1000.
      • --blkio-weight-device=""Block IO weight (relative device weight, format: DEVICE_NAME:WEIGHT)针对特定设备的权重比
      • --device-read-bps=""Limit read rate from a device (format: <device-path>:<number>[<unit>]). Number is a positive integer. Unit can be one of kb, mb, or gb.按每秒读取块设备的数据量设定上限
      • --device-write-bps=""Limit write rate from a device (format: <device-path>:<number>[<unit>]). Number is a positive integer. Unit can be one of kb, mb, or gb.按每秒写入块设备的数据量设定上限
      • --device-read-iops=""Limit read rate (IO per second) from a device (format: <device-path>:<number>). Number is a positive integer.按照每秒读操作次数设定上限
      • --device-write-iops=""Limit write rate (IO per second) from a device (format: ). Number is a positive integer.按照每秒写操作次数设定上限

    默认,所有的容器对于 IO 操作「block IO bandwidth – blkio」都拥有相同优先级。可以通过 --blkio-weight 修改容器 blkio 权重。--blkio-weight 权重值在 10 ~ 1000 之间。

    使用 blkio weight 还需要注意 IO 的调度必须为 CFQ:

    1. ~ cat /sys/block/sda/queue/scheduler
    2. noop [deadline] cfq
    3. ~ sudo sh -c "echo cfq > /sys/block/sda/queue/scheduler"
    4. ~ cat /sys/block/sda/queue/scheduler
    5. noop deadline [cfq]

    按照 Docker 官方文档的介绍测试:

    1. ~ docker run -it --rm --blkio-weight 100 ubuntu-stress:latest /bin/bash
    2. root@0b6770ee80e0:/#
    3. ~ docker run -it --rm --blkio-weight 1000 ubuntu-stress:latest /bin/bash
    4. root@6778b6b39686:/#

    在运行的容器上同时执行如下命令,统计测试时间:

    官方的测试说明是:

    You’ll find that the proportion of time is the same as the proportion of blkio weights of the two containers.

    • --blkio-weight-device="DEVICE_NAME:WEIGHT"

    --blkio-weight-device 可以指定某个设备的权重大小,如果同时指定 --blkio-weight 则以 --blkio-weight 为全局默认配置,针对指定设备以 --blkio-weight-device 指定设备值为主。

    1. ~ docker run -it --rm --blkio-weight-device "/dev/sda:100" ubuntu-stress:latest /bin/bash

    限制容器的写入速度是 1mb「,单位可以是 kb、mb、gb 正整数」:

    1. ~ docker run -it --rm --device-write-bps /dev/sda:1mb ubuntu-stress:latest /bin/bash
    2. root@ffa51b81987c:/# dd if=/dev/zero of=test.out bs=1M count=100 oflag=direct
    3. 100+0 records in
    4. 100+0 records out
    5. 104857600 bytes (105 MB) copied, 100.064 s, 1.0 MB/s # 可以得知写入的平均速度是 1.0 MB/s

    通过 iotop 获取测试过程中的 bps 也是 1.0 MB 为上限:

    读 bps 限制使用方式同写 bps 限制:

    限制容器 write iops 为 5「<device-path>:<limit>,必须为正整数」:

    1. root@c2a2fa232594:/# dd if=/dev/zero of=test.out bs=1M count=100 oflag=direct
    2. 100+0 records in
    3. 100+0 records out
    4. 104857600 bytes (105 MB) copied, 42.6987 s, 2.5 MB/s

    通过 iostat 监控 tps「此处即为 iops」 基本上持续在 10 左右「会有些偏差」:

    1. ~ iostat 1
    2. ... ...
    3. avg-cpu: %user %nice %system %iowait %steal %idle
    4. 1.13 0.00 0.13 23.46 0.00 75.28
    5. Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    6. sda 10.00 0.00 2610.00 0 5220
    7. ... ...

    读 iops 限制使用方式同写 iops 限制:

    注: 在容器中通过 测试读速度并没有看到很好的效果,经查没有找到磁盘读操作的好工具,所以文中没有介绍读测试。

    • blkio.go