mongoDB Replicas and Shards - noobaa/noobaa-core GitHub Wiki

General ReplicaSet and Sharding deployment. Assumes MongoDB version 3.2 and up.

This page will be broken into 2 sections:

  1. [Creating a Replica Set in MongoDB](#my-Creating a Replica Set in MongoDB)
  2. [Creating a Shard Cluster in MongoDB](#Creating a Shard Cluster in MongoDB)

Creating a Replica Set in MongoDB

MongoDB Documentation

Cut to the Chase

  1. Have three machines installed with the same version of MongoDB.
  2. On each of the machine start the mongod service with the --replSet "NAME_OF_REPLICA" (in our case, update the /etc/noobaa_supervisor.conf command for the mongodb service)
  3. On one of the nodes (the one you wish to be the primary) run_ rs.initiate()_
  4. On the primary node, run rs.add(IP:PORT) for each additional node in the replica set
  5. You can verify the replica set configuration and status by using rs.status() and rs.conf()

Instead of running the services with command line args, this configuration can be done in the mongo conf file.

Creating a Shard Cluster in MongoDB

MongoDB Documentation

Cut to the Chase

Prepare the config servers replica set (these hold the MD for the sharded cluster)

  1. Have three machines installed with the same version of MongoDB.
  2. On each of these instances run mongod --configsvr --replSet SETNAME --dbpath /data/db (in our case, update the /etc/noobaa_supervisor.conf command for the mongodb service)
  3. Choose one node to be the primary and run rs.initiate() on it
  4. On the primary node, run rs.add(IP:PORT) for each additional node in the replica set
  5. On each node run mongos --configdb configReplSet/IP1:27019,IP2:27019,IP3:27019 where IP1 - IP3 are the IPs of the three nodes in the set. (in our case, update /etc/noobaa_supervisor.conf and add a mongos service containing the command above)

Enable Sharding for the DB

  1. Connect to the primary mongod by using mongo --host IP --port PORT and run sh.enableSharding('DB_NAME') in the shell

Setting Shards

  1. [Choose a Sharding Key] (https://docs.mongodb.org/manual/tutorial/choose-a-shard-key/#sharding-shard-key-selection) See manual for setting shard key for already populated db.
  2. Shard the specific collections in the db you want to by using the sh.shardCollection(".", shard-key-pattern) command. For example, sharding our objects and their related collections (parts, blocks etc.)
    sh.shardCollection("nbcore.objectmds", { "_id": 1 } )
    
    

A good reference can be found at

Instead of running the services with command line args, this configuration can be done in the mongo conf file.

⚠️ **GitHub.com Fallback** ⚠️