docker资源限制


首先拉取一个progrium/stress的镜像

[root@localhost ~]# docker pull progrium/stress

内存限额

不超过内存限额:

[root@localhost ~]# docker run -it -m 200M --memory-swap 300M progrium/stress --vm 1 --vm-bytes 280M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [8] forked
stress: dbug: [8] allocating 293601280 bytes ...
stress: dbug: [8] touching bytes in strides of 4096 bytes ...
stress: dbug: [8] freed 293601280 bytes
stress: dbug: [8] allocating 293601280 bytes ...
stress: dbug: [8] touching bytes in strides of 4096 bytes ...

超过内存限额:

[root@localhost ~]# docker run -it -m 200M --memory-swap 300M progrium/stress --vm 1 --vm-bytes 380M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [8] forked
stress: dbug: [8] allocating 398458880 bytes ...
stress: dbug: [8] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 8 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) <em><strong>kill error: No such process        //可以看见这里报错了</strong></em>
stress: FAIL: [1] (452) failed run completed in 2s

CPU限额

第一个容器指定1024:

[root@localhost ~]# docker run -it --name cpu1 -c <em><strong>1024</strong></em> progrium/stress --cpu 1
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogcpu worker 1 [8] forked

第二个容器指定512:

[root@localhost ~]# docker run -it --name cpu2 -c <strong><em>512</em></strong> progrium/stress --cpu 1
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogcpu worker 1 [8] forked

然后使用top命令查看CPU使用情况;可以看到两者的CPU使用率接近2:1

磁盘限额

限速bps 30M/s

[root@localhost ~]# docker run -it --name testA --device-write-bps /dev/sda:30MB centos
[root@b0b1e003e11e /]# time dd if=/dev/zero of=test.out bs=1M count=800 oflag=direct
800+0 records in
800+0 records out
838860800 bytes (839 MB) copied, 26.7244 s, 31.4 MB/s

real 0m26.757s
user 0m0.002s
sys 0m3.482s

对比不限速

[root@localhost ~]# docker run -it --name testB centos
[root@localhost ~]# docker run -it --name testB centos[root@7011f4487e71 /]# time dd if=/dev/zero of=test.out bs=1M count=800 oflag=direct
800+0 records in
800+0 records out
838860800 bytes (839 MB) copied, 13.0204 s, 64.4 MB/s

real 0m13.024s
user 0m0.014s
sys 0m12.043s
可以看出有明显的区别
Last modification:July 24th, 2020 at 12:05 am
如果觉得我的文章对你有用,请随意赞赏