Wonder how to deploy replica set mongodb docker? Our experts have put together this guide to help you get started. Our Docker Support team is here to lend a hand with your queries and issues.
Deploy replica set mongodb docker
Basically, we are going to have three containers, which are all inside their own Docker container network.
Let’s name them
mongo1
, mongo2
, and mongo3
. These are three Mongo instances of our replica set.
We are also going to expose each of them to our local machine, so that we can access them using the Mongo shell interface from our local machine.
Each of the three Mongo containers should be able to communicate with all other containers in the network.Advertisements
Creating a New Network
To see all networks currently on your system, run the command
$ docker network ls NETWORK ID NAME DRIVER SCOPE 2a4e341c6039 bridge bridge local 4fbef5286425 host host local 8062e4e7cdca none null local
We will be adding a new network called
my-mongo-cluster
:
$ docker network create my-mongo-cluster
The new network should now be added to your list of networks :
$ docker network ls NETWORK ID NAME DRIVER SCOPE 2a4e341c6039 bridge bridge local 4fbef5286425 host host local f65e93c94e42 mongo-cluster bridge local 8062e4e7cdca none null local
Setting up Our Containers
To start up our first container,
mongo1
run the command:
$ docker run \ -p 30001:27017 \ --name mongo1 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-set
Let’s see what each part of this command does :
: Start a container from an imageAdvertisementsdocker run
: Expose port 27017 in our container, as port 30001 on the localhost-p 30001:27017
: name this container “mongo1”--name mongo1
: Add this container to the “my-mongo-cluster” network.--net my-mongo-cluster
: the name of the image we are using to spawn this containermongo
: Run mongod while adding this mongod instance to the replica set named “my-mongo-set”mongod --replSet my-mongo-set
Set up the other 2 containers by running :
$ docker run \ -p 30002:27017 \ --name mongo2 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-set $ docker run \ -p 30003:27017 \ --name mongo3 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-set
Configuring Database Replication
Now that we have all our Mongo instances up and running, let’s add them to a replica set.
Connect to the Mongo shell in any of the containers.
docker exec -it mongo1 mongo
This command will open up the Mongo shell in our running
mongo1
container (but you can also run it from the mongo2
or mongo3
container as well).
Inside the Mongo shell, we first create our configuration :
MongoDB shell version v5.0.8 > db = (new Mongo('localhost:27017')).getDB('test') test > config = { "_id" : "my-mongo-set", "members" : [ { "_id" : 0, "host" : "mongo1:27017" }, { "_id" : 1, "host" : "mongo2:27017" }, { "_id" : 2, "host" : "mongo3:27017" } ] }
The first
_id
key in the config, should be the same as the --replSet
flag which was set for our mongod instances, which is my-mongo-set
in our case. We then list all the members
we want in our replica set.
Since we added all our Mongo instances to our docker network. Their name in each container resolver to their respective IP addresses in the
my-mongo-cluster
network.
Finally, start the replica set by running the following command in the Mongo shell:
> rs.initiate(config) { "ok" : 1 }
If the command succeeds, your prompt should change to reflect that the current database is part of a replice set:
my-mongo-set:PRIMARY>
This means that the shell is currently associated with the
PRIMARY
database in our my-mongo-set
cluster.
Let’s play around with our new replica set to make sure it works as intended. (I am omitting the
prompt for readability)my-mongo-set:PRIMARY>
First, let’s insert a document into our primary database :
> db.mycollection.insert({name : 'sample'}) WriteResult({ "nInserted" : 1 }) > db.mycollection.find() { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "sample" }
We then make a new connection to one of our secondary databases (located on
mongo2
) and check if our document has replicate:
> db2 = (new Mongo('mongo2:27017')).getDB('test') test > db2.setSecondaryOk() > db2.mycollection.find() { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "sample" }
We run the
db2.setSecondaryOk()
command to let the shell know that we are intentionally querying a database that is not our primary.
Conclusion
To conclude, our Support Engineers demonstrated how to deploy replica set mongodb docker
0 Comments