How to create MongoDB Cluster in Docker/AWS Linux EC2 instance

Saurabh Singh
14 min readNov 3, 2019

--

This article assumes that the reader has basic knowledge of MongoDB cluster(at least in theory) and AWS EC2 dashboard.

I am using MongoDB since last 2 years in my project, but I never configured it on my own. So last weekend, I finally found the courage to make time to do it myself. Following are the pre-requisites that came to my mind:
- Whether to go for Windows or Linux based installation
- I don’t want to setup on my system, because I may sometime work from some remote location
- Which AWS AMI to use for this activity
- How many servers do I need
- And finally how and where to start

There are three main components in MongoDB cluster:

  1. Shard: Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set.
  2. Mongos (router): The mongos acts as query routers, providing an interface between client applications and the sharded cluster.
  3. Config servers: Config servers store metadata and configuration settings for the cluster.

In the official documentation, the main architecture of a sharded cluster looks like this:

I won’t dive deep into the discussion whether the windows is better or linux. But I went for AWS Linux EC2 instance for my test.
Reason:
- Since I plan to create a cluster and I may need 15 nodes in total for that.
- I can access cloud instance from anywhere
- Use docker containers
- I plan to do the same setup to test Redis and OrientDB cluster as well on the same instance
- Cost effective

Launch instance and prepare the environment

To start with, you will need docker installed on your system or you can go ahead and make an AWS Linux instance like I did.

I used Amazon Linux AMI 2018.03.0 image and replaced the 8 GB SSD with 100 GB magnetic tape storage to reduce my cost. I may plan to use this server 15–20 hours per week. With this usage it may cost me around 25–30 USD every month.

I used t3.2xlarge EC2 instance type, having 8 CPU and 32 GB RAM. This is not the ideal configuration but it is more than enough for our test. You can actually use must lower configuration, but I plan to do install and configuration other applications as well in future, I selected this instance.

Note: Please make sure to download and save .pem credentials file when creating and launching the server. You will not be able to get it again.

If you are using Linux or Mac, you can SSH to connect to this instance or if you use Windows then you need to install Putty and PuttyGen to connect. Of-course there are others ways also.

After you connect to the server, please run following commands to update the packages and install docker.

  • Update the installed packages and package cache on your instance
sudo yum update -y
  • Install and start docker service
sudo yum install docker
sudo service docker start
  • Add the ec2-user to the docker group so you can execute Docker commands without using sudo
sudo usermod -a -G docker ec2-user
  • Verify that the ec2-user can run Docker commands without sudo
docker info

Now the server is ready for the mongodb installation.

Create MongoDB shards

Before we start, please note that we are not going to use docker-compose or K8S, so we may have to create a custom network and use that for communication among containers.

Cluster architecture consists of

  • 3 Shards
  • Each shard contains 1 Primary and 2 Secondary nodes/members
  • 3 Config servers
  • 2 Routers/Mongos
  • Create custom bridge network — mynet
docker network create mynetdocker network inspect mynet
  • Script to create shard01: we map the mongo database volume to host machine path and expose 27017 port. Custom network ‘mynet’ is also defined so that containers can communicate.

We will do the same to create shard02 and shard03

docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db11:/data/db -p 27011:27017 --expose=27017 -d --name=mongodb11 mongo --shardsvr --replSet shard01 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db12:/data/db -p 27012:27017 --expose=27017 -d --name=mongodb12 mongo --shardsvr --replSet shard01 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db13:/data/db -p 27013:27017 --expose=27017 -d --name=mongodb13 mongo --shardsvr --replSet shard01 --dbpath /data/db --port 27017
  • Script to create shard02
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db21:/data/db -p 27021:27017 --expose=27017 -d --name=mongodb21 mongo --shardsvr --replSet shard02 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db22:/data/db -p 27022:27017 --expose=27017 -d --name=mongodb22 mongo --shardsvr --replSet shard02 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db23:/data/db -p 27023:27017 --expose=27017 -d --name=mongodb23 mongo --shardsvr --replSet shard02 --dbpath /data/db --port 27017
  • Script to create shard03
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db31:/data/db -p 27031:27017 --expose=27017 -d --name=mongodb31 mongo --shardsvr --replSet shard03 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db32:/data/db -p 27032:27017 --expose=27017 -d --name=mongodb32 mongo --shardsvr --replSet shard03 --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/data/db33:/data/db -p 27033:27017 --expose=27017 -d --name=mongodb33 mongo --shardsvr --replSet shard03 --dbpath /data/db --port 27017

