Skip to main content

MongoDB Cluster Setup

I need to preface this with the following disclaimer: I have no prior knowledge of MongoDB, everything I have learned thus far has been from the MongoDB website and other blogs (my apologies in advance for not proper referencing, but I will try).


I came into a situation where there was a need to develop and deploy a MongoDB cluster. And since I like learning new things, I decided to accept the project. The cluster consists of the following setup:


  • One Mongos server, also acting a part of the config cluster
  • Three Config servers
  • Two Replica Sets, also configured into one Shard


I'm going to do my best to go through a straight install of the cluster from my notes. I'm not really going to focus on what each item in the MongoDB cluster is, or attempt to explain how things work. These are just detailed notes on how to get a cluster up and running. I encourage you to check out the MongoDB website for information on MongoDB and each component in the cluster.


The biggest requirement was to develop this as close to what a production environment would be like. The platform of choice was Red Hat Linux 6.5 and the installation was using MongoDB Enterprise 2.6, as a service contract would be obtained once the team using this system have finished their proof of concept and made a case to have a production environment that would support the cost of MongoDB Enterprise. The team is currently testing to determine if there is a use case for MongoDB and if they could get the funding needed to pay for it.


Installing MongoDB:


So here it is. First off you need to decide on a range of IP addresses and a naming convention.


Something along the lines of:
  • mongos.example.com - 192.168.1.1
  • config02.example.com - 192.168.1.2
  • config03.example.com - 192.168.1.3
  • rs0-1.example.com - 192.168.1.4
  • rs0-2.example.com - 192.168.1.5
  • rs0-3.example.com - 192.168.1.6
  • rs1-1.examlpe.com - 192.168.1.7
  • rs1-2.examplle.com - 192.168.1.8
  • rs1-3.example.com - 192.168.1.9


For each server in the cluster, you will need to add the MongoDB Enterprise Repository and install MongoDB. Run the following commands:
sudo vi /etc/yum.repos.d/mongodb-enterprise-2.6.repo
[mongodb-enterprise-2.6] 
Repository name=MongoDB Enterprise 2.6  
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/2.6/$basearch/ 
gpgcheck=0 
enabled=1
Save the new repo file and install Mongo DB
yum clean all
yum install mongodb-enterprise
service mongod start <verify the install went correctly
mongo < you should be placed into the mongodb shell
exit < leave the mongodb shell
service mongod stop
Move on to the next server in the cluster and repeat.


Configuring the Replica Sets


Follow these steps to create the replica sets. Make sure you use the appropriate replica set naming convention where it applies. My example will run through creating the first replica set, named "rs0-", you would simply need to change that to "rs1-" for the second replica set.


Edit /etc/hosts to append 127.0.0.1 to add the host name of the server after all the "localhost" entries (Try running first without this next part (editing the 127.0.0.1), if you run into issues, such as I did, then make the changes. You should add the IP and host name of the other replica set servers though). Add the IP and hostname of the other servers in the replica set to the end of the file. Save and close the hosts file.


Create the necessary folders for the replica sets on each node
First node:
mkdir -p /data/mongodb/rs0-0
Second node:
mkdir -p /data/monbodb/rs0-1
Third node:
mkdir -p /data/mongodb/rs0-2
Configure the Replica Sets. Do this on each node in the replica set.
Edit the mongod.conf file
vi /etc/mongod.conf
Ensure the fork value is set to true
fork=true
Adjust the dbpath variable to point to the necessary directory
dbpath=/data/mongodb
Remove the comment in front of the port number
port=27017
Change the value of the replcSet
replSet=rs0
Save and close the file
Start the replication members by issuing the following command:
mongod --config /etc/mongod.conf


Configure Replication and Add Members
Log into the first node in the cluster and become root. Enter the MongoDB shell and configure the replica set.
mongo
rs.initiate()
rs.conf()
rs.add("rs0-2.example.com") < do this for each child host in the replica set
rs.conf() < verify that the servers in the replica set are in the config
exit < leave the mongo shell


