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
Post a Comment