微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用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

原文地址:https://www.jb51.cc/docker/435784.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