One of the main advantages of MongoDB is the high-availability, redundancy and strict consistency, which is implemented with Replicasets - group of Mongo database which runs simultaneously and contains the same data. The idea is that once one of the sets fails,  another one will take his job to response, even it was the primary set. It's totally automatically failure-safe and one of the basis of production deployment. In this article we will explain how to deploy such an environment including architecture.

Requirements and assumptions

We took some assumptions. First is that you’ll be installing on Centos 6.4+. The second is that you’ll have internet access from the servers you're setting up.

We’ll go bottom up on this one as some components require other components to be active.

The configuration will be a simple configuration of a primary and secondary DB’s and an arbiter to provide election majority. Anything bigger is basically just a duplication of the same operations.

Installing the secondary replica (sec.db1.example.com).

We will start with the secondary because it's the simplest one.

Install Mongo

Let's use MongoDB official repositories for that:

echo "[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1" | sudo tee /etc/yum.repos.d/10gen.repo

We just install mongo and configure it:

sudo yum install mongo-10gen mongo-10gen-server

Create and setup the key file 

openssl rand -base64 741 > mongo.key
sudo cp mongo.key /etc/
echo "\n\n#Config server configuration\nkeyfile=/etc/mongo.key" | sudo tee -a /etc/mongod.conf
sudo chown mongod:mongod /etc/mongo.key
sudo chmod 600 /etc/mongo.key

The first command creates a new randomized data file under ./mongo.key. We then copy it to /etc/ and add the configuration option that points to it, “keyfile=/etc/mongo.key”, to the configuration file “/etc/mongo.conf”. The last two commands changed the permission and ownership of the key file so that only mongo can use it.

Set the Replication group configuration 

echo $'#Replication Settings\nreplSet=rs0\n' | sudo tee -a /etc/mongod.conf

This will configure mongo instance to be on replica set named ‘rs0’ (replSet=rs0). You can give it any name you like as long as it is consistent across your mongodb instances that are in the same replica set.

Start Mongo

service mongod start
chkconfig mongod on

Installing the Arbiter (arb.db1.example.com) 

If your replica set consists of an even number of database instances (as in this example) you’ll need an arbiter to give the majority vote in case of an elections. 

Luckily arbiters don't consume much resources so you can use weak/small machines for one like this or a multiple instances on a little stronger machine. I'll show both of the methods.

For this exercise we’ll use the first case and use a single instance per machine.

Both of the approaches share these initial steps of installing MongoDB and setup Key.

Install Mongo

echo "[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1" | sudo tee /etc/yum.repos.d/10gen.repo
sudo yum install mongo-10gen mongo-10gen-server

Create and setup the key file  

sudo scp user@sec.db#.example.com:~/mongo.key /etc/
echo "\n\n#Config server configuration\keyfile=/etc/mongo.key" | sudo tee -a /etc/mongod.conf
sudo chown mongod:mongod /etc/mongo.key
sudo chmod 600 /etc/mongo.key

For a single instance per machine setup process

echo $'#Replication Settings\nreplSet=rs0\n' | sudo tee -a /etc/mongod.conf
service mongod start
chkconfig mongod on

Or for multiple instances of an arbiter on a single machine

Stop  the standard mongo instance (if needed...):

service mongod stop 

Set up a new arbiter:

sudo mkdir /data/arb0
sudo chown mongod:mongod /data/arb0
echo 'sudo -u mongod nohup mongod --port 30000 --dbpath /data/arb0 --replSet rs0 --keyFile /etc/mongo.key &' | sudo tee -a /etc/rc.local

Notice that for each instance you’ll need another DB directory “/data/arb#” (there won’t be a lot of data in there but you’ll need it) and of course a different port number.

Installing the Primary Replica Server (pri.db1.example.com)

Install Mongo

echo "[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1" | sudo tee /etc/yum.repos.d/10gen.repo
sudo yum install mongo-10gen mongo-10gen-server

Set the  Replication group configuration 

echo $'#Replication Settings\nreplSet=rs0\n'  | sudo tee -a  /etc/mongod.conf

Setup the key file

sudo scp user@sec.db.example.com:~/mongo.key /etc/
echo "\n\n#Config server configuration\keyfile=/etc/mongo.key" | sudo tee -a /etc/mongod.conf
sudo chown mongod:mongod /etc/mongo.key
sudo chmod 600 /etc/mongo.key

Start the mongodb server

service mongod start
chkconfig mongod on

Setup and configure the replication

mongo 
rs.initiate();
// Add the secondary replica instance :
rs.add("sec.db.example.com");
# Add the Arbiter instance :
rs.addArb('arb.db.example.com');
//Then just check everything is ok with :
rs.conf();

And that is it. Your replica set should be up and running...
Just configure your client to use it. For example:

MongoClient("mongodb://pri.db1.example.com:27017,sec.db1.example.com:27017", array("replicaSet" => "rs0"))

And you should be fine.