How to Configure MongoDB HA Replica Set on AWS EC2

23rd Jan 2013
  • +0
Share on
  • +0
Share on
Share on

It has been always tedious task for choosing right configuration for MongoDB on AWS EC2. Choosing right configuration in this environment is always a challenging and it takes lots of time to make your system Production Ready.You can use following configuration and steps to install MongoDB in EC2 environment for creating Production Ready HA replica set.

All it needs is two machines that will be used as PRIMARY(Master) and SECONDARY (Slave) node and one ARBITER machine for the replica set. However it might get changed based on your application requirement and you can opt for higher number of nodes based on your need. ARBITER is only required in case of even number replica set. If you want to maintain replica set with one PRIMARY and two SECONDARY, ARBITER is not required.

Hardware Requirement

  1. Two 64 bit EC2 instances of medium/large or higher configuration based on your app requirement for PRIMARY and SECONDARY node. There is a data storage limitation of using 32 bit machine and can only support up-to 2.5 GB of storage.
  2. A small 32 bit EC2 machine for MongoDB ARBITER.
  3. It is recommended to have machines in different availability zone to make it High available in-case shutdown of one availability zone.
  4. Use Ext4 EBS volume to support I/O suspend and write-cache flushing for multi-disk consistent snapshots.

Installation steps

  1. Create and Launch an EC2 instance of required configuration as stated above for PRIMARY, SECONDARY and ARBITER nodes.
  2. Create an EBS volume of required size to be used for MongoDB storage for both nodes.
  3. Connect to EC2 instances on PRIMARY and SECONDARY node via SSH
  4. Make a Ext4 file system on both node via sudo mkfs -t ext4 /dev/<Created_EBS_Volume>
  5. Create directory /data/db or any other of your own choice and mount it to attached volume using sudo mount -a /dev/<Created_EBS_Volume> /data/db
  6. Edit your /etc/fstab to enumerate it on start up of instance using sudo echo ‘/dev/sdf /data/db auto noatime,noexec,nodiratime 0 0’ >> /etc/fstab
  7. Download and Install MongoDB on all instances.
  8. Start the PRIMARY node with following command in MongoDB directory using mongod --rest --replSet myHASet (Where myHASet is the name of Replica set, you can choose any name of your choice)
  9. Go to Mongo treminal in MongoDB directory.
  10. Initialize the set using command rs.initiate() on mongo terminal
  11. Check the status of Replicat set after initialization using rs.status() command.
  12. If initialization is success you will see OK in the output something like this{"set" : "sample",
  13. "myState" : 1,
  14. "members" : [
  15. {
  16. "name" : "<PRIMARY_HOSTNAME>:27017",
  17. "self" : true
  18. }
  19. ],
  20. "ok" : 1
  21. }
  22. You can also check the status on http://<PRIMARY_NODE>:27017/_replSet
  23. Your Primary node is ready to use now. You can insert/update document on this node.
  24. Now start SECONDARY node with same command as on primary mongod --rest --replSet myHASet
  25. Now tell the PRIMARY node to add SECONDARY node in replica set. Go to mongo console on PRIMARY node and add this using rs.add(“<SECONDARY_HOSTNAME>”);
  26. If addition is successful you will see the response { "ok" : 1 }
  27. Once your SECONDARY node is attached to replica set you can check the status on http://<PRIMARY_NODE>:27017/_replSet
  28. Now start the ARBITER node using mongod --rest --replSet myset --oplogSize 8
  29. Add the ARBITER node in replica set using command rs.add( { _id:2, host:”<ARBITER_HOSTNAME>”, arbiterOnly:true } )
  30. Once ARBITER is added successfully, you are done with the configuration and your replica set is ready to use.
  31. Got o http://<PRIMARY_NODE>:27017/_replSet and you should be able to see the status of each node as seen in below scree shot.

  1. To test the replica, take down the primary node, and see if SECONDARY is able to pick up and will become PRIMARY node.
  2. You can fire the command db.isMaster() to check the status if SECONDARY node has turned up as Master node.
  3. You can additionally use horizontal sharding using shard cluster for scaling large volume of app data. Configuring shard cluster is not in the scope of this article.

Connecting Replica Set from JAVA API

After you have setup the replica set successfully, you can connect with using JAVA driver from your client application.

You can use following code snippet for making connection to replica set

List addrs = new ArrayList();addrs.add( new ServerAddress( "&lt;PRIMARY_HOST&gt;", "&lt;MONGO_PORT&gt;" ) );addrs.add( new ServerAddress("&lt; SECONDARY_HOST&gt;" , "&lt;MONGO_PORT&gt;"));Mongo m = new Mongo(addrs);DB db = m.getDB("&lt;NAME_OF_DB&gt;");

MongoDB driver is smart enough to connect to PRIMARY node only, in-case if PRIMARY node is down, it will automatically switch to another node for communication.

Here is an honest attempt to guide you to setup MongoDB on AWS EC2. Though this is an open forum and you all are open to post your comments if I have missed anything.

Also, if you don’t want to get into setting up the infrastructure and administration for MongoDB, you can directly use our App42 NoSQL Cloud Storage Service. This service can be accessed using our REST API or using native platform SDKs available in different languages like iOS, Android,J2ME, JAVA, PHP, Ruby, Windows Phone and C#.

About the Author:

This article is written by Ajay Tiwari, Product Architect at ShepHertz Technologies

How has the coronavirus outbreak disrupted your life? And how are you dealing with it? Write to us or send us a video with subject line 'Coronavirus Disruption' to

  • +0
Share on
  • +0
Share on
Share on