Wednesday, April 25, 2018

Sharded Cluser – Basic Outline - MongoDB


Sharded Cluser – Basic Outline

This Outline is based on MongoDB documentation

Basic Components
  • Mongod
  • Mongos
  • Config servers

Deploy Sharded Cluster

1. Create the Config Server Replica Set

Start each member of the config server replica set

Configuration File

sharding:
  clusterRole: configsvr
replication:
  replSetName: <setname>

# mongod -f <path-to-config-file>

Connect to one of the config servers

# mongo --host <hostname> --port <port>

Initiate and add members to the replica set

# rs.initiate()
# rs.add("cfg2.example.net:27019")
# rs.add(“cfg3.example.net:27019”)

Once the config server replica set (CSRS) is initiated and up, proceed to creating the shard replica sets.

2. Create the Shard Replica Sets

Start each member of the shard replica set.

Configuration File

sharding:
  clusterRole: shardsvr
replication:
  replSetName: <replSetName>

# mongod -f <path-to-config-file>

Connect to a member of the shard replica set.

# mongo --host <hostname> --port <port>

Initiate and add members to the replica set

# rs.initiate()
# rs.add"s1-mongo2.example.net:27018")
# rs.add"s1-mongo3.example.net:27018")

3. Connect a mongos to the Sharded Cluster

Connect a mongos to the cluster

Configuration File

sharding:
  configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...

# mongos -f <path-to-config>

Connect to the mongos.

# mongo --host <hostname> --port <port>

4. Add Shards to the Cluster

Use the sh.addShard() method to add each shard to the cluster
 If the shard is a replica set, specify the name of the replica set and specify a member of the set.

# sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")

To check the status

sh.status()

Tuesday, April 24, 2018

Basic Replication Setup with Arbiter - MongoDB

Basic Replication Setup with Arbiter


Arbiter

  • Arbiters are mongod instances that are part of a replica set
  • It does not hold any data
  • It only participates in election during failover
  • If a replica set has an even number of members, add an arbiter
  • Arbiters have minimal resource requirements and do not require dedicated hardware.


Let’s see an example with Arbiter

Servers:

ec2-34-228-80-121.compute-1.amazonaws.com - data
ec2-54-147-246-184.compute-1.amazonaws.com - data
ec2-54-243-20-99.compute-1.amazonaws.com - Arbiter

Note:

  • Please follow the same steps which is mentioned in the previous post Basic Replication Setup till adding replica set member.
  • Config file also as same as the replica set member
  • There will a small change in adding an Arbiter

 Assuming that all 3 servers are up and running

Connect a mongo shell to one of the mongod instances.

ec2-34-228-80-121.compute-1.amazonaws.com
# mongo




Initiate the replica set.


# rs.initiate()





Add the Replica Set Members

# rs.add(“ec2-54-147-246-184.compute-1.amazonaws.com:27017”)
# rs.addArb(“ec2-54-243-20-99.compute-1.amazonaws.com:27017”)
 



View the replica set configuration.

# rs.conf()

Here you can see arbiterOnly : true in the Arbiter node

learning:PRIMARY> rs.conf()
{
        "_id" : "learning",
        "version" : 5,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "ip-172-31-29-148.ec2.internal:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "ec2-54-147-246-184.compute-1.amazonaws.com:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "ec2-54-243-20-99.compute-1.amazonaws.com:27017",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5aded4fcfbfb90cc683df7b3")
        }
}
learning:PRIMARY>


To check the status of Arbiter node

# rs.status()

Here you can find "stateStr" : "ARBITER" in the Arbiter node

learning:PRIMARY> rs.status()
{
        "set" : "learning",
        "date" : ISODate("2018-04-24T07:05:17.699Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "ip-172-31-29-148.ec2.internal:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 588,
                        "optime" : {
                                "ts" : Timestamp(1524553218, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-24T07:00:18Z"),
                        "electionTime" : Timestamp(1524552956, 2),
                        "electionDate" : ISODate("2018-04-24T06:55:56Z"),
                        "configVersion" : 5,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "ec2-54-147-246-184.compute-1.amazonaws.com:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 308,
                        "optime" : {
                                "ts" : Timestamp(1524553218, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-24T07:00:18Z"),
                        "lastHeartbeat" : ISODate("2018-04-24T07:05:16.512Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-24T07:05:16.512Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "ip-172-31-29-148.ec2.internal:27017",
                        "configVersion" : 5
                },
                {
                        "_id" : 2,
                        "name" : "ec2-54-243-20-99.compute-1.amazonaws.com:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 299,
                        "lastHeartbeat" : ISODate("2018-04-24T07:05:16.511Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-24T07:05:13.468Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 5
                }
        ],
        "ok" : 1
}
learning:PRIMARY>

Done…