gpt4 book ai didi

docker 群 : how to publish a service only on a specific node that runs a task

转载 作者:行者123 更新时间:2023-12-02 18:09:40 25 4
gpt4 key购买 nike

我创建了一个 docker-compose.yml 文件,其中包含在两个不同节点上运行的两个服务。这两个服务旨在在与客户端和服务器相同的端口上进行通信。下面是我的 docker-compose.yml 文件。

 version: "3"
services:
service1:
image: localrepo/image1
deploy:
placement:
constraints: [node.hostname == node1]
replicas: 1
resources:
limits:
cpus: "1"
memory: 1000M
restart_policy:
condition: on-failure
ports:
- 8000:8000
networks:
- webnet

service2:
image: localrepo/image2
deploy:
placement:
constraints: [node.hostname == node2]
replicas: 1
resources:
limits:
cpus: "1"
memory: 500M
restart_policy:
condition: on-failure
ports:
- "8000:8000"
networks:
- webnet
networks:
webnet:

当我发出 docker stack deploy -c 时,我收到错误,正在读取

> Error response from daemon: rpc error: code = 3 desc = port '8000' is already in use by service.

在此thread我读到在 swarm 中部署服务可以使 swarm 中的任何节点都可以访问该端口。如果我理解正确的话,这使得该端口被集群中的任何节点占用。在同一个线程中,建议使用mode=host发布,这样只会暴露容器运行的实际主机上的端口。我在端口中将其应用为:

端口: - “模式=主机,目标=8000,发布=8000”

在服务中进行更改并尝试发布 docker stack 会产生另一个错误:

> 1 error(s) decoding:

* Invalid containerPort: mode=host, target=8000, published=8000

有谁知道如何解决这个问题?

p.s:“Version3”和“Version3.2”我都试过了,问题都没有解决。

最佳答案

我不知道你是如何指定host mode的,因为你的docker-compose.yml在任何地方都不代表host mode

顺便说一句,试试 long syntax可以在docker-compose.yml文件中指定host模式

这种长语法是 v3.2 中的新语法,下面是示例(我检查它有效)

(This 是与 docker-compose 语法版本兼容的 docker engine 版本。)

version: '3.4'     # version: '3.2' also will works

networks:
swarm_network:
driver: overlay


services:
service1:
image: asleea/test

command: ["nc", "-vlkp", "8000"]

deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.hostname == node1

ports:
- published: 8000
target: 8000
mode: host

networks:
swarm_network:

service2:
image: asleea/test

command: ["nc", "service1", "8000"]

deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.hostname == node2

ports:
- published: 8000
target: 8000
mode: host

networks:
swarm_network:

关于 docker 群 : how to publish a service only on a specific node that runs a task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48469763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com