对容器进行内存容量控制

对容器内存的限制

内存分为:物理内存和swap内存

关键词

--memory:限制内存的使用容量
--memory-swap:限制内存+swap的大小

启动一个限制内存使用的容器testa,如果不指定--memory-swap的话,默认它的大小和内存一样大

[root@localhost ~]# docker run -itd --name testa -m 200M --memory-swap 300M centos /bin/bash
aa90dc55398b2f927c1111bcfa3090502a3b42c3eb538895a1ddadbe556fdf9d

进入容器testa中查看内存和swap大小,在容器中看到的其实也还是物理机的内存大小

[root@localhost ~]# docker exec -it testa /bin/bash
[root@aa90dc55398b /]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770        1051         992          33        1726        2431
Swap:          2047           0        2047

第二种查看,最为准确,在宿主机的cgroup中查看,内存大小为200M

[root@localhost ~]# cat /sys/fs/cgroup/memory/docker/aa9...f9d/memory.limit_in_bytes 
209715200

还可以通过测试也能够测出来

内存+swap总量是300M,内存占200M,所以swap是100M,启动容器运行

测试使用280M是否可以正常运行,一直在输出说明正常

[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 [6] forked
stress: dbug: [6] allocating 293601280 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 293601280 bytes
stress: dbug: [6] allocating 293601280 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
...

测试使用380M是否可以正常运行,自动跳出,则不能运行,说明380M超出了内存范围

[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 [6] forked
stress: dbug: [6] allocating 398458880 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 6 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 1s
上一篇:JAVA设计模式之工厂模式


下一篇:到底应该怎么理解“平均负载”?