简体   繁体   English

Docker容器CPU分配

[英]Docker container CPU allocation

I have created a container: 我创建了一个容器:

docker run -c=20 -i -t  ubuntu:latest /bin/bash

I tried to use -c flag to control CPU usage and maximize it in 50 %. 我尝试使用-c标志来控制CPU使用率并将其最大化50%。 When I am running md5sum /dev/urandom inside container, it use up 100 % CPU in host machine. 当我在容器内运行md5sum /dev/urandom ,它在主机中耗尽了100%的CPU。

The -c flag for docker run command modifies the container's CPU share weighting relative to the weighting of all other running containers. docker run命令的-c标志修改容器相对于所有其他正在运行的容器的权重的CPU份额权重。

It does not restrict the container's use of CPU from the host machine. 它不限制容器从主机使用CPU。

You can use the --cpu-quota flag to limit CPU usage, for example: 您可以使用--cpu-quota标志来限制CPU使用率,例如:

$ docker run -ti  --cpu-quota=50000 ubuntu:latest /bin/bash

The --cpu-quota is usually used in conjunction with --cpu-period . --cpu-quota通常与--cpu-period一起使用。 Please see more details on the Docker run reference document: 请参阅Docker运行参考文档的更多详细信息:

https://docs.docker.com/reference/run/#runtime-constraints-on-resources https://docs.docker.com/reference/run/#runtime-constraints-on-resources

It seems that you are running a single container, so this is the expected result. 您似乎正在运行单个容器,因此这是预期的结果。

You might find this blog post helpful. 您可能会发现博文有用。

Every new container will have 1024 shares of CPU by default. 默认情况下,每个新容器将拥有1024个CPU份额。 This value does not mean anything, when speaking of it alone. 当单独谈到它时,这个值并不意味着什么。 But if we start two containers and both will use 100% CPU, the CPU time will be divided equally between the two containers because they both have the same CPU shares (for the sake of simplicity I assume that there are no other processes running). 但是如果我们启动两个容器并且两者都将使用100%CPU,则CPU时间将在两个容器之间平均分配,因为它们都具有相同的CPU份额(为了简单起见,我假设没有其他进程在运行)。

Take a look here, this is apparently what you were looking for: 看看这里,这显然是你在寻找的:

https://docs.docker.com/engine/reference/run/#cpu-period-constraint https://docs.docker.com/engine/reference/run/#cpu-period-constraint

The default CPU CFS (Completely Fair Scheduler) period is 100ms. 默认CPU CFS(完全公平调度程序)周期为100毫秒。 We can use --cpu-period to set the period of CPUs to limit the container's CPU usage. 我们可以使用--cpu-period来设置CPU的周期来限制容器的CPU使用率。 And usually --cpu-period should work with --cpu-quota. 通常--cpu-period应该与--cpu-quota一起使用。

Examples: 例子:

$ docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash

If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms. 如果有1个CPU,这意味着容器每50ms可以获得50%的CPU运行时间。

period and quota definition: 期限和配额定义:

Within each given "period" (microseconds), a group is allowed to consume only up to "quota" microseconds of CPU time. 在每个给定的“周期”(微秒)内,允许一组仅消耗高达“配额”微秒的CPU时间。 When the CPU bandwidth consumption of a group exceeds this limit (for that period), the tasks belonging to its hierarchy will be throttled and are not allowed to run again until the next period. 当组的CPU带宽消耗超过此限制时(该时间段),属于其层次结构的任务将受到限制,并且在下一个时间段之前不允许再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM