加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 03:49:21 所属栏目:安全 来源:网络整理
导读:在Docker Swarm中,您可以设置最大系统要求,如下所示: my-service image: hello-world deploy: resources: limits: cpus: '2' memory: 4GB 我有一个容器,其最低系统要求是2个CPU内核和4GB RAM,这是我的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读