memoryReservation 在带有 Fargate 的 ECS 上实际上做了什么?

What does memoryReservation actually do on ECS with Fargate?

ECS 的容器定义允许您为每个容器指定一个 memoryReservation

The soft limit (in MiB) of memory to reserve for the container. When system memory is under contention, Docker attempts to keep the container memory to this soft limit; however, your container can consume more memory when needed, up to either the hard limit specified with the memory parameter (if applicable), or all of the available memory on the container instance, whichever comes first. This parameter maps to MemoryReservation in the Create a container section of the Docker Remote API and the --memory-reservation option to docker run...If you specify memoryReservation, then that value is subtracted from the available memory resources for the container instance on which the container is placed; otherwise, the value of memory is used. For example, if your container normally uses 128 MiB of memory, but occasionally bursts to 256 MiB of memory for short periods of time, you can set a memoryReservation of 128 MiB, and a memory hard limit of 300 MiB. This configuration would allow the container to only reserve 128 MiB of memory from the remaining resources on the container instance, but also allow the container to consume more memory resources when needed.

我相信这是使用 cgroups 的“soft limits”实现的。

这意味着,在 ECS+EC2 上,此选项的目的是允许您在一个实例上安排一组任务,如果这些任务都同时使用了最大内存,将超过可用的总内存在实例中,但您预测他们不会那样做。

但是在 ECS+Fargate 上呢?无论您设置的内存限制如何,Fargate 都有一个 task size and the combined hard limits ("memory" option) of a task's containers cannot exceed the task size's memory. [This last part was wrong; see my clarifying answer.] You are billed based on the task size。那么,将 memoryReservation 设置为低于硬限制可能会带来什么好处?

我能想到的唯一可能是:

  1. memoryReservation 指示 AWS 将您的容器打包到较少数量的底层 VM 上,冒着性能问题的风险对您没有好处(因为价格相同)。
  2. AWS 只是忽略 Fargate 的 memoryReservation

我是否错过了任何可能性?如果不是,亚马逊会说是两者中的哪一个吗?

在一个任务中有多个容器的情况呢?您可能希望为一个容器保留一定量的内存。此外,您可能会遇到不同容器同时需要大量内存的情况,并且您不希望它们中的任何一个占用太多内存并拖慢其余容器。

我现在意识到我的问题中有一个不正确的前提:

Fargate has a task size and the combined hard limits ("memory" option) of a task's containers cannot exceed the task size's memory.

事实上,组合的软限制("memoryReservation"选项)不能超过任务内存。合并的硬限制可以。因此,在 Fargate 上,您可以在单个任务中安排多个 "bursty" 容器,并且单个容器在被杀死之前可能会用完任务的全部可用内存。这有点类似于您可以使用 single-container 任务和 memoryReservation 在 EC2 实例上实现的。

不过,您不需要使用 memoryReservation 在 Fargate 上完成它(您可以不设置任何超出任务大小的内存限制或预留)。所以问题仍然存在,为什么要在 Fargate 上使用该设置?我认为最好的答案可能是 MasterFowl 提供的答案:提示内核不要让您的容器之一占用太多任务内存。