Skip to content

Instantly share code, notes, and snippets.

@dev-sareno
Created October 6, 2020 06:54
Show Gist options
  • Save dev-sareno/ab22b904b81b6b2202c8f0062d975da0 to your computer and use it in GitHub Desktop.
Save dev-sareno/ab22b904b81b6b2202c8f0062d975da0 to your computer and use it in GitHub Desktop.
Dockerize MongoDB Replica

Dockerize MongoDB Replica

Create a MongoDB Replica set with 3 members on local machine with Docker

On this context

  • MongoDB Replica Set are inspired and based on the official tutorial
  • We assume you already knew how to use Docker
  • We'll use the following DNS hostnames
    www.r0.mongodb.my
    www.r1.mongodb.my
    www.r2.mongodb.my
    

Requirements

Add DNS hostnames to hosts file

  1. Open Notepad (or any text editor) as Administrator
  2. Open the file C:\Windows\System32\drivers\etc\hosts
  3. Append the following to hosts file
    127.0.0.1 www.r0.mongodb.my
    127.0.0.1 www.r1.mongodb.my
    127.0.0.1 www.r2.mongodb.my
    
  4. Save

Now, all calls to address www.r0.mongodb.my, www.r1.mongodb.my, and www.r2.mongodb.my will be mapped to machine's localhost.

Docker container port mapping

We'll name our Docker containers as follow

  1. mongo-www-r0-mongodb-my
  2. mongo-www-r1-mongodb-my
  3. mongo-www-r2-mongodb-my

Then we'll map the container ports to the host machine ports as follow

  1. mongo-www-r0-mongodb-my:27017 -> 28015
  2. mongo-www-r1-mongodb-my:27017 -> 28016
  3. mongo-www-r2-mongodb-my:27017 -> 28017

Create a Docker MongoDB container

mongo-www-r0-mongodb-my

$ docker exec -ti $(docker run -ti -d -p 28015:27017 --name mongo-www-r0-mongodb-my mongo:latest bash) bash  # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log  # start mongo server with configuration
root@xxxxxx:~# exit  # exit ssh

--replSet "myReplica" - Configure replica with name of myReplica --bind_ip_all - Allow all ipv4 client connections --fork --logpath /var/log/mongod.log - Start mongodb process in background with specified path for logs

Do the same commands for other containers

mongo-www-r1-mongodb-my

$ docker exec -ti $(docker run -ti -d -p 28016:27017  --name mongo-www-r1-mongodb-my mongo:latest bash) bash  # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log  # start mongo server with configuration
root@xxxxxx:~# exit  # exit ssh

mongo-www-r2-mongodb-my

$ docker exec -ti $(docker run -ti -d -p 28017:27017  --name mongo-www-r2-mongodb-my mongo:latest bash) bash  # create and ssh to container
root@xxxxxx:~# mongod --replSet "myReplica" --bind_ip_all --fork --logpath /var/log/mongod.log  # start mongo server with configuration
root@xxxxxx:~# exit  # exit ssh

Connect to MongoDB primary cluster

For this, we'll use www.r0.mongodb.my (mongo-www-r1-mongodb-my) as the primary cluster

$ mongo --host www.r0.mongodb.my --port 28015
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:8513/?compressors=disabled&gssapiServiceName=mongodb
...

Configure Replica Set Note: It's recommended to read more from the official documentation

> rs.initiate( {
   _id : "myReplica",
   members: [
      { _id: 0, host: "www.r0.mongodb.my:28015" },
      { _id: 1, host: "www.r1.mongodb.my:28016" },
      { _id: 2, host: "www.r2.mongodb.my:28017" }
   ]
})

MongoDB Replica Set connection string

Note: For more details, visit the official documentation for Connection Strings.

Our connection string would be mongodb://www.r0.mongodb.my:28015,www.r1.mongodb.my:28016,www.r2.mongodb.my:28017/?replicaSet=myReplica

Connect Replica Set

On the host machine's terminal (i.e. Command Prompt, PowerShell), run the following command

$ mongo "mongodb://www.r0.mongodb.my:28015,www.r1.mongodb.my:28016,www.r2.mongodb.my:28017/?replicaSet=myReplica"

Conclusion

Now, try to write any documents to the database and check if the inserted data reflected to all secondary clusters.

Task

  • Try to takedown/disable the primary cluster and then check if the new primary cluster is elected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment