The Journey of 100DaysofCode aka 100DaysofMongoDB (@Aasawari_24)

Day13 as #100DaysofMongoDB as #100daysofcode

Taking the Replication on kubernetes environment ahead, let us learn about how to do sharding in a kubernetes Environment.

A MongoDB sharded cluster consists of:

  1. shards: A replica set where each shard contains a subset of sharded cluster.
  2. mongos: Which acts as a query router
  3. config servers: Which store the meta data and configurations settings.

Here, we will be using docker containers to design a sharded structure with one sharded cluster, which is basically a replica set, one config server and one mongos.
Hence, 7 docker containers will be needed.
All the members of shard and config map and mongos will be running on different ports.

Deploying a sharded replica set using docker containers.

The docker compose file will look like:


version: '3'

services:

  shard1svr1:
    container_name: shard1svr1
    image: mongo
    command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
    ports:
      - 50001:27017
    volumes:
      - shard1svr1:/data/db

  shard1svr2:
    container_name: shard1svr2
    image: mongo
    command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
    ports:
      - 50002:27017
    volumes:
      - shard1svr2:/data/db

  shard1svr3:
    container_name: shard1svr3
    image: mongo
    command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
    ports:
      - 50003:27017
    volumes:
      - shard1svr3:/data/db

volumes:
  shard1svr1: {}
  shard1svr2: {}
  shard1svr3: {}

The command here, mongod --shardsvr means the mongod is started as sharded cluster.

The config server compose file would look similar with the command as
mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db.
defining the mongod as config server.
Lastly the yaml file for mongos

version: '3'

services:

  mongos:
    container_name: mongos
    image: mongo
    command: mongos --configdb cfgrs/192.168.29.4:40001,192.168.29.4:40002,192.168.29.4:40003 --bind_ip 0.0.0.0 --port 27017
    ports:
      - 60000:27017

After all the docker compose have been created do

docker-compose -f  configserver.yaml up -d
docker-compose -f shardserver.yaml up -d
docker-compose -f mongod.yaml up -d

The containers will look like:


aasawari.sahasrabuddhe@M-C02DV42LML85 sharding % docker ps
CONTAINER ID   IMAGE                                 COMMAND                  CREATED          STATUS          PORTS                                                                                                                                  NAMES
a89092ce4415   mongo                                 "docker-entrypoint.s…"   12 minutes ago   Up 12 minutes   0.0.0.0:60000->27017/tcp                                                                                                               mongos
fea4644bc0e0   mongo                                 "docker-entrypoint.s…"   15 minutes ago   Up 14 minutes   0.0.0.0:50001->27017/tcp                                                                                                               shard1svr1
da83eb637ae4   mongo                                 "docker-entrypoint.s…"   15 minutes ago   Up 14 minutes   0.0.0.0:50003->27017/tcp                                                                                                               shard1svr3
4c7f0f0ecf86   mongo                                 "docker-entrypoint.s…"   15 minutes ago   Up 14 minutes   0.0.0.0:50002->27017/tcp                                                                                                               shard1svr2
39418f832da9   mongo                                 "docker-entrypoint.s…"   32 minutes ago   Up 31 minutes   0.0.0.0:40003->27017/tcp                                                                                                               cfgsvr3
9297b1bc78ab   mongo                                 "docker-entrypoint.s…"   32 minutes ago   Up 31 minutes   0.0.0.0:40001->27017/tcp                                                                                                               cfgsvr1
b1804155e587   mongo                                 "docker-entrypoint.s…"   32 minutes ago   Up 31 minutes   0.0.0.0:40002->27017/tcp                                                                                                               cfgsvr2
08bcb43c7ff8   gcr.io/k8s-minikube/kicbase:v0.0.30   "/usr/local/bin/entr…"   9 days ago       Up 7 hours      127.0.0.1:50295->22/tcp, 127.0.0.1:50296->2376/tcp, 127.0.0.1:50298->5000/tcp, 127.0.0.1:50299->8443/tcp, 127.0.0.1:50297->32443/tcp   minikube
aasawari.sahasrabuddhe@M-C02DV42LML85 sharding %

After replica sets have been initiated in config server and sharded server with the respective port numbers as

mongo mongodb://192.168.29.4:40001 where 192.168.29.4 is the system IP address

Login to the mongos to add the shards to the mongos.

as

mongos> sh.addShard("shard1rs/192.168.29.4:50001,192.168.29.4:50002,192.168.29.4:50003")

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("623c84a6a12cb9bd172d459a")
  }
  shards:
        {  "_id" : "shard1rs",  "host" : "shard1rs/192.168.29.4:50001,192.168.29.4:50002,192.168.29.4:50003",  "state" : 1,  "topologyTime" : Timestamp(1648133766, 1) }
  active mongoses:
        "5.0.6" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled: yes
        Currently running: no
        Failed balancer rounds in last 5 attempts: 0
        Migration results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1rs	1024

Further let us understand how to add data and access the data through these sharded clusters.

If you have any questions related to docker containers or deploying replica sets using docker containers, feel free to reach out.

Thanks
Aasawari

Share on twitter: https://twitter.com/Aasawari_24

2 Likes