4

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

nitesh
  • 41
  • 2
  • Hi nitesh, and welcome to Stack Overflow. Can I ask you to clarify something about your setup: if the container ports are not mapped to host ports, then how are the mongod processes expected to be able to communicate with each other? – Vince Bowdren Mar 22 '18 at 15:44
  • 1
    If containers are deployed in swarm cluster then there is no need to map/expose ports to host node as containers inside swarm can communicate to each other using container id – nitesh Mar 25 '18 at 06:20

0 Answers0