How to deploy Mongodb cluster with replicas on Docker

How to deploy Mongodb cluster with replicas on Docker
Written by Abhishek JalanApril 26, 2020
9 min read
DevOps
27 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 27 VIEWS SHARE
Abhishek Jalan

DevSecOps Engineer

In this blog, you will learn how we can deploy a MongoDB cluster on Docker with 3 replicas.

Let's learn how to deploy a MongoDB cluster on Docker with 3 replicas.

#Centos8 #MongoDB

Here I am going to use a single CentOS8 machine but best practice is to use 3 different machines if one machine is down but still you can connect MongoDB. You can use any platform, only make sure Docker service is installed and running.

  1. Install Docker Service on CentOS
  2. Deploy 3 MongoDB docker containers on same CentOS
  3. Configure replicaSet
  4. Connect the MongoDB cluster from the remote machine.

For Eg: docker-node.example.com 192.168.100.10

Install Docker Service on CentOS

SSH to server

ssh root@192.168.100.10 

Enable Docker CE Repository

By default, docker packages are not available any more on CentOS 8 or RHEL 8 package repositories, so run following dnf command to enable Docker CE package repository.

[root@docker-node ~]# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Install Docker CE using dnf command

Once the docker ce repo has been configured successfully then run following command to verify which version of docker is available for installation

[root@docker-node ~]# dnf list docker-ce
Available Packages
docker-ce.x86_64              3:19.03.5-3.el7                    docker-ce-stable
[root@docker-node ~]#

Now, use beneath dnf command to install the latest version of docker,

[root@docker-node ~]# dnf install docker-ce -y

After the installation of docker, start and enable its service using the following systemctl commands

[root@docker-node ~]# systemctl start docker
[root@docker-node ~]# systemctl enable docker

Run the following command to verify installed docker version

[root@docker-node ~]# docker --version
Docker version 18.06.3-ce, build d7080c1

Deploy 3 MongoDB docker containers on same CentOS

First of all create 3 directories where you will store all your MongoDB data, (if suppose your docker container deleted or you want to migrate to another docker node then easily you can take backup.

Create 3 directories where you will map 3 mongodb containers.

mongodb0

mongodb1

mongodb2

[root@docker-node ~]#mkdir -p /root/docker/mongodb-cluster/mongodb0
[root@docker-node ~]#mkdir -p /root/docker/mongodb-cluster/mongodb1
[root@docker-node ~]#mkdir -p /root/docker/mongodb-cluster/mongodb2

Now create separate docker network for MongoDatabase:

[root@docker-node ~]#docker network create database

Now deploy 3 MongoDB docker containers with volume persistent. In this blog I am exposing port 5000, 5100 & 5200 but if you can change these port number as per your choice, only make sure those ports are not in use.

mongodb0

[root@docker-node ~]#docker run --hostname mongodb0 -d -p 5000:27017 -v /root/docker/mongodb-cluster/mongodb0:/data/db/ --network='database' --name mongodb0 mongo:latest --replSet "rs0"

mongodb1

[root@docker-node ~]#docker run --hostname mongodb1 -d -p 5100:27017 -v /root/docker/mongodb-cluster/mongodb1:/data/db/ --network='database' --name mongodb1 mongo:latest --replSet "rs0"

mongodb2

[root@docker-node ~]#docker run --hostname mongodb2 -d -p 5200:27017 -v /root/docker/mongodb-cluster/mongodb2:/data/db/ --network='database' --name mongodb2 mongo:latest --replSet "rs0"

To verify if the docker containers are running or not:

[root@docker-node ~]#docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                         NAMES
djdj382ek2ne333        mongo:latest             "docker-entrypoint.s…"   1 hours ago        Up 2 mins         0.0.0.0:5000->27017/tcp                      mongodb0
djdvcsncs2ek2ne       mongo:latest             "docker-entrypoint.s…"   1 hours ago        Up 2 mins         0.0.0.0:5100->27017/tcp                      mongodb1
ncskjcnksnc9323        mongo:latest             "docker-entrypoint.s…"   1 hours ago        Up 2 mins         0.0.0.0:5200->27017/tcp                      mongodb2

Now we will configure replica-set

After Docker setup, later connect to all 3 MongoDB containers using mongo shell (If not installed, please install on your local machine).

Open 3 shell terminal and login to all 3 mongodb.

To check current replicaset status

rs.status()

To create replicaSet between 3 mongodb containers, execute the below commands.

rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "192.168.100.10:5000" },
      { _id: 1, host: "192.168.100.10:5100" },
      { _id: 2, host: "192.168.100.10:5200" },
   ]
})

To verify replicaSet configuration

rs.status()

Once replicaSet completed, login with below commands.

#mongo 'mongodb://192.168.100.10:5000,192.168.100.10:5100,192.168.100.10:5200/?replicaSet=rs0'

Its Done :)


If you guys have any issue or doubts please leave your comment and also provide your feedback.
In next blog, I will show you how to backup/restore mongodb database data.

mongodb
replicas
docker
27 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 27 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Abhishek Jalan
DevSecOps Engineer
+21 more
17 Blog Posts
4 Discussion Threads
Trending Categories
93
Software12
DevOps34
Frontend Development13
Backend Development13
Server Administration13
Linux Administration12
Data Center14
Sentry11
Terraform15
Ansible9
Docker13
Penetration Testing12
Kubernetes12
NGINX8
JenkinsX10
Jenkins18
SSL6
Ethical-Hacking10
Python8
NodeJs9
RedHat8
Github11
AngularJs15
Google Cloud Platform (GCP)6
SonarQube9
Amazon Web Service (AWS)2
VMware17
Blockchain6
Snipe-IT7