docker - 如何使用 Docker Swarm 设置最低容器要求

标签 docker docker-compose docker-swarm system-requirements

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

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

我有一个容器,其最低系统要求为 2 个 CPU 核心和 4GB RAM,这正是我的 Docker Swarm 中节点的大小。这意味着当该容器运行时,它需要是该节点上运行的唯一容器。

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

更新

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

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.

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

如上所述here ,可以使用以下命令将节点标签添加到节点:

docker node update --label-add hello-world=yes <node-name>

然后在堆栈文件中,您可以限制容器仅在具有指定标签的节点上运行,而在其他容器上运行以避免使用 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 Swarm 设置最低容器要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46319170/

相关文章:

docker - 在docker-compose中创建一个全局变量od “extra_hosts”

docker - 如何删除早于 x 数量的 docker 堆栈

Docker 组合错误 : cannot restrict inter-container communication

python - 无法从外部连接到运行 CherryPy 服务器的 docker 容器

python - 服务 attention_ocr 模型时出错 ("error": "Expected one or two output Tensors, found 17")

php - 可以将Docker镜像设为 “built” (“compiled”)而不要求Docker吗?

node.js - 为什么我的 docker Node 容器退出

docker - Docker群模式-工作节点上没有已发布的端口

docker - 带有初始数据的 Neo4j 容器 (docker)

docker - Alpine 不可满足约束 : missing packages