Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Выполнение заданий 2го спринта #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions mongo-sharding-repl/README.md
Original file line number Diff line number Diff line change
@@ -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();
```
162 changes: 162 additions & 0 deletions mongo-sharding-repl/compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
93 changes: 93 additions & 0 deletions mongo-sharding/README.md
Original file line number Diff line number Diff line change
@@ -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();
```
Loading