限制容器对内存的使用
一个 docker host 上会运行若干容器,每个容器都需要 CPU、内存和 IO 资源。
对于 KVM,VMware 等虚拟化技术,用户可以控制分配多少 CPU、内存资源给每个虚拟机。
对于容器,Docker 也提供了类似的机制避免某个容器因占用太多资源而影响其他容器乃至整个 host 的性能。
内存限额
容器可使用的内存包括两部分:
- 物理内存
- swap
Docker 通过下面两组参数来控制容器内存的使用量。
-m
或--memory
:设置内存的使用限额,例如 100M, 2G。--memory-swap
:设置 内存+swap 的使用限额。
举例:
docker run -m 200M --memory-swap=300M ubuntu
- 允许该容器最多使用 200M 的内存和 100M 的 swap。
- 默认情况下,上面两组参数为 -1,即对容器内存和 swap 的使用没有限制。
注:progrium/stress 镜像可用于对容器执行压力测试。
--vm 1
:启动 1 个内存工作线程。--vm-bytes 280M
:每个线程分配 280M 内存。
1 [email protected]:~# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M 2 Unable to find image ‘progrium/stress:latest‘ locally 3 latest: Pulling from progrium/stress 4 a3ed95caeb02: Pull complete 5 871c32dbbb53: Pull complete 6 dbe7819a64dd: Pull complete 7 d14088925c6e: Pull complete 8 58026d51efe4: Pull complete 9 7d04a4fe1405: Pull complete 10 1775fca35fb6: Pull complete 11 5c319e267908: Pull complete 12 Digest: sha256:e34d56d60f5caae79333cee395aae93b74791d50e3841986420d23c2ee4697bf 13 Status: Downloaded newer image for progrium/stress:latest 14 WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap. 15 stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd 16 stress: dbug: [1] using backoff sleep of 3000us 17 stress: dbug: [1] --> hogvm worker 1 [6] forked 18 stress: dbug: [6] allocating 293601280 bytes ... 19 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 20 stress: dbug: [6] freed 293601280 bytes 21 stress: dbug: [6] allocating 293601280 bytes ... 22 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 23 stress: dbug: [6] freed 293601280 bytes 24 stress: dbug: [6] allocating 293601280 bytes ... 25 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 26 stress: dbug: [6] freed 293601280 bytes 27 stress: dbug: [6] allocating 293601280 bytes ...
- 分配 280M 内存。
- 释放 280M 内存。
- 再分配 280M 内存。
- 再释放 280M 内存。
- 一直循环......
如果让工作线程分配的内存超过 300M,结果如下:
1 [email protected]:~# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 380M 2 WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap. 3 stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd 4 stress: dbug: [1] using backoff sleep of 3000us 5 stress: dbug: [1] --> hogvm worker 1 [6] forked 6 stress: dbug: [6] allocating 398458880 bytes ... 7 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 8 stress: dbug: [6] freed 398458880 bytes 9 stress: dbug: [6] allocating 398458880 bytes ... 10 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 11 stress: dbug: [6] freed 398458880 bytes 12 stress: dbug: [6] allocating 398458880 bytes ... 13 stress: dbug: [6] touching bytes in strides of 4096 bytes ... 14 stress: dbug: [6] freed 398458880 bytes 15 stress: dbug: [6] allocating 398458880 bytes ...
- 分配的内存超过限额,stress 线程报错,容器退出。
ps:docker host 未设置swap 则不支持限制内存swap
如果在启动容器时只指定 -m
而不指定 --memory-swap
,那么 --memory-swap
默认为 -m
的两倍
比如:docker run -it -m 200M ubuntu 容器最多使用 200M 物理内存和 200M swap。
--------------引用来自---------------
https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587660&idx=1&sn=9b2b36c378026cad974913e537e84aa4&chksm=8d3080d5ba4709c3b0efa550c89e511099a913746dc5d6ac3d38e925dd7663dac4f6d692c691&scene=21#wechat_redirect
原文地址:https://www.cnblogs.com/gsophy/p/10306789.html
时间: 2024-11-07 20:50:23