Create config servers

use following script to create 3 config server. ‘configserver’ is the name of replica-set

docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/configserver/data/db41:/data/db -d --expose=27017 --name=configserver41 mongo --configsvr --replSet configserver --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/configserver/data/db42:/data/db -d --expose=27017 --name=configserver42 mongo --configsvr --replSet configserver --dbpath /data/db --port 27017
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-v /mongodb/configserver/data/db43:/data/db -d --expose=27017 --name=configserver43 mongo --configsvr --replSet configserver --dbpath /data/db --port 27017

Create routers servers

use following script to create two routers

docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-p 27051:27017 --expose=27017 -d --name=mongos51 mongo mongos --port 27017 --configdb configserver/configserver41:27017,configserver42:27017,configserver43:27017 --bind_ip_all
docker run --log-opt mode=non-blocking \
--network mynet \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=10 \
-p 27052:27017 --expose=27017 -d --name=mongos52 mongo mongos --port 27017 --configdb configserver/configserver41:27017,configserver42:27017,configserver43:27017 --bind_ip_all

Initialize config servers replica set

We can configure the config server replicate set and check the status using following commands.

Provide details of all members or config servers while initializing the replica set.

docker exec -it configserver41 bash -c "echo 'rs.initiate({_id: \"configserver\",configsvr: true, members: [{ _id : 0, host : \"configserver41:27017\" },{ _id : 1, host : \"configserver42:27017\" },{ _id : 2, host : \"configserver43:27017\" }]})' | mongo"docker exec -it configserver41 bash -c "echo 'rs.status()' | mongo"
docker exec -it configserver42 bash -c "echo 'rs.initiate({_id: \"configserver\",configsvr: true, members: [{ _id : 0, host : \"configserver41:27017\" },{ _id : 1, host : \"configserver42:27017\" },{ _id : 2, host : \"configserver43:27017\" }]})' | mongo"docker exec -it configserver42 bash -c "echo 'rs.status()' | mongo"
docker exec -it configserver43 bash -c "echo 'rs.initiate({_id: \"configserver\",configsvr: true, members: [{ _id : 0, host : \"configserver41:27017\" },{ _id : 1, host : \"configserver42:27017\" },{ _id : 2, host : \"configserver43:27017\" }]})' | mongo"docker exec -it configserver43 bash -c "echo 'rs.status()' | mongo"

Replica set status will look like this. There will be one PRIMARY node and two SECONDARY nodes.

