如何使用Docker Swarm设置最小容器需求

在Docker Swarm中,您可以设置最大系统要求,如下所示:

my-service
  image: hello-world
  deploy:
    resources:
      limits:
        cpus: '2'
        memory: 4GB

我有一个容器,其最低系统要求是2个cpu内核和4GB RAM,这是我的Docker Swarm中节点的确切大小.这意味着当此容器运行时,它必须是该节点上运行的唯一容器.

但是,当我将容器与其他容器一起运行时,其他容器将放置在同一节点上.如何确保Docker为此容器提供最低级别的cpu和RAM?

更新

添加了@yamenk建议的预留,但是我仍然在同一节点上启动其他容器,这会导致我试图保护的容器出现性能问题:

my-service
  image: hello-world
  deploy:
    resources:
      reservations:
        cpus: '2'
        memory: 4GB
最佳答案
更新

显然,在docker swarm中记忆保留的效果并没有很好的记录,它们是最好的努力.要了解内存预留标志的影响,请检查documentation

When memory reservation is set,Docker detects memory contention or
low memory and forces containers to restrict their consumption to a
reservation limit.

Memory reservation is a soft-limit feature and does not guarantee
the limit won’t be exceeded. Instead,the feature attempts to ensure
that,when memory is heavily contended for,memory is allocated based
on the reservation hints/setup.

要强制在同一节点上没有其他容器运行,您需要设置服务约束.您可以做的是为swarm特定标签中的节点提供节点,并使用这些标签来调度服务,使其仅在具有这些特定标签的节点上运行.

如描述的那样,可以使用以下命令将节点标签添加到节点:

docker node update --label-add hello-world=yes 

然后在堆栈文件中,您可以限制容器在仅具有指定标签的节点上运行,而将其他容器限制为避免标记为hello-world = yes的节点.

my-service:
  image: hello-world
  deploy:
    placement:
      constraints:
        - node.labels.hello-world == yes

other-service:
  ...
  deploy:
    placement:
      constraints:
        - node.labels.hello-world == no

如果要在多个节点上启动my-service的副本,并且仍然在每个节点上运行一个容器,则需要设置my-service的全局模式,并将相同的标签添加到要运行容器的节点.

全局模式确保只有一个容器将运行满足服务约束的每个节点:

my-service:
  image: hello-world
  deploy:
    mode: global
    placement:
      constraints:
        - node.labels.hello-world == yes

旧答案:

您可以设置资源预留:

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        reservations:
          cpus: '1'
          memory: 20M

相关文章

Docker是什么Docker是 Docker.Inc 公司开源的一个基于 LXC技...
本文为原创,原始地址为:http://www.cnblogs.com/fengzheng...
镜像操作列出镜像:$ sudo docker imagesREPOSITORY TAG IMA...
本文原创,原文地址为:http://www.cnblogs.com/fengzheng/p...
在 Docker 中,如果你修改了一个容器的内容并希望将这些更改...
在Docker中,--privileged 参数给予容器内的进程几乎相同的权...