Bootstrap

MongoDB Sharded Cluster 分片集群搭建教程:3 Config + 3 Shard + 2 Router容器化部署详细步骤【复制可执行】

MongoDB, allows for efficient horizontal scaling through sharding, making it suitable for handling large volumes of data. In this guide, we’ll walk through the process of setting up a MongoDB sharded cluster using Docker containers. The cluster will consist of three replica set config servers, three shards, and two router instances.

It is recommended that you first check out this article in order to get to know about basics of sharded mongo cluster first.

在这里插入图片描述

Step 1: Create Docker Network

To isolate our MongoDB containers, create a Docker network:

docker network create mongo-shard-cluster

Step 2: Config Server Containers

Create three config server containers:

docker run -d --net mongo-shard-cluster --name config-svr-1 -p 27101:27017 mongo:4.4 mongod --port 27017 --configsvr --replSet config-svr-replica-set

docker run -d --net mongo-shard-cluster --name config-svr-2 -p 27102:27017 mongo:4.4 mongod --port 27017 --configsvr --replSet config-svr-replica-set

docker run -d --net mongo-shard-cluster --name config-svr-3 -p 27103:27017 mongo:4.4 mongod --port 27017 --configsvr --replSet config-svr-replica-set

Step 3: Initialize Config Server Replica Set

Access the MongoDB shell of config-svr-1 and initialize the replica set: 注意rs.initiate命令只需要在其中一台Config Server执行即可,下边的实例是在config-svr-1上执行,执行完毕,再其他2台Config Server上检查rs.status()也可以看到结果。

docker exec -it config-svr-1 mongo

rs.initiate({
    _id: "config-svr-replica-set",
    configsvr: true,
    members: [
        { _id: 0, host: "config-svr-1:27017" },
        { _id: 1, host: "config-svr-2:27017" },
        { _id: 2, host: "config-svr-3:27017" }
    ]
})

Check the status to ensure successful configuration:

rs.status()

Step 4: Create and Initialize Shards

Repeat the following steps for each shard (shard-1, shard-2, shard-3): 对于每一个shard,只需要在其中一个容器中执行,如对于shard-1,共有3个container,只需要在shard-1-node-a上执行,另外2个shard-1-node-bshard-1-node-c不需要执行。

Create Shard Containers: 注意把下边代码中的X换成数字,如果创建shard-1,那么X就是1,以此类推。

docker run -d --net mongo-shard-cluster --name shard-X-node-a -p 2711X:27017 mongo:4.4 mongod --port 27017 --shardsvr --replSet shard-X-replica-set
docker run -d --net mongo-shard-cluster --name shard-X-node-b -p 2712X:27017 mongo:4.4 mongod --port 27017 --shardsvr --replSet shard-X-replica-set
docker run -d --net mongo-shard-cluster --name shard-X-node-c -p 2713X:27017 mongo:4.4 mongod --port 27017 --shardsvr --replSet shard-X-replica-set

Initialize Shard Replica Set: 注意把下边代码中的X换成数字

docker exec -it shard-X-node-a mongo

rs.initiate({
    _id: "shard-X-replica-set",
    members: [
        { _id: 0, host: "shard-X-node-a:27017" },
        { _id: 1, host: "shard-X-node-b:27017" },
        { _id: 2, host: "shard-X-node-c:27017" }
    ]
})

rs.status()

Step 5: Create Router Containers

Create two router containers:

docker run -d --net mongo-shard-cluster --name router-1 -p 27141:27017 mongo:4.4 mongos --port 27017 --configdb config-svr-replica-set/config-svr-1:27017,config-svr-2:27017,config-svr-3:27017 --bind_ip_all

docker run -d --net mongo-shard-cluster --name router-2 -p 27142:27017 mongo:4.4 mongos --port 27017 --configdb config-svr-replica-set/config-svr-1:27017,config-svr-2:27017,config-svr-3:27017 --bind_ip_all

Step 6: Initialize Routers and Add Shards

Access the MongoDB shell of router-1 and initialize: 如果你只有1个shard,比如只创建了shard-1-node-ashard-1-node-b,和shard-1-node-c,那么下边的sh.addShard命令只需要执行第一条命令即可。

docker exec -it router-1 mongo

sh.addShard("shard-1-replica-set/shard-1-node-a:27017", "shard-1-replica-set/shard-1-node-b:27017", "shard-1-replica-set/shard-1-node-c:27017")

sh.addShard("shard-2-replica-set/shard-2-node-a:27017", "shard-2-replica-set/shard-2-node-b:27017", "shard-2-replica-set/shard-2-node-c:27017")

sh.addShard("shard-3-replica-set/shard-3-node-a:27017", "shard-3-replica-set/shard-3-node-b:27017", "shard-3-replica-set/shard-3-node-c:27017")

sh.status()

Now! We’ve successfully set up a MongoDB sharded cluster. This architecture provides scalability and high availability for handling large datasets. Ensure to adjust ports, names, and configurations based on your specific requirements.

;