Replica Deployment in Docker on different Machine with Ubuntu

Hi Team, I am running MongoDb in two different Machine inside Docker on Ubuntu Desktop in Local Area Network, I want to make replica of both the Machine MongoDb database. Can someone guide me or share some resources to read and perform the same?

Setting up a MongoDB replica set with two different machines inside Docker on a local area network involves several steps. Below is a guide to help you set this up:

Prerequisites:

  1. Two Ubuntu Desktop machines with Docker installed.
  2. MongoDB Docker containers running on both machines.
  3. Both machines should be able to communicate with each other over the network.

Step 1: Launch MongoDB Containers

On both machines, start your MongoDB Docker containers with replica set configurations:

Machine 1:

bash

Copy code

docker run -d --name mongo1 -p 27017:27017 -v ~/mongo1:/data/db mongo --replSet "rs0"

Machine 2:

bash

Copy code

docker run -d --name mongo2 -p 27018:27017 -v ~/mongo2:/data/db mongo --replSet "rs0"

Here:

  • --replSet "rs0" sets the replica set name as rs0.
  • You can change ~/mongo1 and ~/mongo2 to appropriate directories on your machines.

Step 2: Get the IP Addresses of Both Machines

Identify the IP addresses of both machines:

Machine 1:

bash

Copy code

hostname -I

Machine 2:

bash

Copy code

hostname -I

Step 3: Connect to the MongoDB Shell

Access the MongoDB shell in the primary MongoDB container (Machine 1):

bash

Copy code

docker exec -it mongo1 mongo

Step 4: Initiate the Replica Set

In the MongoDB shell of Machine 1, initialize the replica set with the following command:

javascript

Copy code

rs.initiate({
   _id: "rs0",
   members: [
      { _id: 0, host: "IP_ADDRESS_MACHINE_1:27017" },
      { _id: 1, host: "IP_ADDRESS_MACHINE_2:27018" }
   ]
})

Replace IP_ADDRESS_MACHINE_1 and IP_ADDRESS_MACHINE_2 with the IP addresses obtained earlier.

Step 5: Check Replica Set Status

After initiating the replica set, you can check the status by running:

javascript

Copy code

rs.status()

This command will show the status of your replica set, including which nodes are primary and secondary.

Step 6: Test the Replica Set

To test that the replica set is working correctly, insert data into the primary MongoDB instance and check that it replicates to the secondary.

Step 7: Optional - Add a Third Node (Arbiter)

You can add a third node as an arbiter to avoid split-brain scenarios:

  1. Launch a third MongoDB container on either machine:

bash

Copy code

docker run -d --name mongo3 -p 27019:27017 -v ~/mongo3:/data/db mongo --replSet "rs0"
  1. Add the arbiter to the replica set in the MongoDB shell:

javascript

Copy code

rs.addArb("IP_ADDRESS_MACHINE_1_OR_2:27019")

Additional Resources

This guide should get you started on setting up your MongoDB replica set on two different machines in a local area network. Let me know if you run into any issues! Setting up a MongoDB replica set with two different machines inside Docker on a local area network involves several steps. Below is a guide to help you set this up:

Prerequisites:

  1. Two Ubuntu Desktop machines with Docker installed.
  2. MongoDB Docker containers running on both machines.
  3. Both machines should be able to communicate with each other over the network.

Step 1: Launch MongoDB Containers

On both machines, start your MongoDB Docker containers with replica set configurations:

Machine 1:

bash

Copy code

docker run -d --name mongo1 -p 27017:27017 -v ~/mongo1:/data/db mongo --replSet "rs0"

Machine 2:

bash

Copy code

docker run -d --name mongo2 -p 27018:27017 -v ~/mongo2:/data/db mongo --replSet "rs0"

Here:

  • --replSet "rs0" sets the replica set name as rs0.
  • You can change ~/mongo1 and ~/mongo2 to appropriate directories on your machines.

Step 2: Get the IP Addresses of Both Machines

Identify the IP addresses of both machines:

Machine 1:

bash

Copy code

hostname -I

Machine 2:

bash

Copy code

hostname -I

Step 3: Connect to the MongoDB Shell

Access the MongoDB shell in the primary MongoDB container (Machine 1):

bash

Copy code

docker exec -it mongo1 mongo

Step 4: Initiate the Replica Set

In the MongoDB shell of Machine 1, initialize the replica set with the following command:

javascript

Copy code

rs.initiate({
   _id: "rs0",
   members: [
      { _id: 0, host: "IP_ADDRESS_MACHINE_1:27017" },
      { _id: 1, host: "IP_ADDRESS_MACHINE_2:27018" }
   ]
})

Replace IP_ADDRESS_MACHINE_1 and IP_ADDRESS_MACHINE_2 with the IP addresses obtained earlier.

Step 5: Check Replica Set Status

After initiating the replica set, you can check the status by running:

javascript

Copy code

rs.status()

This command will show the status of your replica set, including which nodes are primary and secondary.

Step 6: Test the Replica Set

To test that the replica set is working correctly, insert data into the primary MongoDB instance and check that it replicates to the secondary.

Step 7: Optional - Add a Third Node (Arbiter)

You can add a third node as an arbiter to avoid split-brain scenarios:

  1. Launch a third MongoDB container on either machine:

bash

Copy code

docker run -d --name mongo3 -p 27019:27017 -v ~/mongo3:/data/db mongo --replSet "rs0"
  1. Add the arbiter to the replica set in the MongoDB shell:

javascript

Copy code

rs.addArb("IP_ADDRESS_MACHINE_1_OR_2:27019")

Additional Resources

This guide should get you started on setting up your MongoDB replica set on two different machines in a local area network. Let me know if you run into any issues!