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

Getting Samsung Dex Configured to Work with Azure DevOps Repos

Recently, I upgraded my phone to the Samsung Galaxy Note 10+... I'm a big fan of big phones (a perk to being a big guy). I've always been intrigued with the idea of using one device for everything. Well, with the Samsung Dex application that comes built into these next gen phones, it might be possible...?  As a guy that spends a lot of time working on ARM Templates and PowerShell scripts for Azure management, I was curious to see if I could get my phone, using Dex, connected to my Azure DevOps environment and start working with repos.... Well, to my surprise, I was able to, and without much pain. So, in this post, I'll run through how I got my Dex environment setup and working with Azure DevOps Repos. Getting Started With Samsung Dex open, go to the Google Play store and install Termux ( https://play.google.com/store/apps/details?id=com.termux&hl=en_US ) Once that's installed, open it! Next, we need to gift Termux with permissions to a...

Facebook and Two-Factor Auth

So in this post, I'm going to go over a quick setup on how to turn of two-factor auth with Facebook. Facebook does some interesting things once you turn on two-factor auth. If you have the app installed on your device, it will push a login code to that device. You can also set this up with SMS and the DUO Mobile app. We'll go over all three here. For a more high-level document on 2-factor auth, see my post HERE . As I've said before, this is not meant to be an in-depth guide, but more of a how-to for those that wouldn't normally think of turning on additional security settings. With that, let's get started: 1) Fist off, log into your Facebook on a computer and go over to the little down arrow in the upper right corner and select Settings. In the settings area, you'll want to select "Security" on the left. 2) In the Security Settings, you'll need to select the Edit button in the "Login Approvals" section. You'll be presented wi...