[ec2-user@ip-######### ~]$ docker exec -it mongodb11 bash -c "echo 'rs.status()' | mongo"
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("#######-#####3-############") }
MongoDB server version: 4.2.1
{
"set" : "shard01",
"date" : ISODate("2019-11-03T12:15:31.046Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"lastCommittedWallTime" : ISODate("2019-11-03T12:15:29.470Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"readConcernMajorityWallTime" : ISODate("2019-11-03T12:15:29.470Z"),
"appliedOpTime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"lastAppliedWallTime" : ISODate("2019-11-03T12:15:29.470Z"),
"lastDurableWallTime" : ISODate("2019-11-03T12:15:29.470Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1572783299, 1),
"lastStableCheckpointTimestamp" : Timestamp(1572783299, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2019-11-03T12:03:08.815Z"),
"termAtElection" : NumberLong(2),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1572771161, 1),
"t" : NumberLong(1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2019-11-03T12:03:09.453Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2019-11-03T12:03:10.459Z")
},
"members" : [
{
"_id" : 0,
"name" : "mongodb11:27017",
"ip" : "172.19.0.2",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 806,
"optime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-11-03T12:15:29Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1572782588, 1),
"electionDate" : ISODate("2019-11-03T12:03:08Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "mongodb12:27017",
"ip" : "172.19.0.3",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 749,
"optime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-11-03T12:15:29Z"),
"optimeDurableDate" : ISODate("2019-11-03T12:15:29Z"),
"lastHeartbeat" : ISODate("2019-11-03T12:15:30.855Z"),
"lastHeartbeatRecv" : ISODate("2019-11-03T12:15:30.502Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "mongodb11:27017",
"syncSourceHost" : "mongodb11:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "mongodb13:27017",
"ip" : "172.19.0.13",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 608,
"optime" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1572783329, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-11-03T12:15:29Z"),
"optimeDurableDate" : ISODate("2019-11-03T12:15:29Z"),
"lastHeartbeat" : ISODate("2019-11-03T12:15:30.997Z"),
"lastHeartbeatRecv" : ISODate("2019-11-03T12:15:29.143Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "mongodb12:27017",
"syncSourceHost" : "mongodb12:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff0000000000000002")
},
"lastCommittedOpTime" : Timestamp(1572783329, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1572783326, 1),
"t" : NumberLong(5)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1572783329, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1572783329, 1)
}

Initialize shared replica set

We can configure the shared replicate set and check the status using following commands.

Provide details of all members or shared nodes while initializing the replica set.

docker exec -it mongodb11 bash -c "echo 'rs.initiate({_id : \"shard01\", members: [{ _id : 0, host : \"mongodb11:27017\" },{ _id : 1, host : \"mongodb12:27017\" }]}, { _id : 2, host : \"mongodb13:27017\" }]})' | mongo"docker exec -it mongodb11 bash -c "echo 'rs.status()' | mongo"
docker exec -it mongodb21 bash -c "echo 'rs.initiate({_id : \"shard02\", members: [{ _id : 0, host : \"mongodb21:27017\" },{ _id : 1, host : \"mongodb22:27017\" }]}, { _id : 2, host : \"mongodb23:27017\" }]})' | mongo"docker exec -it mongodb21 bash -c "echo 'rs.status()' | mongo"
docker exec -it mongodb31 bash -c "echo 'rs.initiate({_id : \"shard03\", members: [{ _id : 0, host : \"mongodb31:27017\" },{ _id : 1, host : \"mongodb32:27017\" }]}, { _id : 2, host : \"mongodb33:27017\" }]})' | mongo"docker exec -it mongodb31 bash -c "echo 'rs.status()' | mongo"

Replica set status will look like this. There will be one PRIMARY node and two SECONDARY nodes.

MongoDB server version: 4.2.1
{
"set" : "configserver",
"date" : ISODate("2019-11-03T12:17:04.279Z"),
"myState" : 2,
"term" : NumberLong(5),
"syncingTo" : "configserver43:27017",
"syncSourceHost" : "configserver43:27017",
"syncSourceId" : 2,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"lastCommittedWallTime" : ISODate("2019-11-03T12:17:02.650Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"readConcernMajorityWallTime" : ISODate("2019-11-03T12:17:02.650Z"),
"appliedOpTime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"durableOpTime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"lastAppliedWallTime" : ISODate("2019-11-03T12:17:02.650Z"),
"lastDurableWallTime" : ISODate("2019-11-03T12:17:02.650Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1572783409, 1),
"lastStableCheckpointTimestamp" : Timestamp(1572783409, 1),
"members" : [
{
"_id" : 0,
"name" : "configserver41:27017",
"ip" : "172.19.0.10",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 435,
"optime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2019-11-03T12:17:02Z"),
"syncingTo" : "configserver43:27017",
"syncSourceHost" : "configserver43:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "configserver42:27017",
"ip" : "172.19.0.9",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 434,
"optime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2019-11-03T12:17:02Z"),
"optimeDurableDate" : ISODate("2019-11-03T12:17:02Z"),
"lastHeartbeat" : ISODate("2019-11-03T12:17:03.050Z"),
"lastHeartbeatRecv" : ISODate("2019-11-03T12:17:03.879Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "configserver43:27017",
"syncSourceHost" : "configserver43:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "configserver43:27017",
"ip" : "172.19.0.8",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 434,
"optime" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1572783422, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2019-11-03T12:17:02Z"),
"optimeDurableDate" : ISODate("2019-11-03T12:17:02Z"),
"lastHeartbeat" : ISODate("2019-11-03T12:17:02.999Z"),
"lastHeartbeatRecv" : ISODate("2019-11-03T12:17:04.125Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1572782992, 1),
"electionDate" : ISODate("2019-11-03T12:09:52Z"),
"configVersion" : 1
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(1572783422, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1572783422, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1572783422, 1)
}

Initialize routers/mongos

We can configure both routers and introduce all three shards to them and check the status using following commands.

docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard01/mongodb11\")' | mongo "
docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard02/mongodb21\")' | mongo "
docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard03/mongodb31\")' | mongo "
docker exec -it mongos51 bash -c "echo 'sh.status()' | mongo "
docker exec -it mongos52 bash -c "echo 'sh.addShard(\"shard01/mongodb11\")' | mongo "
docker exec -it mongos52 bash -c "echo 'sh.addShard(\"shard02/mongodb21\")' | mongo "
docker exec -it mongos52 bash -c "echo 'sh.addShard(\"shard03/mongodb31\")' | mongo "
docker exec -it mongos52 bash -c "echo 'sh.status()' | mongo "

Router status will look like this.

MongoDB server version: 4.2.1
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dbe480d40aaff49b37d26d5")
}
shards:
{ "_id" : "shard01", "host" : "shard01/mongodb11:27017,mongodb12:27017,mongodb13:27017", "state" : 1, "tags" : [ "A" ] }
{ "_id" : "shard02", "host" : "shard02/mongodb21:27017,mongodb22:27017,mongodb23:27017", "state" : 1, "tags" : [ "B" ] }
{ "_id" : "shard03", "host" : "shard03/mongodb31:27017,mongodb32:27017,mongodb33:27017", "state" : 1, "tags" : [ "C" ] }
active mongoses:
"4.2.1" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 5
Last reported error: Could not find host matching read preference { mode: "primary" } for set shard02
Time of Reported error: Sun Nov 03 2019 06:59:06 GMT+0000 (UTC)
Migration Results for the last 24 hours:
4 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard01 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard01 Timestamp(1, 0)
{ "_id" : "studymongo", "primary" : "shard02", "partitioned" : true, "version" : { "uuid" : UUID("5381275a-b411-4a98-8913-a39cbad635b3"), "lastMod" : 1 } }
studymongo.random
shard key: { "type" : 1, "pkey" : 1 }
unique: false
balancing: true
chunks:
shard01 2
shard02 3
shard03 2
{ "type" : { "$minKey" : 1 }, "pkey" : { "$minKey" : 1 } } -->> { "type" : "A", "pkey" : { "$minKey" : 1 } } on : shard03 Timestamp(4, 0)
{ "type" : "A", "pkey" : { "$minKey" : 1 } } -->> { "type" : "A", "pkey" : { "$maxKey" : 1 } } on : shard01 Timestamp(2, 0)
{ "type" : "A", "pkey" : { "$maxKey" : 1 } } -->> { "type" : "B", "pkey" : { "$minKey" : 1 } } on : shard01 Timestamp(5, 0)
{ "type" : "B", "pkey" : { "$minKey" : 1 } } -->> { "type" : "B", "pkey" : { "$maxKey" : 1 } } on : shard02 Timestamp(5, 1)
{ "type" : "B", "pkey" : { "$maxKey" : 1 } } -->> { "type" : "C", "pkey" : { "$minKey" : 1 } } on : shard02 Timestamp(1, 7)
{ "type" : "C", "pkey" : { "$minKey" : 1 } } -->> { "type" : "C", "pkey" : { "$maxKey" : 1 } } on : shard03 Timestamp(3, 0)
{ "type" : "C", "pkey" : { "$maxKey" : 1 } } -->> { "type" : { "$maxKey" : 1 }, "pkey" : { "$maxKey" : 1 } } on : shard02 Timestamp(1, 9)
tag: A { "type" : "A", "pkey" : { "$minKey" : 1 } } -->> { "type" : "A", "pkey" : { "$maxKey" : 1 } }
tag: B { "type" : "B", "pkey" : { "$minKey" : 1 } } -->> { "type" : "B", "pkey" : { "$maxKey" : 1 } }
tag: C { "type" : "C", "pkey" : { "$minKey" : 1 } } -->> { "type" : "C", "pkey" : { "$maxKey" : 1 } }

Now we have a sharded cluster and next step is to create sharded database and collection. Define appropriate shard key and distribution type. In the output above you can notice following:

  • sharded database — studymongo
  • sharded collection — random
  • sharded zone/key — 3 zones defined on ‘type’ property/attribute

I will explain database sharding in next article in this series. I will also explain the major settings involved in managing cluster, voting algorithm, node fail-over workflow, arbitrator etc.

Cheers.

--

--