在用docker swarm想做跨主机容器互通的时候遇到容器互不通的问题,研究了好久
docker swarm init #管理节点上执行
docker swarm join --token SWMTKN-1-0iq85hac12m20vmc19s5xzlmnxi77ab74dn4adpuoa44bi0xmj-58e2jqov91fu2v7vw5vzp6coj 10.10.10.203:2377 #工作切点上执行
version: "3.7"
networks:
network-nginx:
driver: overlay
services:
nginx1:
image: nginx:1.19.2
ports:
- 8801:80
networks:
- network-nginx
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
nginx2:
image: nginx:1.19.2
ports:
- 8800:80
networks:
- network-nginx
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
docker stack deploy -c docker-compose-nginx.yml nginx
root@localhost-203-root-all docker-swarm]# docker stack ls
NAME SERVICES ORCHESTRATOR
nginx 2 Swarm
结果是在nginx1容器curl nginx2:80卡住不响应
在nginx2容器curl nginx1:80同样卡住不响应
docker network ls
tepstma6heze nginx_network-nginx overlay swarm
发现两个节点都有创建网络
docker run -id -p 8500:8500 harbor.xxx.com/xxx/consul:1.7.3
ExecStart=/usr/bin/dockerd后面增加--cluster-store=consul://10.10.10.203:8500 --cluster-advertise=ens192:2376
修改配置后
systemctl daemon-reload
systemctl restart docker
docker network create -d overlay --subnet 10.10.20.0/24 --attachable net
docker network ls
docker run -it --net=net busybox
发现nginx1仍然ping不通nginx2
尝试关闭防火墙,再测试,居然可以了。也不知道是什么原因。我在想应该是docker swarm启用了自己的端口吧。
原文:https://blog.51cto.com/riverxyz/3212490