Distributed and Sharded TODO APP
July 31, 2024Setting Up the Cluster
So we only have one machine. The cluster will be made possible in this machine using docker containers. If you are not familiar with docker check out the official docker website here docker
Step 1: Creating the Cluster Network
The first step is to create a Docker network that will facilitate communication between the MongoDB containers. Code snipets below are part of a Makefile
network: @echo "Creating the Cluster Network" @docker network create todoCluster || true
Step 2: Starting MongoDB Instances
Next, it's time to summon the MongoDB instances from the depths of Docker! We'll spin up multiple containers and join them to the replica set, creating a powerful ensemble that will make your data feel like royalty.
start_db_instance: network @echo "Starting the MongoDB Instance five mongo replSet instances" @docker run -d --rm -p 27017:27017 --name mongo1 --network todoCluster mongo:5 --replSet rs0 @docker run -d --rm -p 27018:27017 --name mongo2 --network todoCluster mongo:5 --replSet rs0 @docker run -d --rm -p 27019:27017 --name mongo3 --network todoCluster mongo:5 --replSet rs0 @docker run -d --rm -p 27020:27017 --name mongo4 --network todoCluster mongo:5 --replSet rs0 @docker run -d --rm -p 27021:27017 --name mongo5 --network todoCluster mongo:5 --replSet rs0
This start_db_instance target starts five MongoDB instances (containers) named mongo1, mongo2, mongo3, mongo4, and mongo5. Each instance is configured to join the replica set named rs0. We're not playing around here; we're building a full-fledged replica set with all the bells and whistles!