diff --git a/mongo-sharding-repl/README.md b/mongo-sharding-repl/README.md new file mode 100644 index 00000000..f3453645 --- /dev/null +++ b/mongo-sharding-repl/README.md @@ -0,0 +1,103 @@ +# mongo-sharding-repl + +## Как запустить + +Запускаем mongodb, два роутера, конфигурационный сервер и две шарды + +```shell +docker-compose up -d +``` + +Подключаемся к серверу конфигурации и делаем инициализацию + +```shell +docker exec -it configServer mongosh + +rs.initiate( + { + _id : "config_server", + configsvr: true, + members: [ + { _id : 0, host : "configServer" } + ] + } +); +exit(); +``` + +Подключимся к первой реплике + +```shell +docker exec -it repl11 mongosh + +rs.initiate({_id: "rs0", members: [{_id: 0, host: "repl11"}, {_id: 1, host: "repl12"}, {_id: 2, host: "repl13"}]}); +exit(); + +``` + +```shell +docker exec -it repl21 mongosh + +rs.initiate({_id: "rs1", members: [{_id: 0, host: "repl21"},{_id: 1, host: "repl22"},{_id: 2, host: "repl23"}]}); +exit(); +``` +Инициализируем роутер и наполняем шарды данными: + +```shell +docker exec -it mongos_router1 mongosh + +sh.addShard( "rs0/repl11,repl12,repl13"); +sh.addShard( "rs1/repl21,repl22,repl23"); + +sh.enableSharding("somedb"); +sh.shardCollection("somedb.helloDoc", { "name" : "hashed" } ) + +use somedb +for(var i = 0; i < 1000; i++) db.helloDoc.insert({age:i, name:"testname"+i}) +db.helloDoc.countDocuments() +exit(); +``` + +Выведем количество документов по первой реплике + +```shell +docker compose exec -T repl11 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl12 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl13 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl21 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl22 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl23 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` diff --git a/mongo-sharding-repl/compose.yaml b/mongo-sharding-repl/compose.yaml new file mode 100644 index 00000000..a5fd42d2 --- /dev/null +++ b/mongo-sharding-repl/compose.yaml @@ -0,0 +1,162 @@ +name: mongo-sharding-repl + +services: + + configServer: + image: mongo:latest # docker образ + container_name: configServer + restart: always + networks: + app-network: + ipv4_address: 173.18.0.20 + volumes: + - config-data:/data/db + command: --configsvr --replSet config_server --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + mongos_router1: + image: mongo:latest + container_name: mongos_router1 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.23 + command: mongos --configdb config_server/configServer:27017 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl11: + image: mongo:latest # docker образ + container_name: repl11 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.24 + volumes: + - repl11-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl12: + image: mongo:latest # docker образ + container_name: repl12 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.25 + volumes: + - repl12-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl13: + image: mongo:latest # docker образ + container_name: repl13 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.26 + volumes: + - repl13-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl21: + image: mongo:latest # docker образ + container_name: repl21 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.27 + volumes: + - repl21-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl22: + image: mongo:latest # docker образ + container_name: repl22 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.28 + volumes: + - repl22-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl23: + image: mongo:latest # docker образ + container_name: repl23 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.29 + volumes: + - repl23-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + pymongoApi: + container_name: pymongoApi + build: + context: api_app + dockerfile: Dockerfile + image: kazhem/pymongo_api:1.0.0 + networks: + app-network: + ipv4_address: 173.18.0.30 + depends_on: + - configServer + - mongos_router1 + - repl11 + - repl12 + - repl13 + - repl21 + - repl22 + - repl23 + ports: + - 27016:27006 + environment: + MONGODB_URL: "mongodb://mongos_router1:27017/?readPreference=secondaryPreferred" + MONGODB_DATABASE_NAME: "somedb" + +networks: + app-network: + driver: bridge + ipam: + driver: default + config: + - subnet: 173.18.0.0/16 + +volumes: + config-data: + repl11-data: + repl12-data: + repl13-data: + repl21-data: + repl22-data: + repl23-data: \ No newline at end of file diff --git a/mongo-sharding/README.md b/mongo-sharding/README.md new file mode 100644 index 00000000..e2fb0ed9 --- /dev/null +++ b/mongo-sharding/README.md @@ -0,0 +1,93 @@ +# mongo-sharding + +## Как запустить + +Запускаем mongodb, два роутера, конфигурационный сервер и две шарды + +```shell +docker-compose up -d +``` + +Подключаемся к серверу конфигурации и делаем инициализацию + +```shell +docker exec -it configServer mongosh --port 27020 + +rs.initiate( + { + _id : "config_server", + configsvr: true, + members: [ + { _id : 0, host : "configServer:27020" } + ] + } +); +exit(); +``` + +Инициализируем шарды + +```shell +docker exec -it shard-1 mongosh --port 27021 + +rs.initiate( + { + _id : "shard-1", + members: [ + { _id : 0, host : "shard1:27021" } + ] + } +); +exit(); +``` + +```shell +docker exec -it shard-2 mongosh --port 27022 + +rs.initiate( + { + _id : "shard-2", + members: [ + { _id : 1, host : "shard2:27022" } + ] + } + ); + +rs.status(); +exit(); +``` + +Инициализируем роутер и наполняем шарды данными: + +```shell +docker exec -it mongos_router1 mongosh --port 27023 + +sh.addShard( "shard-1/shard1:27021"); +sh.addShard( "shard-2/shard2:27022"); + +sh.enableSharding("somedb"); +sh.shardCollection("somedb.helloDoc", { "name" : "hashed" } ) + +use somedb +for(var i = 0; i < 1000; i++) db.helloDoc.insert({age:i, name:"testname"+i}) +db.helloDoc.countDocuments() +exit(); +``` + +Выведем количество документов по первой шарде + +```shell + docker exec -it shard-1 mongosh --port 27021 + use somedb + db.helloDoc.countDocuments() + exit(); + ``` + +И количество документов по второй шарде + +```shell + docker exec -it shard-2 mongosh --port 27022 + use somedb; + db.helloDoc.countDocuments(); + exit(); + ``` diff --git a/mongo-sharding/compose.yaml b/mongo-sharding/compose.yaml new file mode 100644 index 00000000..f8b53e48 --- /dev/null +++ b/mongo-sharding/compose.yaml @@ -0,0 +1,132 @@ +name: mongo-sharding + +services: + + configServer: + image: mongo:latest # docker образ + container_name: configServer + restart: always + ports: + - "27020:27020" + networks: + app-network: + ipv4_address: 173.18.0.20 + volumes: + - config-data:/data/db + command: + [ + "--configsvr", + "--replSet", + "config_server", + "--bind_ip_all", + "--port", + "27020" + ] + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + shard1: + image: mongo:latest + container_name: shard-1 + restart: always + ports: + - "27021:27021" + networks: + app-network: + ipv4_address: 173.18.0.21 + volumes: + - shard1-data:/data/db + command: + [ + "--shardsvr", + "--replSet", + "shard-1", + "--bind_ip_all", + "--port", + "27021" + ] + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + shard2: + image: mongo:latest + container_name: shard-2 + restart: always + ports: + - "27022:27022" + networks: + app-network: + ipv4_address: 173.18.0.22 + volumes: + - shard2-data:/data/db + command: + [ + "--shardsvr", + "--replSet", + "shard-2", + "--bind_ip_all", + "--port", + "27022" + ] + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + mongos_router1: + image: mongo:latest + container_name: mongos_router1 + restart: always + ports: + - "27023:27023" + networks: + app-network: + ipv4_address: 173.18.0.23 + command: + [ + "mongos", + "--configdb", + "config_server/configServer:27020", + "--bind_ip_all", + "--port", + "27023" + ] + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + pymongoApi1: + container_name: pymongoApi1 + build: + context: api_app + dockerfile: Dockerfile + image: kazhem/pymongo_api:1.0.0 + depends_on: + - configServer + - shard1 + - shard2 + - mongos_router1 + ports: + - 27016:27106 + environment: + MONGODB_URL: "mongodb://mongos_router1:27023/?readPreference=secondaryPreferred" + MONGODB_DATABASE_NAME: "somedb" + +networks: + app-network: + driver: bridge + ipam: + driver: default + config: + - subnet: 173.18.0.0/16 + +volumes: + mongodb_data_container: + config-data: + shard1-data: + shard2-data: \ No newline at end of file diff --git a/redis/redis.conf b/redis/redis.conf new file mode 100644 index 00000000..9e07f28c --- /dev/null +++ b/redis/redis.conf @@ -0,0 +1,5 @@ +port 6379 +cluster-enabled yes +cluster-config-file nodes.conf +cluster-node-timeout 5000 +appendonly yes diff --git a/sharding-repl-cache/README.md b/sharding-repl-cache/README.md new file mode 100644 index 00000000..97353d98 --- /dev/null +++ b/sharding-repl-cache/README.md @@ -0,0 +1,109 @@ +# sharding-repl-cache + +## Как запустить + +Запускаем mongodb, два роутера, конфигурационный сервер и две шарды + +```shell +docker-compose up -d +``` + +Подключаемся к серверу конфигурации и делаем инициализацию + +```shell +docker exec -it configServer mongosh + +rs.initiate( + { + _id : "config_server", + configsvr: true, + members: [ + { _id : 0, host : "configServer" } + ] + } +); +exit(); +``` + +Подключимся к первой реплике + +```shell +docker exec -it repl11 mongosh + +rs.initiate({_id: "rs0", members: [{_id: 0, host: "repl11"}, {_id: 1, host: "repl12"}, {_id: 2, host: "repl13"}]}); +exit(); + +``` + +```shell +docker exec -it repl21 mongosh + +rs.initiate({_id: "rs1", members: [{_id: 0, host: "repl21"},{_id: 1, host: "repl22"},{_id: 2, host: "repl23"}]}); +exit(); +``` +Инициализируем роутер и наполняем шарды данными: + +```shell +docker exec -it mongos_router1 mongosh + +sh.addShard( "rs0/repl11,repl12,repl13"); +sh.addShard( "rs1/repl21,repl22,repl23"); + +sh.enableSharding("somedb"); +sh.shardCollection("somedb.helloDoc", { "name" : "hashed" } ) + +use somedb +for(var i = 0; i < 1000; i++) db.helloDoc.insert({age:i, name:"testname"+i}) +db.helloDoc.countDocuments() +exit(); +``` + +Выведем количество документов по первой реплике + +```shell +docker compose exec -T repl11 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl12 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl13 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl21 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl22 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker compose exec -T repl23 mongosh +use somedb +db.helloDoc.countDocuments(); +exit(); + ``` + +```shell +docker exec -it repl12 mongosh +rs.status().members.length; +exit(); +``` \ No newline at end of file diff --git a/sharding-repl-cache/compose.yaml b/sharding-repl-cache/compose.yaml new file mode 100644 index 00000000..82c6dbc6 --- /dev/null +++ b/sharding-repl-cache/compose.yaml @@ -0,0 +1,174 @@ +name: sharding-repl-cache + +services: + + configServer: + image: mongo:latest # docker образ + container_name: configServer + restart: always + networks: + app-network: + ipv4_address: 173.18.0.20 + volumes: + - config-data:/data/db + command: --configsvr --replSet config_server --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + mongos_router1: + image: mongo:latest + container_name: mongos_router1 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.23 + command: mongos --configdb config_server/configServer:27017 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl11: + image: mongo:latest # docker образ + container_name: repl11 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.24 + volumes: + - repl11-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl12: + image: mongo:latest # docker образ + container_name: repl12 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.25 + volumes: + - repl12-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl13: + image: mongo:latest # docker образ + container_name: repl13 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.26 + volumes: + - repl13-data:/data/db + command: --shardsvr --replSet rs0 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl21: + image: mongo:latest # docker образ + container_name: repl21 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.27 + volumes: + - repl21-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl22: + image: mongo:latest # docker образ + container_name: repl22 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.28 + volumes: + - repl22-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + repl23: + image: mongo:latest # docker образ + container_name: repl23 + restart: always + networks: + app-network: + ipv4_address: 173.18.0.29 + volumes: + - repl23-data:/data/db + command: --shardsvr --replSet rs1 --bind_ip_all --port 27017 + healthcheck: + test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ] + interval: 5s + start_period: 10s + + redis_1: + image: "redis:latest" + container_name: redis_1 + ports: + - "6379" + networks: + app-network: + ipv4_address: 173.18.0.2 + + pymongoApi: + container_name: pymongoApi + build: + context: api_app + dockerfile: Dockerfile + image: kazhem/pymongo_api:1.0.0 + networks: + app-network: + ipv4_address: 173.18.0.30 + depends_on: + - configServer + - mongos_router1 + - repl11 + - repl12 + - repl13 + - repl21 + - repl22 + - repl23 + - redis_1 + ports: + - 8080:8080 + environment: + MONGODB_URL: "mongodb://mongos_router1" + MONGODB_DATABASE_NAME: "somedb" + REDIS_URL: "redis://redis_1:6379" + +networks: + app-network: + driver: bridge + ipam: + driver: default + config: + - subnet: 173.18.0.0/16 + +volumes: + config-data: + repl11-data: + repl12-data: + repl13-data: + repl21-data: + repl22-data: + repl23-data: + redis_1_data: {} \ No newline at end of file diff --git a/sharding-repl-cache/redis/redis.conf b/sharding-repl-cache/redis/redis.conf new file mode 100644 index 00000000..99dda0bb --- /dev/null +++ b/sharding-repl-cache/redis/redis.conf @@ -0,0 +1 @@ +port 6379 diff --git a/sprint2.drawio b/sprint2.drawio new file mode 100644 index 00000000..a187c467 --- /dev/null +++ b/sprint2.drawio @@ -0,0 +1,495 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +