Limit a container's resources

Limit a container’s resources

译自 https://docs.docker.com/engine/admin/resource_constraints/

默认情况下,容器没有资源限制,可以使用系统所有资源。docker 通过 docker run 配置容器的内存,cpu, 磁盘io使用量。

Memory

Understand the risks of running out of memory

限制容器对系统内存的使用量是非常重要的。对于Linux 主机,如果没有足够的内容来执行重要的系统任务,将会抛出 OOM 或者 Out of Memory Exception, 随后系统会开始杀死进程以释放内存。每个进程都有可能被 kill,包括Dockerd和其它的应用程序。如果重要的系统进程被Kill,会导致整个系统宕机。

产生 OOM 异常时,Docker 通过调整 Docker Daemon 的优先级来减少重要系统进程被终止的风险。但是容器的优先级未做调整。这使得单个容器相比Docker Daemon和其他系统进程更容易被 kill 掉。你不应该通过手动调整--oom-score-adj--oom-disable-kill,来避开这些安全措施。

原文:
Docker attempts to mitigate these risks by adjusting the OOM priority on the Docker daemon so that it will be less likely to be killed than other processes on the system. The OOM priority on containers is not adjusted. This makes it more likely that an individual container will be killed than that the Docker daemon or other system processes will be killed. You should not try to circumvent these safeguards by manually setting --oom-score-adj to an extreme negative number on the daemon or a container, or by setting --oom-disable-kill on a container.

有关 Linux kernel’s OOM 管理,参考 Out of Memory Management

可以通过以下建议来减少 OOM 带来的风险:

  • Perform tests to understand the memory requirements of your application before placing it into production.
  • Ensure that your application runs only on hosts with adequate resources.
  • Limit the amount of memory your container can use, as described below.
  • Be mindful when configuring swap on your Docker hosts. Swap is slower and less performant than memory but can provide a buffer against running out of system memory.
  • Consider converting your container to a service, and using service-level constraints(约束) and node labels to ensure that the application runs only on hosts with enough memory

Limit a contains’s access to memory

Docker 可以强制执行硬性内存限制,即只允许容器使用给定的内存大小。
Docker 也可以执行非硬性内存限制,即容器可以使用尽可能多的内存,除非内核检测到主机上的内存不够用了。

OptionDescription
-m or --memory容器能使用的内存最大值。如果设置了该值,所允许的最小值为4字节
--memory-swap *容器可以交换到磁盘的内存大小(即容器可用的 swap 大小)。 details
--memory-swappiness默认情况下,主机内核可以置换的容器所使用匿名页的百分比。 --memory-swappiness的值在 0 ~ 100之间。details
--memory-reservation允许你设置一个比 --memory 小非硬性限制。当docker检测到竞争或者主机内存不足时该配置被激活。如果配置了 --memory-reservation, 一定要比 --memory 值小。因为是非硬性限制,所以不能保障容器占用的内存不会超出该值限定的范围。
--kernel-memory容器所能使用的最大内核内存。最小值为4m。由于内核内存无法被置换,占用大量内核内存的容器可能会使主机以及主机上其他的容器受影响。detail
--oom-kill-disable默认情况下,发生 OOM 时,kernel会杀死容器内进程。使用 --oom-kill-disable 可以阻止这种情况发生。使用该参数的同事,需要配置--memory。如果-m 参数未配置,产生OOM时,主机为了释放内存会杀死系统进程
--memory-swap detail

--memory-swap 只有在设置了 --memory 后才会有意义。使用Swap,可以让容器将超出限制部分的内存置换到磁盘上。swap过于频繁的话,会影响系统性能。

不同的设置会产生不同的效果:

  • --memory-swap 值为正整数, 那么--memory--memory-swap都必须要设置。--memory-swap 表示你能使用的内存和swap分区大小的总和。eg. --memory=300m, --memory-swap=1g, 那么该容器能够使用 300m 内存和 700m swap.
  • --memory-swap=0, 表示使用默认值。这种情况下,容器能使用的内存大小为 a,能使用的交换分区大小也为 a。因为 Docker 默认容器交换分区的大小和内存相同。
    如果在容器中运行一个一直不停申请内存的程序,你会观察到该程序最终能占用的内存大小为 2a。
    eg: $ docker run -m 1G ubuntu:16.04,该容器能使用的内存大小为 1G,能使用的 swap 分区大小也为 1G。容器内的进程能申请到的总内存大小为 2G。
  • --memory-swap的值和--memory的值一样。同--memory-swap=0一样。
  • --memory-swap 未被设置,--memory=x.容器可以使用 x 内存, 2x swap。
  • --memory-swap=-1, 容器可以使用任意大小的swap,上限不得超过主机swap大小。
--memory-swappiness detail

  • 0 , 表示关闭匿名页置换
  • 100, 所有匿名页都可交换
  • default(即不设置 --memory-swappiness), 值会从主机上继承。
--memory-reservation detail

官方网站上并无详细介绍,这里引用http://blog.csdn.net/candcplusplus/article/details/53728507中的一段话:

Memory reservation 是一种软性限制,用于节制容器内存使用。给--memory-reservation设置一个比-m小的值后,虽然容器最多可以使用-m使用的内存大小,但在宿主机内存资源紧张时,在系统的下次内存回收时,系统会回收容器的部分内存页,强迫容器的内存占用回到--memory-reservation设置的值大小。

没有设置时(默认情况下)--memory-reservation的值和-m的限定的值相同。将它设置为 0 会设置的比-m的参数大 等同于没有设置。

Memory reservation 是一种软性机制,它不保证任何时刻容器使用的内存不会超过--memory-reservation限定的值,它只是确保容器不会长时间占用超过--memory-reservation限制的内存大小。

例如:

$ docker run -it -m 500M --memory-reservation 200M ubuntu:16.04 /bin/bash

如果容器使用了大于 200M 但小于 500M >内存时,下次系统的内存回收会尝试将容器的内存锁紧到 200M 以下。

例如:

$ docker run -it --memory-reservation 1G ubuntu:16.04 /bin/bash

容器可以使用尽可能多的内存。--memory-reservation确保容器不会长时间占用太多内存。

--kernel-memory details

内核内存限制用分配给容器的总体内存来表示。 考虑以下情况:

  • Unlimited memory, unlimited kernel memory: 默认行为
  • Unlimited memory, limited kernel memory: 当所有cgroup所需的内存量大于主机上实际存在的内存量时,这是适当的。 您可以将内核内存配置为永远不会超过主机上可用的内存,而需要更多内存的容器需要等待。
  • Limited memory, umlimited kernel memory: 整体内存受限,但内核内存不受限
  • Limited memory, limited kernel memory: 限制用户和内核内存对于调试与内存有关的问题都很有用。 如果一个容器正在使用意外的任何一种类型的内存,它将耗尽内存而不会影响其他容器或主机。 在此设置中,如果内核内存限制低于用户内存限制,则内存内存不足会导致容器遇到OOM错误。 如果内核内存限制高于用户内存限制,则内核限制不会导致容器遇到OOM。

CPU limitation

to be continue

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值