简化群:
manager1 node
- consul-agent
worker1 node
- consul-client1
- web-app:80
- web-network:9000
database1 node
- consul-client2
- redis:6379
- mongo:27017
web-app
和web-network
服务可以通过其服务名称正确连接到redis
和mongo
,例如redis.createClient('6379', 'redis')
和mongoose.connect('mongodb://mongo')
。< / p>
但是,容器web-app
无法连接到web-network
,我试图提出这样的请求:
request('http://web-network:9000')
但是得到错误:
errorno: ECONNREFUSED
address: 10.0.1.9
port: 9000
使用私有IP请求web-network
确实有效:
request('http://11.22.33.44:9000')
我错过了什么?为什么他们可以连接到redis
和mongo
但不能连接到每个容器之间?将redis
/ mongo
移动到与web-app
相同的节点时,它仍然有效,因此我认为问题不会发生,因为服务无法与服务进行通信相同的服务器节点。
我们能否让docker网络使用私有IP而不是预先配置的子网?
docker stack deploy file
version: '3'
services:
web-app:
image: private-repo/private-image
networks:
- swarm-network
ports:
- "80:8080"
deploy:
placement:
constraints:
- node.role==worker
web-network:
image: private-repo/private-image2
networks:
- swarm-network
ports:
- "9000:8080"
deploy:
placement:
constraints:
- node.role==worker
redis:
image: redis:latest
networks:
- swarm-network
ports:
- "6739:6739"
deploy:
placement:
constraints:
- engine.labels.purpose==database
mongo:
image: mongo:latest
networks:
- swarm-network
ports:
- "27017:27017"
deploy:
placement:
constraints:
- engine.labels.purpose==database
networks:
swarm-network:
driver: overlay
docker stack deploy app -c docker-compose.yml