Skip to content

Instantly share code, notes, and snippets.

@gayanvirajith
Last active January 8, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gayanvirajith/c3c94db347488debd0a11b952359ea2c to your computer and use it in GitHub Desktop.
Save gayanvirajith/c3c94db347488debd0a11b952359ea2c to your computer and use it in GitHub Desktop.
MongoDB replica set with docker and docker-compose

How to setup

  • Update mongod.env file with custom username and password
  • Run docker compose up -d
  • log on to mongo cli using below command
docker exec -it {docker-container-id} mongo -u "{your-username}" -p "{your-password}" --authenticationDatabase "admin"
  • configure (initialize replicaset) on mongo-cli
rs.initiate({
    "_id" : "rs0",
    "members" : [
        {"_id" : 0,"host" : "mongodb1:27017"},
        {"_id" : 1,"host" : "mongodb2:27017"},
        {"_id" : 2,"host" : "mongodb3:27017"}
    ]
});
  • option step to setup primority primary node (You may need to restart docker-compose)
conf = rs.config();
conf.members[0].priority = 2;
rs.reconfig(conf);

Optional step to create db user

use admin;
db.createUser({user: "cluster_admin",pwd: "password",roles: [ { role: "userAdminAnyDatabase", db: "admin" },  { "role" : "clusterAdmin", "db" : "admin" } ]});
db.auth("cluster_admin", "password"); 

References & Credis:

https://github.com/willitscale/learning-docker/tree/master/tutorial-12

https://www.youtube.com/watch?v=-XzMfd4XQak&t=1106s&ab_channel=WillitScale

version: "3.0"
volumes:
mongo-keys:
mongo-data-1:
mongo-data-2:
mongo-data-3:
networks:
rs0:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.10.5.0/24
services:
mongo-keys:
image: depop/openssl-bats
volumes:
- mongo-keys:/mongo-conf
command: 'bash -c "openssl rand -base64 741 > /mongo-conf/mongodb-keyfile; chmod 600 /mongo-conf/mongodb-keyfile; chown 999 /mongo-conf/mongodb-keyfile"'
# Primary
mongodb1:
image: mongo:4.0.4
restart: always
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
volumes:
- mongo-keys:/opt/keyfile
- mongo-data-1:/data/db
env_file:
./mongod.env
ports:
- 27017:27017
command: 'mongod --smallfiles --maxConns 51200 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet rs0'
depends_on:
- mongo-keys
networks:
rs0:
# Worker 1
mongodb2:
image: mongo:4.0.4
restart: always
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
volumes:
- mongo-keys:/opt/keyfile
- mongo-data-2:/data/db
env_file:
./mongod.env
ports:
- 27018:27017
command: 'mongod --smallfiles --maxConns 51200 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet rs0'
depends_on:
- mongo-keys
networks:
rs0:
# Worker 2
mongodb3:
image: mongo:4.0.4
restart: always
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
volumes:
- mongo-keys:/opt/keyfile
- mongo-data-3:/data/db
env_file:
./mongod.env
ports:
- 27019:27017
command: 'mongod --smallfiles --maxConns 51200 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet rs0'
depends_on:
- mongo-keys
networks:
rs0:
MONGO_INITDB_ROOT_USERNAME=YourUser
MONGO_INITDB_ROOT_PASSWORD=YourPassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment