在特定端口和主机名中部署副本
Deploy Replicas in specific ports and hostname
我需要在 swarm 中部署一个 docker 容器。我想部署 docker 容器的 4 个副本。我想设置每个容器 运行 的端口,我需要知道它们的主机名。
我想吃 4 个副本。
- 副本 1 应该监听端口 3001 和主机名 slave1。
- 副本 2 应该监听端口 3002 和主机名 slave2。
- 副本 3 应该监听端口 3003 和主机名 slave3。
- 副本 4 应该监听端口 3004 和主机名 slave4。
每个副本都有相同的 Dockerfile(因为我想要 运行 的过程是相同的)。在 Dockerfile 中,我使用标准命令公开了所有 4 个端口:
暴露 3001 3002 3003 3004
我尝试了这个 docker-compose 文件,我使用 4 个端口并使用 "mode:replicated"
进行部署
services:
slave:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 4
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3001:3001"
- "3002:3002"
- "3003:3003"
- "3004:3004"
networks:
my_net:
external: true
但它没有像我想的那样工作,如上所述。
希望问题描述有道理。请告诉我。
我认为您误解了 docker 群模式的工作方式。 Swarm 模式不适用于容器/节点级别,但它是更高的抽象级别 - 它适用于服务。
服务由给定数量的节点上的给定数量的容器 运行 实例组成。 Swarm 将处理 swarm 中有多少个容器实例 运行 并且它将处理服务容器在哪些节点上 运行 (当然您可以使用 replicas
和 constraints
).
群模式的一大好处是你不需要了解群的基础设施。您不关心那里有哪些节点以及哪个容器 运行 在哪个节点上。
相反,您只需告诉 swarm 您想要联系什么服务,swarm 模式将决定将您的请求发送到哪个节点上的哪个容器。
因此,在您的示例中,如果您的服务在端口 3001 上 运行,假设有一个名为 GET /hello
的 api 端点,您将请求 http://slave:3001/hello。这就是 swarm 模式发挥作用的地方,因为它知道哪些容器 运行 在哪些节点上,它将决定将您的请求转发到哪里。
如果你想让特定的容器监听特定节点上的特定端口,你必须定义多个服务并使用 constraints and labels 配置这些服务......所以你的 docker-compose.yml 会看起来像:
services:
slave1:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave1
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3001:3001"
slave2:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave2
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3002:3001"
slave3:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave3
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3003:3001"
slave4:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave4
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3004:3001"
networks:
my_net:
external: true
但请注意,这正在破坏 swarm 的大部分优势。
我需要在 swarm 中部署一个 docker 容器。我想部署 docker 容器的 4 个副本。我想设置每个容器 运行 的端口,我需要知道它们的主机名。
我想吃 4 个副本。
- 副本 1 应该监听端口 3001 和主机名 slave1。
- 副本 2 应该监听端口 3002 和主机名 slave2。
- 副本 3 应该监听端口 3003 和主机名 slave3。
- 副本 4 应该监听端口 3004 和主机名 slave4。
每个副本都有相同的 Dockerfile(因为我想要 运行 的过程是相同的)。在 Dockerfile 中,我使用标准命令公开了所有 4 个端口: 暴露 3001 3002 3003 3004
我尝试了这个 docker-compose 文件,我使用 4 个端口并使用 "mode:replicated"
进行部署services:
slave:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 4
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3001:3001"
- "3002:3002"
- "3003:3003"
- "3004:3004"
networks:
my_net:
external: true
但它没有像我想的那样工作,如上所述。
希望问题描述有道理。请告诉我。
我认为您误解了 docker 群模式的工作方式。 Swarm 模式不适用于容器/节点级别,但它是更高的抽象级别 - 它适用于服务。
服务由给定数量的节点上的给定数量的容器 运行 实例组成。 Swarm 将处理 swarm 中有多少个容器实例 运行 并且它将处理服务容器在哪些节点上 运行 (当然您可以使用 replicas
和 constraints
).
群模式的一大好处是你不需要了解群的基础设施。您不关心那里有哪些节点以及哪个容器 运行 在哪个节点上。
相反,您只需告诉 swarm 您想要联系什么服务,swarm 模式将决定将您的请求发送到哪个节点上的哪个容器。
因此,在您的示例中,如果您的服务在端口 3001 上 运行,假设有一个名为 GET /hello
的 api 端点,您将请求 http://slave:3001/hello。这就是 swarm 模式发挥作用的地方,因为它知道哪些容器 运行 在哪些节点上,它将决定将您的请求转发到哪里。
如果你想让特定的容器监听特定节点上的特定端口,你必须定义多个服务并使用 constraints and labels 配置这些服务......所以你的 docker-compose.yml 会看起来像:
services:
slave1:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave1
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3001:3001"
slave2:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave2
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3002:3001"
slave3:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave3
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3003:3001"
slave4:
image: "DOCKER_REPO_NAME"
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.type == slave4
restart_policy:
condition: on-failure
networks:
- my_net
ports:
- "3004:3001"
networks:
my_net:
external: true
但请注意,这正在破坏 swarm 的大部分优势。