Friday, December 12, 2014

How to Insall Mongo DB cluster Guide




1: Add the MongoDB Repository
vi /etc/yum.repos.d/mongodb.repo

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

Then exit and save the file with the command :wq

2: Install MongoDB
yum install mongo-10gen mongo-10gen-server


Config server
The config server processes are mongod instances that store the cluster’s metadata.
More

Replca Set
A MongoDB replica set is a cluster of mongod instances that replicate amongst one another and ensure automated failover.
More

Mongos
mongos for “MongoDB Shard,” is a routing service for MongoDB shard configurations that processes queries from the application layer, and determines the location of this data in the sharded cluster, in order to complete these operations.
More

Example:



Server Setup
Add a new user
Create a new user named mongodb on each server, this user will be the one who starts the mongodb processes.

adduser mongodb
su - mongodb

Prepare directories:
We need to prepare all the data and log directories with proper privileges.

# Commands are using to setup the server.
# Creating a directory for data path.
sudo mkdir /var/lib/mongodb/dbs
sudo chown mongodb:mongodb -R /var/lib/mongodb/dbs
cd /etc/
sudo mkdir mongodb
sudo chown mongodb:mongodb -R /etc/mongodb
sudo cp mongodb.conf mongodb/
sudo mv mongodb.conf mongodb.conf.default
We did this on all the servers running mongodb cluster.

Configuration Servers:
Make a configuration file for mongodb’s config server.

cd /etc/mongodb/
vi config_db.conf
The configuration file of mongod running on config servers should have,

fork=true
dbpath=/var/lib/mongodb/dbs/config_db
logpath=/var/log/mongodb/config_db.log
logappend=true
port=27020
Last step, start the config server by

sudo mongod --configsvr --config /etc/mongodb/config_db.conf
And do the same thing on all 3 config servers of our own.


Mongos
First we need to create a configuration file for mongos.

cd /etc/mongodb/
vi mongos.conf
The content in mongos configuration file is

fork = true
port = 27017
configdb = xxx.xxx.xxx.xxx:port,xxx.xxx.xxx.xxx:port,xxx.xxx.xxx.xxx:port # Here you should put the domain name of your 3 configuration servers.
logpath=/var/log/mongodb/mongos.log
Now we start our mongos process.

mongos --config /etc/mongodb/mongos.conf

Replica Sets
First we need to create configuration files for our mongod.
We have 3 Replica Sets and each set has 3 mongod running, one of them is ”arbiter”, we create 3 

configuration files on each of our data server like.

cd /etc/mongodb/
touch set0_db.conf
touch set1_db.conf
touch set2_db.conf

The content inside should have

fork = true
port = 27017
dbpath=/var/lib/mongodb/dbs/set<index of this set>_db
logpath=/var/log/mongodb/set<index of this set>_db.log
logappend = true
journal = true
replSet = set<index of this set>
And as usual, we start the mongod process using command:

mongod --config set<index of this set>_db.conf
Last step, we need to initialize these 3 sets seperately.

set0

