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 #55

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REDIS_URL: "redis://173.17.0.2:6379"
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# pymongo-api

## Задание 1. Планирование

Схемы приложения находятся тут: https://drive.google.com/file/d/1G8XDKuPBGRL1FXMgBxP1zA86DqaDoB4J/view?usp=sharing

На первой схеме показана исходная структура приложения

1 этап. Далее показан первый вариант схемы - использование шардирование в MongoDB для повышения производительности.

2 этап. Второй вариант схемы. Показана реализация репликации MongoDB для повышения отказоустойчивости.

3 этап. Третий вариант схемы - показано кэширование для ещё большего повышения производительности.

4 этап. Показана релизация трех инстансов приложения с использованием API Gateway и Consul.

5 этап. Показана реализация сервиса CDN для доставки статического контента пользователям в разных регионах.



## Как запустить

Запускаем mongodb и приложение
Expand Down
284 changes: 281 additions & 3 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
version: '3.8'

services:
mongodb1:
container_name: mongodb1
container_name: sharding-repl-cache
image: dh-mirror.gitverse.ru/mongo:latest
volumes:
- mongodb1_data_container:/data/db
Expand All @@ -14,11 +16,287 @@ services:
depends_on:
- mongodb1
ports:
- 8080:8080
- "8080:8080"
environment:
MONGODB_URL: "mongodb://mongodb1"
MONGODB_DATABASE_NAME: "somedb"

# Сервер конфигурации
configSrv:
image: mongo:latest
container_name: configSrv
restart: always
ports:
- "27017:27017"
networks:
app-network:
ipv4_address: 173.17.0.10
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

# 1-й шард
shard1:
image: mongo:latest
container_name: shard1
restart: always
ports:
- "27018:27018"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard1-data:/data/db
command:
[
"--shardsvr",
"--replSet", "rs0",
"--bind_ip_all",
"--port", "27018"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 1-й шард, реплика 1
shard11:
image: mongo:latest
container_name: shard11
restart: always
ports:
- "27018:27018"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard11-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard1",
"--bind_ip_all",
"--port", "27018"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 1-й шард, реплика 2
shard12:
image: mongo:latest
container_name: shard12
restart: always
ports:
- "27018:27018"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard12-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard1",
"--bind_ip_all",
"--port", "27018"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 1-й шард, реплика 3
shard13:
image: mongo:latest
container_name: shard13
restart: always
ports:
- "27018:27018"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard13-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard1",
"--bind_ip_all",
"--port", "27018"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 2-й шард
shard2:
image: mongo:latest
container_name: shard2
restart: always
ports:
- "27019:27019"
networks:
app-network:
ipv4_address: 173.17.0.8
volumes:
- shard2-data:/data/db
command:
[
"--shardsvr",
"--replSet", "rs0",
"--bind_ip_all",
"--port", "27019"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 2-й шард, реплика 1
shard21:
image: mongo:latest
container_name: shard21
restart: always
ports:
- "27019:27019"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard21-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard2",
"--bind_ip_all",
"--port", "27019"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 2-й шард, реплика 2
shard22:
image: mongo:latest
container_name: shard22
restart: always
ports:
- "27019:27019"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard22-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard2",
"--bind_ip_all",
"--port", "27019"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# 2-й шард, реплика 3
shard23:
image: mongo:latest
container_name: shard23
restart: always
ports:
- "27019:27019"
networks:
app-network:
ipv4_address: 173.17.0.9
volumes:
- shard23-data:/data/db
command:
[
"--shardsvr",
"--replSet", "shard2",
"--bind_ip_all",
"--port", "27019"
]
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s

# Роутер
mongos_router:
image: mongo:latest
container_name: mongos_router
restart: always
ports:
- "27020:27020"
networks:
app-network:
ipv4_address: 173.17.0.7
command:
[
"mongos",
"--configdb",
"config_server/configSrv:27017",
"--bind_ip_all",
"--port", "27020"
]
depends_on:
- configSrv
- shard1
- shard2
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 5s
start_period: 10s



redis:
image: "redis:latest"
container_name: redis
ports:
- "6379"
volumes:
- redis_data:/data
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
networks:
app-network:
ipv4_address: 173.17.0.2

networks:
app-network:
driver: bridge
ipam:
driver: default
config:
- subnet: 173.17.0.0/16

volumes:
mongodb1_data_container:
mongodb1_data_container:
config-data:
shard1-data:
shard11-data:
shard12-data:
shard13-data:
shard2-data:
shard21-data:
shard22-data:
shard23-data:
redis_data:

60 changes: 58 additions & 2 deletions scripts/mongo-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,64 @@
# Инициализируем бд
###

docker compose exec -T mongodb1 mongosh <<EOF
# sudo docker-compose down -v

# sudo docker-compose up -d



sudo docker compose exec -T configSrv mongosh --port 27017 --quiet <<EOF
rs.initiate(
{
_id : "config_server",
configsvr: true,
members: [
{ _id : 0, host : "configSrv:27017" }
]
}
);
exit();
EOF

sudo docker compose exec -T shard1 mongosh --port 27018 --quiet <<EOF

rs.initiate(
{
_id : "shard1",
members: [
{ _id : 0, host : "shard1:27018" },
]
}
);
exit();
EOF


sudo docker compose exec -T shard2 mongosh --port 27019 --quiet <<EOF
rs.initiate(
{
_id : "shard2",
members: [
{ _id : 1, host : "shard2:27019" }
]
}
);
exit();
EOF

sudo docker compose exec -T mongos_router mongosh --port 27020 --quiet <<EOF
sh.addShard( "shard1/shard1:27018");
sh.addShard( "shard2/shard2:27019");
sh.enableSharding("somedb");
sh.shardCollection("somedb.helloDoc", { "name" : "hashed" } )
use somedb
for(var i = 0; i < 1000; i++) db.helloDoc.insertOne({age:i, name:"ly"+i})
for(var i = 0; i < 1200; i++) db.helloDoc.insert({age:i, name:"ly"+i})
db.helloDoc.countDocuments()
exit();
EOF

sudo docker compose exec -T shard1 mongosh --port 27018 --quiet <<EOF
use somedb;
db.helloDoc.countDocuments();
exit();
EOF