I would like to setup 3 member mongo replica set on docker swarm with mode = global
and stuck in enabling replication.
For e.g. Docker swarm nodes: node1, node2, node3 (all manager). All nodes were created on different VM's.
Next created 1 service (mongo1) and executed in global mode to install 1 mongo container/instance on each node (node1, node2, node3) in swarm cluster.
Now, I am facing problem in initializing replica-set without using IP/hostname of host machine as mongo container port is not exposed/mapped to host port.
Command to intialize replica-set from mongo prompt:
rs.initiate({_id : "rs", members: [{_id: 0, host: "{host1}"},
{_id: 1, host: "{host2}"}, {_id:2, host:"{host3}"}]})
{host1}, {host2}, {host3}
are placeholders.
Is there anyway to initialize 3 member replica set without using host IP address?
This can also be setup by using swarm mode = replicated where 3 services has to be created with 1 replica and use service-name to initialize replica like below:
rs.initiate({_id : "rs", members: [{_id: 0, host: "mongo1"},
{_id: 1, host: "mongo2"}, {_id:2, host:"mongo3"}]})
mono1, mongo2 & mongo3
are service name in replicated mode.
But I would like to do setup in GLOBAL mode only.
Below is the stack file (global mode)
stack-file.yml:
version: "3.2"
services:
mongo1:
image: mongo:latest
volumes:
- mongo_data:/data/db
networks:
- mongo_net
deploy:
endpoint_mode: dnsrr
mode: global
command: mongod --replSet rs
volumes:
mongo_data:
networks:
mongo_net:
external: true
To create network docker network create --driver overlay mongo_net