Skip to content
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
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# MongoDB Environment
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=password
MONGO_EXPRESS_USERNAME=admin
MONGO_EXPRESS_PASSWORD=password
42 changes: 12 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
# pymongo-api
###1
Сделано

## Как запустить
###2
Зайти в папку mongo-sharding и выполнить команды из README

Запускаем mongodb и приложение
###3
Зайти в папку mongo-sharding-repl и выполнить команды из README

```shell
docker compose up -d
```
###4
Зайти в папку sharding-repl-cache и выполнить команды из README

Заполняем mongodb данными
###5
Сделано

```shell
./scripts/mongo-init.sh
```

## Как проверить

### Если вы запускаете проект на локальной машине

Откройте в браузере http://localhost:8080

### Если вы запускаете проект на предоставленной виртуальной машине

Узнать белый ip виртуальной машины

```shell
curl --silent http://ifconfig.me
```

Откройте в браузере http://<ip виртуальной машины>:8080

## Доступные эндпоинты

Список доступных эндпоинтов, swagger http://<ip виртуальной машины>:8080/docs
###6
![scheme](./full_scheme.png)
3 changes: 2 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
services:
mongodb1:
container_name: mongodb1
image: dh-mirror.gitverse.ru/mongo:latest
image: mongo:latest
privileged: true
volumes:
- mongodb1_data_container:/data/db

Expand Down
Binary file added full_scheme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions mongo-sharding-repl/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# MongoDB Environment
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=password
MONGO_EXPRESS_USERNAME=admin
MONGO_EXPRESS_PASSWORD=password
1 change: 1 addition & 0 deletions mongo-sharding-repl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
149 changes: 149 additions & 0 deletions mongo-sharding-repl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# MongoDB Sharding Setup with Replica Sets

This project sets up a MongoDB sharded cluster with 2 shards (each with 2 replicas), 1 config server, and Redis for caching.

## Architecture

- 1 Config Server (configsvr)
- 2 Shards (each with 2 replicas)
- Shard 1: shard1svr1, shard1svr2
- Shard 2: shard2svr1, shard2svr2
- 1 Mongos Router
- 1 Redis Cache
- 1 API Service

## Prerequisites

- Docker and Docker Compose installed
- At least 4GB of RAM available
- Ports 27016-27021 and 6379 available

## Setup Steps

1. Start the containers:
```bash
docker compose up -d
```

2. Initialize the Config Server Replica Set:
```bash
docker exec -it configsvr mongosh --eval '
rs.initiate({
_id: "configrs",
configsvr: true,
members: [
{_id: 0, host: "configsvr:27017"}
]
})'
```

3. Initialize Shard 1 Replica Set:
```bash
docker exec -it shard1svr1 mongosh --eval '
rs.initiate({
_id: "shard1rs",
members: [
{_id: 0, host: "shard1svr1:27017"},
{_id: 1, host: "shard1svr2:27017"}
]
})'
```

4. Initialize Shard 2 Replica Set:
```bash
docker exec -it shard2svr1 mongosh --eval '
rs.initiate({
_id: "shard2rs",
members: [
{_id: 0, host: "shard2svr1:27017"},
{_id: 1, host: "shard2svr2:27017"}
]
})'
```

5. Add Shards to the Cluster:
```bash
docker exec -it mongos mongosh --eval '
sh.addShard("shard1rs/shard1svr1:27017,shard1svr2:27017")
sh.addShard("shard2rs/shard2svr1:27017,shard2svr2:27017")'
```

6. Enable Sharding for a Database:
```bash
docker exec -it mongos mongosh --eval '
sh.enableSharding("somedb")'
```

7. Shard a Collection:
```bash
docker exec -it mongos mongosh --eval '
sh.shardCollection("somedb.your_collection", { shardKey: 1 })'
```

8. Seed database:
```bash
sh scripts/mongo-init.sh
```

## Verification

1. Check Config Server Status:
```bash
docker exec -it configsvr mongosh --eval 'rs.status()'
```

2. Check Shard Status:
```bash
docker exec -it mongos mongosh --eval 'sh.status()'
```

3. Check Redis Connection:
```bash
docker exec -it redis redis-cli ping
```

## How to run

1. Start all services:
```bash
docker compose up -d
```

2. Wait for all containers to start (about 30 seconds)

3. Run the initialization scripts in order:
```bash
# Initialize config server
docker exec -it configsvr mongosh --eval 'rs.initiate({_id: "configrs", configsvr: true, members: [{_id: 0, host: "configsvr:27017"}]})'

# Initialize shard 1
docker exec -it shard1svr1 mongosh --eval 'rs.initiate({_id: "shard1rs", members: [{_id: 0, host: "shard1svr1:27017"}, {_id: 1, host: "shard1svr2:27017"}]})'

# Initialize shard 2
docker exec -it shard2svr1 mongosh --eval 'rs.initiate({_id: "shard2rs", members: [{_id: 0, host: "shard2svr1:27017"}, {_id: 1, host: "shard2svr2:27017"}]})'

# Add shards to cluster
docker exec -it mongos mongosh --eval 'sh.addShard("shard1rs/shard1svr1:27017,shard1svr2:27017"); sh.addShard("shard2rs/shard2svr1:27017,shard2svr2:27017")'

# Enable sharding
docker exec -it mongos mongosh --eval 'sh.enableSharding("somedb")'

# Shard collection
docker exec -it mongos mongosh --eval 'sh.shardCollection("somedb.your_collection", { shardKey: 1 })'

# Seed database
sh scripts/mongo-init.sh
```

4. Verify the setup:
```bash
# Check sharding status
docker exec -it mongos mongosh --eval 'sh.status()'

# Check Redis
docker exec -it redis redis-cli ping
```

5. Access the API:
- Local: http://localhost:8080
- Remote: http://<your-ip>:8080
10 changes: 10 additions & 0 deletions mongo-sharding-repl/api_app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.12.1-slim
WORKDIR /app
EXPOSE 8080
COPY requirements.txt ./
# Устанавливаем зависимости python не пересобирая их
RUN pip install --no-cache --no-cache-dir -r requirements.txt
# Копирование кода приложения
COPY app.py /app/
ENTRYPOINT ["uvicorn"]
CMD ["app:app", "--host", "0.0.0.0", "--port", "8080"]
Loading