您的位置:首页 > 运维架构 > Docker

Docker 限制容器资源

2017-06-12 11:00 549 查看
默认情况下,容器没有资源的限制,它可以使用整个主机的所有资源。Dcoker提供了控制资源的方法,
多少内存,CPU,IO,都可以在docker run使用标志符来设置。

内存

Docker可以强制执行硬内存限制,允许容器使用不超过给定数量的用户或系统内存,
或软限制,允许容器使用所需的内存,除非满足某些条件,例如 内核检测到主机上的低内存或争用。
当单独使用或设置多个选项时,这些选项中的一些具有不同的效果。
选项说明
-m
or
--memory=
容器能使用的最大的内存。
如果你设置了这个选项,最小允许使用的值为4M。
--memory-swap
*
容器允许swap的内存大小。
更多细节:
--memory-swap
details
.
--memory-swappiness
默认情况下,不需要设置。
主机内核可以让容器使用一定比例的匿名内存页。你可以通过这个参数来设置这个百分比。更多细节:
--memory-swappiness
details
.
--memory-reservation
允许你指定软限制,比--memory 参数值要小。这软限制在Docker检测到主机上的连接或内存较少时激活。
--kernel-memory
容器可以使用的最大的内核内存。最小值是4M。 因为内核内存不能被交换出去,一个缺少内核内存的容器可能会阻碍主机的资源,这会对主机和其他容器产生负面影响。
更多细节:
--kernel-memory
details
.
--oom-kill-disable
默认情况下:out-of-memory(oom)内存溢出的错误出现,内核杀死容器中的进程。使用--oom-kill-disable 可以改变这种行为。 仅仅在已经设置了-m 的容器中关闭 OOM 杀死。 如果-m 标志没有设置,主机可能耗尽内存,内核可能需要杀死主机系统进程来释放内存。
--memory-swap 细节

如果没有设置,而--me内核内存限制以分配给容器的总内存来表示。mory设置如300m了,则swap默认为300*2=600M

如果--memory,--memory-swap都设置了

--memory-swap 代表了可以使用的总共内存,--memory 代表不使用swap的内存

如:--memory-swap=1g,--memory=300M,

docker可以使用300M内存,1g-300m=700m swap.

如果设置为-1,则容器不限制使用swap 内存。

CPU

OptionDescription
--cpu-shares
Set this flag to a value greater or less than the default of 1024 to increase or reduce the container’s weight, and give it access to a greater or lesser proportion of the host machine’s CPU cycles. This is only enforced when CPU cycles are constrained. When plenty of CPU cycles are available, all containers use as much CPU as they need. In that way, this is a soft limit.
--cpu-shares
does not prevent containers from being scheduled in swarm mode. It prioritizes container CPU resources for the available CPU cycles. It does not guarantee or reserve any specific CPU access.
--cpu-period
容器上一个逻辑CPU的调度周期.
--cpu-periodmor默认值是100000(100ms)
--cpu-quota
在由--cpu-period设置的时间段内容器可以调度的最大时间量。
--cpuset-cpus
使用这个选项指定CPU使用一个或者多个CPU核,并用逗号分隔。
--cpu-period 和 --cpu-qota
例子:如果你有1核的CPU系统,容器run使用--cpu-period=100000
--cpu-quota=50000,容器可以消耗高达50%的1个CPU

$ docker run -ti --cpu-period=10000 --cpu-quota=50000 busybox


如果你有4核的CPU系统,容器运行--cpu-period=100000
--cpu-quota=200000,容器可以使用最多2个逻辑CPU(--cpu-period的200%)

$ docker run -ti --cpu-period=100000 --cpu-quota=200000


--cpu-set-cpus
给容器实际4核的CPU

$ docker run -ti --cpuset-cpus=4 busybox


阻塞IO

有两个选项可用于调整给定容器对直接块IO设备的访问。
您还可以按照每秒的字节数或每秒的IO操作来指定带宽限制。

OptionDescription
blkio-weight
By default, each container can use the same proportion of block IO bandwidth (blkio). The default weight is 500. To raise or lower the proportion of blkio used by a given container, set the
--blkio-weight
flag to a value between 10 and 1000. This setting affects all block IO devices equally.
blkio-weight-device
The same as
--blkio-weight
, but you can set a weight per device, using the syntax
--blkio-weight-device="DEVICE_NAME:WEIGHT"
The DEVICE_NAME:WEIGHT is a string containing a colon-separated device name and weight.
--device-read-bps
and
--device-write-bps
Limits the read or write rate to or from a device by size, using a suffix of
kb
,
mb
, or
gb
.
--device-read-iops
or
--device-write-iops
Limits the read or write rate to or from a device by IO operations per second.
如果指定--blkio-weight和-blkio-weight-device,Docker使用--blkio-weight作为默认权重,
并使用--blkio-weight-device重写命名设备上的默认值。

要将/ dev / sda的容器的设备权重设置为200,而不指定默认blkio-weight:

$ docker run -it \
--blkio-weight-device "/dev/sda:200" \
ubuntu


此示例将ubuntu容器限制为从/ dev / sda到每秒1000次IO操作的最大读取速率:

$ docker run -ti --device-read-iops /dev/sda:1000 ubuntu


总结

默认容器没有资源的限制,Docker提供了控制方法;

内存,CPU,IO

docker run + 标志符来设置

主要参数

内存

-m 容器能使用的最大内存

--memory--reservation

比-m的值要小,在docker检测到主机的内存较少时激活

CPU

--cpu-period, --cpu-quota

允许容器使用50%的CPU

--cpu-period = 10 ,--cpu-quota=5

允许容器使用主机4核中的两核

--cpu-period = 10 , --cpu-quota=20

IO

控制每秒的字节数或每秒的IO操作来限制带宽

限制从/dev/sda 每秒1000次的IO读取操作

docker run -ti --device-read-iops /dev/sda:1000 ubuntu
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: