2019/3/21 容器限制块设备I/O
在server节点,使用centos:latest镜像,创建名为blocklimit的容器。创建完之后进入容器,使用dd命令,测试写入41MB数据所花的时间。(dd if=/dev/zero of=testfile0 bs=8k count=5000 oflag=direct)然后通过修改相应的Cgroup文件来限制写磁盘的速度为1024000字节,最后使用dd命令测试写41MB花的时间来验证I/O是否限制了。将上述操作命令和返回结果以文本形式提交到答题框。
[root@server ~]# docker run -tid --name blocklimit 192.168.200.111:5000/centos:latest
297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d
[root@server ~]# docker exec -ti 297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d bash
[root@297f541c44e8 /]# dd if=/dev/zero of=testfile0 bs=8k count=5000 oflag=direct
5000+0 records in
5000+0 records out
40960000 bytes (41 MB) copied, 0.262266 s, 156 MB/s
[root@297f541c44e8 /]# df -h 查看宿主机上对应设备号
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:1-150995307-da906bdcb115c703cf9a54ded36ef8561c0bfea8cce620ad35784fbcb6e538a7 10G 289M 9.8G 3% /
tmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/vda1 40G 20G 21G 50% /etc/hosts
shm 64M 0 64M 0% /dev/shm
tmpfs 2.0G 0 2.0G 0% /proc/scsi
[root@server dev]# cd /dev/mapper/
[root@server mapper]# ll
total 0
crw------- 1 root root 10, 236 Mar 20 16:20 control
lrwxrwxrwx 1 root root 7 Mar 20 16:20 docker-253:1-150995307-2e1f28b864ae4851cd080e03d04f5ac5712f9aca58606f811a47b581116a0f21 -> ../dm-2
lrwxrwxrwx 1 root root 7 Mar 20 16:20 docker-253:1-150995307-be98b173df17fa551d9ca5a2b71ebb857b072609eab309764b2bc429a267f78a -> ../dm-1
lrwxrwxrwx 1 root root 7 Mar 20 16:41 docker-253:1-150995307-da906bdcb115c703cf9a54ded36ef8561c0bfea8cce620ad35784fbcb6e538a7 -> ../dm-3
lrwxrwxrwx 1 root root 7 Mar 20 16:20 docker-253:1-150995307-pool -> ../dm-0
[root@server mapper]# ls -l ../dm-3
brw-rw---- 1 root disk 252, 3 Mar 20 16:41 ../dm-3 得到链接设备的对应主副设备号 252:3
[root@server docker-297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d.scope]# pwd
/sys/fs/cgroup/blkio/system.slice/docker-297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d.scope
[root@server docker-297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d.scope]# echo "252:3 1024000" > blkio.throttle.write_bps_device
[root@server docker-297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d.scope]# docker exec -ti 297f541c44e8b0124efa240779cfd126aa2aad5a2ca9102fc4c200cb4f81826d bash
[root@297f541c44e8 /]# dd if=/dev/zero of=testfile0 bs=8k count=5000 oflag=direct
5000+0 records in
5000+0 records out
40960000 bytes (41 MB) copied, 39.9053 s, 1.0 MB/s
***************************
另一解法
[root@server ~]# docker run -tid --name blocklimit 192.168.200.116:5000/centos:latest
ae5a00693a19845879a0677c50db003195a2928311332336e8fa4ae3b506d13d
[root@server ~]# docker exec -ti blocklimit /bin/bash
root@ae5a00693a19:/# dd if=/dev/zero of=testfile0 bs=8k count=5000 oflag=direct
5000+0 records in
5000+0 records out
40960000 bytes (41 MB) copied, 0.625001 s, 65.5 MB/s
[root@server ~]# cd /dev/mapper/
[root@server mapper]# docker inspect -f "{{.GraphDriver.Data.DeviceName}}" blocklimit
docker-253:1-75579855-037a636640e010789bae5740bb610f8cbcd661e7c68b24b8d979e8ef98ee145a
[root@server mapper]# ls -l
total 0
crw------- 1 root root 10, 236 Apr 27 06:27 control
lrwxrwxrwx 1 root root 7 Apr 29 09:31 docker-253:1-75579855-037a636640e010789bae5740bb610f8cbcd661e7c68b24b8d979e8ef98ee145a -> ../dm-3
lrwxrwxrwx 1 root root 7 Apr 29 08:39 docker-253:1-75579855-55238fdfd787f2d178cccde03c9919478614c9602dc4eedcc1ddfaa63f057b74 -> ../dm-2
lrwxrwxrwx 1 root root 7 Apr 29 08:39 docker-253:1-75579855-6aacdee53d0e5232e8199f46e85a469e6cfa2aacd357f891e7b63bdadd5d0253 -> ../dm-1
lrwxrwxrwx 1 root root 7 Apr 29 08:39 docker-253:1-75579855-pool -> ../dm-0
[root@server mapper]# ls -l /dev/dm-3
brw-rw---- 1 root disk 252, 3 Apr 29 09:31 /dev/dm-3
[root@server mapper]# echo "252:3 1024000" > /sys/fs/cgroup/blkio/system.slice/docker-ae5a00693a19845879a0677c50db003195a2928311332336e8fa4ae3b506d13d.scope/blkio.throttle.write_bps_device
[root@server mapper]# docker exec -ti blocklimit /bin/bash
root@ae5a00693a19:/# dd if=/dev/zero of=testfile0 bs=8k count=5000 oflag=direct
5000+0 records in
5000+0 records out
40960000 bytes (41 MB) copied, 40.0085 s, 1.0 MB/s