0% found this document useful (0 votes)
42 views3 pages

MongoDB Tutorial - How To Deploy A MongoDB Replica Set or Cluster Using Docker

This document provides instructions for deploying a MongoDB replica set using Docker. It describes creating a Docker network, launching three MongoDB containers connected to that network, configuring the replica set within one container, and verifying the setup. The replica set ensures data is backed up across nodes, with one primary and secondary nodes ready to take over if needed.

Uploaded by

janakdpatel09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

MongoDB Tutorial - How To Deploy A MongoDB Replica Set or Cluster Using Docker

This document provides instructions for deploying a MongoDB replica set using Docker. It describes creating a Docker network, launching three MongoDB containers connected to that network, configuring the replica set within one container, and verifying the setup. The replica set ensures data is backed up across nodes, with one primary and secondary nodes ready to take over if needed.

Uploaded by

janakdpatel09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MongoDB Tutorial: How to Deploy a

MongoDB Replica Set or Cluster


Using Docker
How to deploy a mongodb replica set using Docker
Share

Watch on

Introduction
In this tutorial, we will learn how to deploy a MongoDB replica set or cluster using Docker. MongoDB replica set
ensures that your data is backed up and available in case of hardware or software failure. To create a replica set, you
need at least three nodes, with one acting as the primary node. The primary node handles all the reads and writes,
while the secondary nodes act as backups or standby nodes.

Creating a Docker Network


The first step is to create a separate Docker network. This network will be used to connect the containers. You can
create the network using the following command:

sudo docker network create net

Verify that the network has been created by running the command:

docker network ls

You should see the network "mango_net" in the list.

Creating the Containers


Next, we need to create three containers and attach them to the network we just created. Use the following command
to create the first container:

docker run -d -p 130001:27017 --network=net --name m1 mongo:4.0.4 --replSet set

Here, we are mapping the internal port 27017 of the container to the external port 130001. The container is attached
to the network "net" and given the name "m1". We are using the image version 4.0.4 and specifying the replica set
name as "set".

Create the second and third containers using the following commands:
docker run -d -p 130002:27017 --network=net --name m2 mongo:4.0.4 --replSet set
docker run -d -p 130003:27017 --network=net --name m3 mongo:4.0.4 --replSet set

Make sure to change the external port and container name for each command.

To verify that the containers are running, use the command:

docker ps

You should see all three containers listed.

Configuring the Replica Set


Now, we need to configure the replica set. Log in to the first container (m1) using the following command:

docker exec -it m1 mongo

Inside the container, create a variable named "config" and set it to the configuration of the replica set. The
configuration should specify the replica set name and the members (nodes) of the replica set. For example:

config = {
_id: "set",
members: [
{ _id: 0, host: "m1:27017" },
{ _id: 1, host: "m2:27017" },
{ _id: 2, host: "m3:27017" }
]
}

Initialize the replica set using the following command:

rs.initiate(config)

This command will initialize the replica set using the configuration specified in the "config" variable.

Verifying the Replica Set


To verify that the replica set has been set up correctly, you can use the following command:

rs.status()

This command will display detailed information about the replica set, including the state of each node. The primary
node will have the state "PRIMARY", while the secondary nodes will have the state "SECONDARY".

Now, let's simulate a failover by stopping the primary node (m1). After stopping the node, one of the secondary
nodes will become the new primary. To stop the node, use the following command:

docker stop m1
After stopping the node, log in to one of the secondary nodes (e.g., m2) and check the status of the replica set using
the command "rs.status()". You should see that the secondary node has become the new primary.

By setting up a replica set with multiple nodes, you can ensure that your data remains accessible even in case of
hardware or software failures. The secondary nodes act as backups or standby nodes, ready to take over as the
primary node if necessary.

Conclusion
In this tutorial, we have learned how to deploy a MongoDB replica set or cluster using Docker. Replica sets provide
data backup and availability in case of failures. By following the steps outlined in this tutorial, you can easily set up
and configure a MongoDB replica set using Docker.

Thank you for reading!

Made with VideoToBlog

You might also like