-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce sharding rules to MongoDB collections
- Loading branch information
Showing
7 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
version: '3' | ||
services: | ||
|
||
# Config Server | ||
config1: | ||
image: mongo | ||
container_name: mongo-config1 | ||
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27100:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
config2: | ||
image: mongo | ||
container_name: mongo-config2 | ||
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27101:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
config3: | ||
image: mongo | ||
container_name: mongo-config3 | ||
command: mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27102:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
|
||
# Mongos | ||
mongos1: | ||
image: mongo | ||
container_name: mongos1 | ||
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all | ||
ports: | ||
- 27110:27017 | ||
restart: always | ||
volumes: | ||
- ./scripts:/scripts | ||
networks: | ||
- mongo | ||
mongos2: | ||
image: mongo | ||
container_name: mongos2 | ||
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all | ||
ports: | ||
- 27111:27017 | ||
restart: always | ||
volumes: | ||
- ./scripts:/scripts | ||
networks: | ||
- mongo | ||
mongos3: | ||
image: mongo | ||
container_name: mongos3 | ||
command: mongos --port 27017 --configdb config-rs/config1:27017,config2:27017,config3:27017 --bind_ip_all | ||
ports: | ||
- 27112:27017 | ||
restart: always | ||
volumes: | ||
- ./scripts:/scripts | ||
networks: | ||
- mongo | ||
|
||
# Shards | ||
# Shards 1 | ||
shard1-1: | ||
image: mongo | ||
container_name: mongo-shard1-1 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27120:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard1-2: | ||
image: mongo | ||
container_name: mongo-shard1-2 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27121:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard1-3: | ||
image: mongo | ||
container_name: mongo-shard1-3 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27122:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
|
||
# Shards 2 | ||
shard2-1: | ||
image: mongo | ||
container_name: mongo-shard2-1 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27123:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard2-2: | ||
image: mongo | ||
container_name: mongo-shard2-2 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27124:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard2-3: | ||
image: mongo | ||
container_name: mongo-shard2-3 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27125:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
|
||
# Shards 3 | ||
shard3-1: | ||
image: mongo | ||
container_name: mongo-shard3-1 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27126:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard3-2: | ||
image: mongo | ||
container_name: mongo-shard3-2 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27127:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
shard3-3: | ||
image: mongo | ||
container_name: mongo-shard3-3 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-3 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27128:27017 | ||
restart: always | ||
networks: | ||
- mongo | ||
|
||
networks: | ||
mongo: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
version: '3' | ||
services: | ||
|
||
# Config Server | ||
config1: | ||
image: mongo:7.0.1 | ||
container_name: mongo-config1 | ||
command: | ||
- /bin/sh | ||
- -c | ||
- | # run the init script for the primary replica after mongod has been started. | ||
sh -c "sleep 5 && mongosh < /scripts/init-config1.js" & | ||
mongod --port 27017 --configsvr --replSet config-rs --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27100:27017 | ||
restart: always | ||
|
||
|
||
# Shards | ||
# Shards 1 | ||
shard1-1: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard1-1 | ||
command: | ||
- /bin/sh | ||
- -c | ||
- | # run the init script for the primary replica after mongod has been started. | ||
sh -c "sleep 5 && mongosh < /scripts/init-shard1-1.js" & | ||
mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27110:27017 | ||
restart: always | ||
|
||
shard1-2: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard1-2 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27111:27017 | ||
restart: always | ||
|
||
shard1-3: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard1-3 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-1 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27112:27017 | ||
restart: always | ||
|
||
|
||
# Shards 2 | ||
shard2-1: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard2-1 | ||
command: | ||
- /bin/sh | ||
- -c | ||
- | # run the init script for the primary replica after mongod has been started. | ||
sh -c "sleep 5 && mongosh < /scripts/init-shard2-1.js" & | ||
mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27113:27017 | ||
restart: always | ||
|
||
shard2-2: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard2-2 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27114:27017 | ||
restart: always | ||
|
||
shard2-3: | ||
image: mongo:7.0.1 | ||
container_name: mongo-shard2-3 | ||
command: mongod --port 27017 --shardsvr --replSet shard-rs-2 --bind_ip_all | ||
volumes: | ||
- ./scripts:/scripts | ||
ports: | ||
- 27115:27017 | ||
restart: always | ||
|
||
|
||
# Mongos | ||
mongos1: | ||
image: mongo:7.0.1 | ||
container_name: mongos1 | ||
command: | ||
- /bin/sh | ||
- -c | ||
- | # run the init script for the primary replica after the config servers and the shards has been configured | ||
sh -c "sleep 20 && mongosh < /scripts/init-mongos1.js" & | ||
mongos --port 27017 --configdb config-rs/config1:27017 --bind_ip_all | ||
ports: | ||
- 27120:27017 | ||
restart: always | ||
volumes: | ||
- ./scripts:/scripts | ||
|
||
depends_on: | ||
- config1 | ||
- shard1-1 | ||
- shard1-2 | ||
- shard1-3 | ||
- shard2-1 | ||
- shard2-2 | ||
- shard2-3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
rs.initiate( | ||
{ | ||
_id: "config-rs", | ||
configsvr: true, | ||
members: [ | ||
{ _id : 0, host : "config1:27017" }, | ||
] | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
sh.addShard( "shard-rs-1/shard1-1:27017,shard1-2:27017,shard1-3:27017") | ||
sh.addShard( "shard-rs-2/shard2-1:27017,shard2-2:27017,shard2-3:27017") | ||
|
||
sh.enableSharding("yorkie-meta") | ||
sh.shardCollection("yorkie-meta.projects", { _id : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.users", { username : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.clients", { project_id : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.documents", { project_id : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.changes", { doc_id : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.snapshots", { doc_id : "hashed" } ) | ||
sh.shardCollection("yorkie-meta.syncedseqs", { doc_id : "hashed" } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
rs.initiate( | ||
{ | ||
_id : "shard-rs-1", | ||
members: [ | ||
{ _id : 0, host : "shard1-1:27017" }, | ||
{ _id : 1, host : "shard1-2:27017" }, | ||
{ _id : 2, host : "shard1-3:27017" } | ||
] | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
rs.initiate( | ||
{ | ||
_id : "shard-rs-2", | ||
members: [ | ||
{ _id : 0, host : "shard2-1:27017" }, | ||
{ _id : 1, host : "shard2-2:27017" }, | ||
{ _id : 2, host : "shard2-3:27017" } | ||
] | ||
} | ||
) |