Skip to main content

MongoDB read replica setup with Ec2 instances

MongoDB setup  with ec2 instances, odd no of ec2 instances should be used which helps in polling when the instances are down. Backup and restore will be easily managed when ebs storage volume is used and mongodb is setup on a independent instance.


Let's say we decide to go with 1 primary and 2 secondary instance as read replicas.
Steps:
--Install mongodb on all the instances or install on one instance and launch similar instances using aws console.
--Attach ebs volumes for db,log and journal files in each of the instance. Change the path to these folders in /etc/mongodb.conf. Make sure mongod has read and write permission on these folders.

Now restart mongodb server on each of the instance to make sure changes done are working fine.
$sudo /etc/init.d/mongodb restart

Replication Steps:
Copy this line at the bottom of  mongodb.conf file,  replSet = rs0, and specify the same replica set name on same instance.

Now, start each mongod instance

        mongod --config /etc/mongodb.conf

Open mongo shell in any of the instance by hitting $mongo in the terminal and execute the commands below.

$ rs.initiate()   -- Initiates the replica set
$ rs.conf()        -- Displays the current replica set configuration

Add the other members to the replica,
rs.add("ec2-address:27017")  --secondary


For more basic steps, Refer Deploy a Replica Set- MongoDB 


When Mongodb setup on ec2 instances setup on primary and secondary with read replicas.

If any of the replica stops working,

cfg = rs.conf()

{
"_id" : "rs0",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "ip-10-128-149-4:27017"
},
{
"_id" : 1,
"host" : "ec2-54-251-238-211.ap-southeast-1.compute.amazonaws.com:27017"
}
]
}


rs0:SECONDARY> cfg.members=[cfg.members[0]]
[ { "_id" : 0, "host" : "ip-10-128-149-4:27017" } ]
rs0:SECONDARY> rs.reconfig(cfg, {force : true})
{ "ok" : 1 }
rs0:SECONDARY> rs.status()

{
"set" : "rs0",
"date" : ISODate("2013-11-13T21:04:24Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ip-10-128-149-4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 15772,
"optime" : Timestamp(1384376658, 1),
"optimeDate" : ISODate("2013-11-13T21:04:18Z"),
"self" : true
}
],
"ok" : 1
}
rs0:PRIMARY> rs.add("ec2-54-254-66-31.ap-southeast-1.compute.amazonaws.com:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.status

Comments

Popular posts from this blog

Understanding TOP command and purpose

$top top - 12:24:34 up 9 days, 21:58, 0 users, load average: 5.98, 5.32, 4.30 Tasks: 13 total, 1 running, 12 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.5 us, 1.5 sy, 0.0 ni, 92.6 id, 0.0 wa, 0.0 hi, 0.5 si, 0.0 st KiB Mem: 12969522+total, 11112360+used, 18571628 free, 135900 buffers KiB Swap: 0 total, 0 used, 0 free. 49328208 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 40 root 20 0 1466540 912540 12568 S 7.7 0.7 67:03.03 bundle 43 root 20 0 1413152 860252 11116 S 6.7 0.7 65:41.24 bundle The load averages indicate the average number of processes waiting for CPU time over the specified time periods. Shows running processes and their status. Buffer is the amount of data used while it's being written or read. The numbers are in KiB's showing the RAM available on system us - user process sy - system process process ID (PID), user, priority (PR), virtual memory usage (VIRT), resident memory usage (RES), shared memory usage (SHR), CPU usage (%...

RubyConf 2013 at pune..retrieved from drafts

A great experience while interacting with the ruby community from different places. Lots of knowledge and inspiration flows. Met many folks from india and abroad.