zoukankan      html  css  js  c++  java
  • 容器(四)限制容器对内存的使用【22】

    (六)限制容器对内存的使用

    ​ 一个 docker host 上会运行若干容器,每个容器都需要 CPU、内存和 IO 资源。对于 KVM,VMware 等虚拟化技术,用户可以控制分配多少 CPU、内存资源给每个虚拟机。对于容器,Docker 也提供了类似的机制避免某个容器因占用太多资源而影响其他容器乃至整个 host 的性能。

    (1)内存限额

    ​ 与操作系统类似,容器可使用的内存包括两部分:物理内存和 swap。 Docker 通过下面两组参数来控制容器内存的使用量。

    1. -m--memory:设置内存的使用限额,例如 100M, 2G。
    2. --memory-swap:设置 内存+swap 的使用限额。
    root@cuiyongchao:/dockerfile# docker run --memory=200M --memory-swap=300M ubuntu 
    
    

    ​ 其含义是允许该容器最多使用 200M 的内存和 100M 的 swap。默认情况下,上面两组参数为 -1,即对容器内存和 swap 的使用没有限制。

    ​ 下面我们将使用 progrium/stress 镜像来学习如何为容器分配内存。该镜像可用于对容器执行压力测试。执行如下命令:

    docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M
    --vm 1:启动 1 个内存工作线程。
    --vm-bytes 280M:每个线程分配 280M 内存。
    
    

    运行结果如下:

    root@cuiyongchao:/dockerfile# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M
    Unable to find image 'progrium/stress:latest' locally
    latest: Pulling from progrium/stress
    Image docker.io/progrium/stress:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
    a3ed95caeb02: Pull complete 
    871c32dbbb53: Pull complete 
    dbe7819a64dd: Pull complete 
    d14088925c6e: Pull complete 
    58026d51efe4: Pull complete 
    7d04a4fe1405: Pull complete 
    1775fca35fb6: Pull complete 
    5c319e267908: Pull complete 
    Digest: sha256:e34d56d60f5caae79333cee395aae93b74791d50e3841986420d23c2ee4697bf
    Status: Downloaded newer image for progrium/stress:latest
    WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
    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 ...
    stress: dbug: [6] freed 293601280 bytes
    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 ...
    stress: dbug: [6] freed 293601280 bytes
    stress: dbug: [6] allocating 293601280 bytes ...
    stress: dbug: [6] touching bytes in strides of 4096 bytes ...
    
    
    

    因为 280M 在可分配的范围(300M)内,所以工作线程能够正常工作,其过程是:

    1. 分配 280M 内存。
    2. 释放 280M 内存。
    3. 再分配 280M 内存。
    4. 再释放 280M 内存。
    5. 一直循环......

    如果让工作线程分配的内存超过 300M,结果如下:

    root@cuiyongchao:/dockerfile# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 310M
    
    

    分配的内存超过限额,stress 线程报错,容器退出。

    如果在启动容器时只指定 -m 而不指定 --memory-swap,那么 --memory-swap 默认为 -m 的两倍,比如:

    docker run -it -m 200M ubuntu

    容器最多使用 200M 物理内存和 200M swap。

  • 相关阅读:
    Python 模块的安装与使用
    Python——list切片
    IPv4与IPv6数据报格式
    计算机网络——网络层
    大型网站技术
    mysql主从复制数据库
    Laravel-安装composer
    centos7 yum安装配置redis
    最新cenos执行service httpd restart 报错Failed to restart httpd.service: Unit not found.
    Memcache安装
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/13967534.html
Copyright © 2011-2022 走看看