Bobcares

Deploy replica set mongodb docker

by | Jan 1, 2023

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 mongo1mongo2, 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 :

  • docker run : Start a container from an imageAdvertisements
  • -p 30001:27017 : Expose port 27017 in our container, as port 30001 on the localhost
  • --name mongo1 : name this container “mongo1”
  • --net my-mongo-cluster : Add this container to the “my-mongo-cluster” network.
  • mongo : the name of the image we are using to spawn this container
  • mongod --replSet my-mongo-set : Run mongod while adding this mongod instance to the replica set named “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 my-mongo-set:PRIMARY> prompt for readability)

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

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF