简体   繁体   English

如何设置Docker容器的CPU优先级(niceness)?

[英]How to set the CPU priority (niceness) of a Docker container?

One of my containers is always busy, and is taking CPU away from other containers (webservers) that need to be responsive and are only active from time to time. 我的一个容器总是很忙,并且正在将CPU从需要响应且仅不时处于活动状态的其他容器(网络服务器)中夺走。

I would like to lower the CPU priority of the CPU-consuming container, so that whenever the other containers need the CPU, it is not clogged. 我想降低占用CPU的容器的CPU优先级,以便每当其他容器需要CPU时,它都不会被阻塞。

How do I do this? 我该怎么做呢? I have been searching the web for a while now, but I can't find the answer. 我已经在网上搜索了一段时间,但找不到答案。

I have tried running the container with --entrypoint='nice 10 mybinary' , but it turns out --entrypoint can only run binaries, not shell commands. 我试过使用--entrypoint='nice 10 mybinary'运行容器,但事实证明--entrypoint仅能运行二进制文件,而不能运行shell命令。

You can limit CPU resources on the container level. 您可以在容器级别限制CPU资源。 I recommend to use --cpu-shares 512 for your case. 我建议为您的情况使用--cpu-shares 512

https://docs.docker.com/config/containers/resource_constraints/ : https://docs.docker.com/config/containers/resource_constraints/

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. 将此标志设置为大于或小于默认值1024的值,以增加或减少容器的重量,并使其可以访问更多或更少比例的主机CPU周期。 This is only enforced when CPU cycles are constrained. 仅在限制CPU周期时才执行此操作。 When plenty of CPU cycles are available, all containers use as much CPU as they need. 当有足够的CPU周期可用时,所有容器都会根据需要使用尽可能多的CPU。 In that way, this is a soft limit. 这样,这是一个软限制。 --cpu-shares does not prevent containers from being scheduled in swarm mode. --cpu-shares不会阻止以群集模式调度容器。 It prioritizes container CPU resources for the available CPU cycles. 它将容器CPU资源的优先级分配给可用的CPU周期。 It does not guarantee or reserve any specific CPU access. 它不保证或保留任何特定的CPU访问权限。

--cpu-shares looks like a good answer, although it's not clear to me how to verify it's working. --cpu-shares看起来是一个不错的答案,尽管我不清楚如何验证它是否正常工作。 I'm also curious what the max value is? 我也很好奇最大值是多少? Document doesn't say. 文件没有说。

But, as an alternative for trusted containers, that same document also shows --cap-add=sys_nice that will allow changing process priorities within a container. 但是,作为可信任容器的替代方法,该文档还显示--cap-add=sys_nice ,它将允许更改容器内的进程优先级。 ie, if the nice or renice command is available within the container, it should work when you add the sys_nice capability. 即,如果容器中有nicerenice命令,则在添加sys_nice功能时它应该可以工作。 You'll only want to allow this capability for trusted containers because you don't want untrusted programs changing their own priorities willy nilly. 您只想允许此功能用于受信任的容器,因为您不希望不受信任的程序故意改变其优先级。

You can verify by inspecting the NI column for the process in question using top or ps -efl on the host. 您可以通过在主机上使用topps -efl检查NI进程中的有关进程来进行验证。

Setting the CPU shares is the most direct answer to your request, and typically preferred over adding capabilities to the container could be used by a malicious actor inside of the container to impact the host. 设置CPU份额是您请求的最直接答案,并且通常比容器中添加功能更受容器内部的恶意行为者影响主机。 The only reason I can think of to add the SYS_NICE capability to the container is if you have multiple processes inside the container and want to give different priorities to them, or need to change the priority while the container is running. 我可以想到的向容器添加SYS_NICE功能的唯一原因是,如果容器内有多个进程,并且希望为其赋予不同的优先级,或者需要在容器运行时更​​改优先级。

The more traditional solution to noisy neighbors is to configure each container with a limit on how much CPU and memory it is allowed to use. 对于嘈杂的邻居,更传统的解决方案是为每个容器配置一个允许使用多少CPU和内存的限制。 This is an upper bound, so realize there may be idle CPU resources if you set this low and do not have any other tasks available for the CPU to run. 这是一个上限,因此请注意,如果将此值设置得较低,并且没有其他可用于运行CPU的任务,则可能会有空闲的CPU资源。

The easiest way to set the limit on containers from the docker run command line is with --cpus which allows you to configure a fractional number of cores to be available to the container. 通过--cpus docker run命令行在容器上设置限制的最简单方法是使用--cpus ,它使您可以配置可用于容器的少量内核。 Passing an option like --cpus 2.5 allows the container to use as many as 2.5 cores before the kernel scheduler throttles the process. 传递--cpus 2.5类的选项可使容器在内核调度程序限制进程之前使用多达2.5个内核。 If you had a 4 core host, that would ensure that at least 1.5 cores are always available to other processes. 如果您有4核主机,那将确保其他进程始终至少有1.5核可用。

Related to these limits, with Swarm Mode you can also configure a reservation for CPU (and memory). 与这些限制相关,在群集模式下,您还可以配置CPU(和内存)的预留空间。 The reservation is a lower limit that Docker ensures has not been reserved for any other containers. 保留是Docker确保尚未为任何其他容器保留的下限。 This is used to select nodes to schedule containers, and may prevent some containers from being scheduled when there are not enough resources available, rather than scheduling so many jobs on a single node that it fails. 这用于选择要调度容器的节点,并且可能在没有足够的可用资源时阻止某些容器的调度,而不是在单个节点上调度如此多的作业而导致其失败。

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

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