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

Visio Stencils Pack for Azure and Microsoft Integration (v5.0.0)

First off, I'd like to send a big shout out to Sandro Pereira who's been managing the vision stencils pack. Essentially, I'm re-posting his information here, because I had a really hard time finding the latest Visio pack for Azure, that wasn't a bunch of SVG's. My thought is that if more people re-post, maybe the search engines of the internet will have an easier time propagating the information... Sandero's Blog (the original post around the new stencils): https://blog.sandro-pereira.com/2019/10/18/microsoft-integration-and-azure-stencils-pack-for-visio-new-major-version-available-v5-0-0/ Sandero's GitHub: https://github.com/sandroasp/Microsoft-Integration-and-Azure-Stencils-Pack-for-Visio Microsoft TechNet Download: https://gallery.technet.microsoft.com/Collection-of-Integration-e6a3f4d0 I will say, and maybe it's just because I'm using Visio 2013, each icon has a boarder that needs to be removed when putting it on the page... Likely it

Disable Security Features to Dual Boot OS X - El Capitan

So, I've recently been working on updating/rebuilding my latest little friend (an 11" MacBook Air) to dual boot the latest OS X, El Capitan, and Kali. I'll go over everything in full detail as soon as I can finish the setup, but I wanted to get this out there for anyone else that may stumble upon any issues with setting up rEFInd on an updated mac. To start, while on Yosemite, I downloaded El Capitan from the App Store, and copied the install files to a USB. From there I preformed my upgrade. This might not be a good option for some people, as I'm sure your mac might be your primary computer... For me, that's not a problem. I tend to keep my essentials on USB drives / cloud storage as I tend to need access from different devices (phone, computer, tablet, etc.). After preforming a clean install of El Capitan, I headed over to the rEFInd website, download the program and ran the install.sh script... Now, that appears to work, like it did with pervious versions of

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