Highly availible Mongo on Azure

There are many reasons why Mongo DB became a promement database for devs. As microsoft only recently launched DocumentDB as it’s own NoSQL option it’s still early stage and catching on Mongo is still the NoSQL db of choice. You can always gow with MongoLabs or MongoHQ which provide Mongo as a service. However if you recently stumbled on 6000$ of azure credits it’s good to know that these credits are not usable for 3rd party services. So setting up you’re own Mongo cluster might be in your best interest.

I’ll try to walk through the basic points in doing this. It’s generally a pretty straigh forward process so this should be painless. I hope.

You’ll have to start by creating a virtual network in Azure. You’ll want to put your VM’s on this once you do as they’ll need to talk to each other. You can do this by going through the basic steps.

azurevnet

Important note that it’s not possible to add VM’s into an existing VNet. You need to provision them Vnet first.

You’ll be needint at least 2 VM’s to setup high availibility. I went ahead and used Ubuntu VM’s to test this out. Installing mongo is rather easy

  1. sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
  1. echo ‘deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen’ | sudo tee /etc/apt/sources.list.d/mongodb.list
  2. Sudo apt-get update
  3. sudo apt-get install -y mongodb-org

You can check out the official post from mongo which is fairly verbose

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Once you have mongo installed you’ll realize that the Master slave model has moved to a replica set model.

Now we can start to configure. First check the version of mongo you’re running by checking

$ mongod –version

db version v2.6.5

If you’re aon the latest you should open up the mongo conf to get started. Start by opening up /etc/mongo.conf

comment out the bind_ip line so mongo attaches to external interfaces as well. We’ll need this for replication.

Also at the bottom go ahead and add

# in replicated mongo databases, specify the replica set name here

replSet=rep1

This is an arbitary name you will use to name your set.

You need to do this step on all the nodes. Once they’re up you can go ahead and log in on the  mongo running on the node you wish to elect the master.

On the master node open the mongo console by typing mongo. Once you’ve logged in RS command sets are used to manage the replica set. You can do this via

>Rs.initiate() //run this on master only

This will initiate the master set. Now you need to add the your slave server to the replica set. This will ensure the Master copies the content over to the slave server. In my case my slave vm hostname is “sql2

>Rs.add(“sql2”) // run this on master

>Rs.status() // run this on both as it

When you log in on the slave server you can run this command to see the status.

>Rs.conf()

The order of running these commands and where you run them are important as you can end up  with to configuraitons that each think they are the master. You should also now see that when you’re in the console a “Primary” should appear on the active line.

Next up depending on which driver you’re using to speak to mongo. If you were to use Python this would mea handling a failed connection and using the replicaset connection type.

>>> from pymongo import ReplicaSetConnection
>>> c = ReplicaSetConnection(‘localhost:27017’, replicaSet=‘repl0’)
>>> c.test_database
Database(ReplicaSetConnection([u’…’, u’…’]), u’test_database’)
>>> c[‘test_database’]
Database(ReplicaSetConnection([u’…’, u’…’]), u’test_database’)

Alp

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s