Config and Mongos Servers


For this next part, things get slightly confusing. This is because we will be running a Mongos and Config service on one box, mongos.example.com. There is a particular start order in order to get this to work right, or so it seems for me. You will want to start the config service on the two dedicated config servers, config01 & config02. Then you will start the Mongos service, and finally start the config service on the mongos server. So here it goes:


On the Config Servers
Make the metadata directory
mkdir -p /data/meta
Start MongoDB with the appropriate flags
mongod --configsvr --fork --logpath /var/log/config.log --dbpath /data/meta --port 27019


On the Mongos Server
Make sure the mongod process is not running
service mongod stop
Start the query/router service and specify the config servers
mongos --fork --logpath /var/log/mongod.log --configdb mongos.example.local:27019,config02.example.local:27019,config03.example.local:27019


Adding Shards to the Mongos Server
Log on to one of the replica set servers and connect to to the query server
mongo --host mongo.example.com --port 27017
Add the replica sets as shards
sh.addshard("rs0/rs0-1.example.com:27017,rs0-2,example.com:27017,rs0-3.example.com:27017")
sh.addshard(“rs1/rs1-1.example.com:27017,rs0-2,example.com:27017,rs0-3.example.com:27017")
Verify the shard config
sh.status()

At this point you *should* have a functional MongoDB cluster with Sharding capabilities. Use your friend and mine, Google, if you have any issues or questions (that’s what I've been doing, and what I would recommend if you post a question).

Comments

Popular posts from this blog

Using Python for GPG/PGP File Encryption - Part 1

So, this will be the start of a series that will build a python script for GPG/PGP file encryption. In this post, we'll look at installing gnupg for python and using python to setup the keystore, create a private key, exporting the associated public key, and importing a public key. Now everything done here can be done with simple gnupg commands, but learning how to do this with python will help in understanding the script we'll be building to complete file encryption. I will be covering non-python gnupg commands in a future post. Additionally, the folks at the python-gnupg site over at pythonhosted.org have done a really great job at documenting everything (link to their site at the bottom). The stuff I'll be going over will be more of a start-to-finish for anyone that may get lost in the muck of doing stuff with python. Full Disclosure #1: Any key identifier throughout the series of posts is FICTITIOUS and DOES NOT represent any real key, either associated with myself or...

Windows Server 2008: Log on as batch job

From time to time, I have to set up some scheduled tasks that required a dedicated account to run. And when doing so, I'll usually forget that the dedicated account usually isn't given any more permissions than what it needs to complete the task at hand. So, after setting up the task, Windows will usually yell at me and say "The account needs batch job rights". So here's how to grant batch job permissions on your server. Go to your start menu, and start searching for Local Security Policy In the left pane of the MMC that opens up, expand Local Policies, and highlight User Rights Assignment. Now, in the left right pane, locate "Log on as a batch job" and double click it. In the properties window that opens up, add the user or group that needs this permission. I find that if you have multiple service accounts running different tasks on the same server, it's easier to just add a group verses the individual a...

Using Python for GPG/PGP File Encryption - Part 2

Previously we looked at creating keys, importing public and private keys and the overall setup of gnupg with python. This time around, we're going to take a look at file encryption. Overall the file encryption process is fairly general/easy. But it lacks in the area of scaleablilty, ie to start, you'll only be encrypting one file at a time, which can be done outside of Python with ease. The idea of going over everything in Python, is that you can setup a script to encrypt multiple files in a folder (look for that in Part 3). Assumptions; you have python, and python-gnupg installed, and a public key from someone you want to encrypt and send files to imported to your keystore home (see Part 1 for more information here. Let's get started with Python file encryption. Start off by getting into your python shell, and enter the following: >>> import os >>> import gnupg >>> gpg_home = "/path/to/keyfile/.gnupg" >>> gpg = gnupg....