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...

Keto Kickin' Choffle

Alight, first recipe on the blog... If you've had a chance to reach the "About" section, you will have seen that, rather than spinning up multiple blogs, I'm just going to combine my passions for tech, food, health, and music into this one space. If you're not a fan, then too bad. I really just don't want to manage more than one blog.... With that, here goes nothing... So I recently came across the choffle. This is a really great, and fast to make replacement for buns. I'm also a huge fan of spicy foods. Well, I got the great idea to combine the heat with the choffle. And let me tell you, it turned out really good. I like to use this for a bun replacement when I'm in the mood for that extra kick, but not the extra toppings. Why? Because the extra toppings are now combined in the choffle. Thus removing the need for the additional toppings on my burgers. Enjoy. Kickin' choffle The choffle mix with a kick! Author: Larry L. Preparation Time: 3...

Replacing rsyslog with syslog-ng on RHEL 6.5

So...I had a piece of monitoring software that didn't play nice with the RHEL default rsyslog for log collection. The software was developed to only work with syslog-ng. I'll be going over the steps that I took, that worked for me, in replacing rsyslog with syslog-ng. I would imagine that these same steps should work for any Linux system similar to RHEL (Fedora, CentOS, etc.). For others (like Debein based distributions), I would need to look into that (coming in a future update to this post). First, remove rsyslog. You will need to keep the dependencies as they will be needed for syslog-ng: sudo rpm -e --nodeps rsyslog Next we will need to add the EPEL repository (more info can be found HERE ): wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm sudo rpm -ivh epel-release-6-8.rpm sudo yum repolist That last command will list all the installed repositories. You are simply verifying that the EPEL package has been installed. Now that we fi...