rs.initiate({_id:'set0', members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host: 'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");

set1

rs.initiate({_id:'set1', members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host: 'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");

set2

rs.initiate({_id:'set2', members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host: 'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");

Add Shards:
Now we can connect to ‘mongos’ and add our 3 Replica Sets as 3 shards.

mongo --host <domain name of mongos> --port <port of mongos running>
connecting to: <domain name of mongos>/test
mongos> sh.addShard("set0/<primary of set0>:<port>");
{ "shardAdded" : "set0", "ok" : 1 }
mongos> sh.addShard("set1/<primary of set1>:<port>");
{ "shardAdded" : "set1", "ok" : 1 }
mongos> sh.addShard("set2/<primary of set2>:<port>");
{ "shardAdded" : "set2", "ok" : 1 }

Enable database sharding:
In order to make use of sharding in MongoDB, we need to manually choose the database and collections those
we want them to be sharded.
Take our system as an example.
First connect to mongos,

mongo --host <host> --port <port>
Then type the following commands in mongo shell.
Here we need to set the collection ‘students’ in database ‘test’ being able to be sharded.

use admin
sh.enableSharding("test")
sh.shardCollection("test.students", { "grades": 1 })
That’s it, we have successfully set up our MongoDB Sharding Cluster!

Verify Sharding:
Now you need to find out if your cluster is really working.
You can use the following code to verify the sharding we currently have.

mongo --host 198.211.98.146 --port 27017
use admin
db.runCommand( { listshards : 1 } );
And you suppose to have the result below

{
 "shards" : [
  {
   "_id" : "set0",
   "host" : "set0/198.211.100.130:27018,198.211.100.172:27017"
  },
  {
   "_id" : "set1",
   "host" : "set1/198.211.100.130:27017,198.211.100.158:27018"
  },
  {
   "_id" : "set2",
   "host" : "set2/198.211.100.158:27017,198.211.100.172:27018"
  }
 ],
 "ok" : 1
}

Other Settings

Copy DB
Sometimes, like we encountered once, we need to change one of our config servers to another machine.
In this case, we need to do the following things.
  • Shutdown all processes (mongod, mongos, config server).
  • Copy the data subdirectories (dbpath tree) from the config server to the new config servers.
  • Start the config servers.
  • Restart mongos processes with the new –configdb parameter.
  • Restart mongod processes.
You can use this command to copy a database from another server.

mongo --port 27020
use config
db.copyDatabase("config", "config", "xxx.xxx.xxx.xxx:27020");

Logrotate
Since every day MongoDB generates a lot of logs, we need a way to compress them and delete them after a period of time.
So we can created 2 crontab jobs to achieve this goal.
This script runs daily at 0:05AM to collect the old logs and compress them.

#! /bin/sh
killall -SIGUSR1 mongod
killall -SIGUSR1 mongos # This line only applicable on swordfish
find /var/log/mongodb -type f \( -iname "*.log.*" ! -iname "*.gz" \) -exec gzip -f {} \;
This script runs every first day of a month, this will remove all the compressed logs from last month.

#! /bin/sh
find /var/log/mongodb -type f -name "*.gz" -exec rm -f {} \;
We also need to add crontab for these two shell commands.

crontab -e

0  0 * * * /path/to/your/mongodb_logrotate.sh
0 10 1 * * /path/to/your/mongodb_clearlog.sh

Deploy MMS Agent
We are now using 10gen’s MMS as our monitoring system. In order to use this, we need to let their agent running
on our mongos server.
Here is how we set it up.
First download the agent from your hosts dashboard.
Then

# prereqs
sudo apt-get install python python-setuptools
sudo easy_install pip
sudo pip install pymongo

#set up agent
cd /path/to/your/dir
mkdir mms-agent
unzip name-of-agent.zip -d mms-agent
cd mms-agent
nohup python agent.py > logs/agent.log 2>&1 &
And we finished!
The agent will auto discover other servers in you cluster, although it still needs some manually work for you to do in the dashboard, but it is really helpful for us to monitor the whole system in real time.

How to install Zabbix on Centos. IT Infrastructure Availability and performance Monitoring Tool


1. Installing Zabbix daemons
1 Download the source archive
$ tar -zxvf zabbix-2.4.2.tar.gz

2 Create user account
groupadd zabbix   
useradd -g zabbix zabbix

3 Create Zabbix database
Database creation scripts
MySQL
shell> mysql -u<username> -p<password>
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> quit;
shell> mysql -u<username> -p<password> zabbix < database/mysql/schema.sql
# stop here if you are creating database for Zabbix proxy
shell> mysql -u<username> -p<password> zabbix < database/mysql/images.sql
shell> mysql -u<username> -p<password> zabbix < database/mysql/data.sql

4 Configure the sources
prerequisites:
yum install net-snmp
yum install php-common php-mbstring php-bcmath

Step 2
Make sure that all software prerequisites are met.
cd zabbix-2.4.2
./configure --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2

5 Make and install everything
make install

note: error: 'XML_PARSE_HUGE' undeclared (first use in this function) zabbix upgrade
vi /usr/include/libxml2/libxml/parser.h
add:
 typedef enum {
     ...
     XML_PARSE_HUGE = 1 << 19, / * relax any hardcoded limit from the parser * /
     ...
 } XmlParserOption;
save and type
make install

6 Review and edit configuration files
edit the Zabbix agent configuration file /usr/local/etc/zabbix_agentd.conf
edit the Zabbix server configuration file /usr/local/etc/zabbix_server.conf

if you have installed a Zabbix proxy, edit the proxy configuration file
/usr/local/etc/zabbix_proxy.conf

vi /usr/local/etc/zabbix_server.conf
DBPassword=password


7 Start up the daemonsshell> zabbix_server
shell> zabbix_agentd

To start/stop daemons copy scripts from
/zabbix-2.4.2/misc/init.d/fedora/core to /etc/init.d/

2. Installing Zabbix web interface
Copying PHP files
Common locations of HTML documents directories for Apache web servers include:
/var/www/html (Fedora, RHEL, CentOS)

mkdir <htdocs>/zabbix
cd frontends/php
cp php <htdocs>/zabbix

chmod -R 777 /var/www/html/zabbix

Installing frontend
Step 1
In your browser, open Zabbix URL: http://<server_ip_or_name>/zabbix
You should see the first screen of the frontend installation wizard.



After completing all the steps: you will see Zabbix “Welcome” screen. Enter the user name Admin with password zabbix to log in as a Zabbix superuser.




Install Windows Agent:

Create zabbix agent configuration file c:\zabbix_agentd.conf.
sample file available at zabbix_agents_2.2.0.win\conf\zabbix_agentd.win.conf.

Now edit configuration and update following values.
Server=192.168.1.11
Hostname=WIN-SERVER-2012

Lets install zabbix agent as windows server by executing following command from command line:
c:\zabbix_agents_2.2.0.win\bin\win64> zabbix_agentd.exe --install

Use following command to start zabbix agent service from command line
c:\zabbix_agents_2.2.0.win\bin\win64> zabbix_agentd.exe --start

Open run windows >> type “services.msc” >> press enter
start zabbix service.


Install Zabbix-Agent from source on the Linux server that you want to monitor.
download zabbix source package
tar -xzf zabbix-2.0.2.tar.gz
./configure --enable-agent
make install
adduser zabbix

Copy the sample configs to /usr/local/etc for the agentd.
[root@mail zabbix-2.0.2]# cp conf/zabbix_agentd.conf /usr/local/etc

Now go to /etc/zabbix/zabbix_agentd.conf, and edit (at least):
Server=192.168.0.69
ServerActive=192.168.0.69 [Example IP address of the Zabbix Server]
Hostname=Mail_Server [Exactly the same name as our zabbix host name – See #1 above.

NOTE:This name is case sensitive!]
ListenIP=192.168.0.100 [Example

Next, configure /etc/init.d/
cp misc/init.d/debian/zabbix-agent /etc/init.d/zabbix-agent

Install Zabbix Agent on CentOS/RHEL

Step 1: Add Required Repository
CentOS/RHEL 6:
# rpm -Uvh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2-1.el6.noarch.rpm

CentOS/RHEL 5:
# rpm -Uvh http://repo.zabbix.com/zabbix/2.2/rhel/5/x86_64/zabbix-release-2.2-1.el5.noarch.rpm

Step 2: Install Zabbix Agent
yum install zabbix zabbix-agent

Step 3: Update Zabbix Agent Configuration
Edit zabbix agent configuration file /etc/zabbix/zabbix_agentd.conf and update

Zabbix server ip
#Server=[zabbix server ip]
#Hostname=[ Hostname of client system ]

Server=192.168.1.11
Hostname=Server1

Step 4: Start/Stop Zabbix Agent
# /etc/init.d/zabbix-agent start
# /etc/init.d/zabbix-agent stop


Screen shots: