Apply completed lab 4 implementation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+247
@@ -0,0 +1,247 @@
|
||||
# Compose-стенд lab4: nginx → 2 API-реплики → PostgreSQL (метаданные) + Redis (сессии) + SeaweedFS (S3, байты).
|
||||
# Перед первым запуском: python3 scripts/init_secrets.py — секреты появятся в secrets/ (вне Git).
|
||||
|
||||
# Общая часть API-реплик, migration и reconciler: один образ, разные режимы запуска.
|
||||
x-api: &api
|
||||
build: .
|
||||
image: messenger-api:lab4
|
||||
environment:
|
||||
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/messenger
|
||||
SPRING_DATASOURCE_USERNAME: messenger
|
||||
SPRING_DATA_REDIS_HOST: redis
|
||||
SESSION_TTL_SECONDS: 3600
|
||||
S3_ENDPOINT: http://s3:8333
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
# Секреты читаются Spring как свойства: имя файла в /run/secrets = имя свойства (configtree).
|
||||
secrets:
|
||||
- source: db_password
|
||||
target: spring.datasource.password
|
||||
- source: redis_password
|
||||
target: spring.data.redis.password
|
||||
- source: s3_api_access_key
|
||||
target: s3.access-key
|
||||
- source: s3_api_secret_key
|
||||
target: s3.secret-key
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
migration:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health/ready"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
restart: unless-stopped
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.0'
|
||||
memory: 512M
|
||||
reservations:
|
||||
cpus: '0.5'
|
||||
memory: 256M
|
||||
|
||||
services:
|
||||
# Nginx балансирует запросы между API-репликами; единственный опубликованный порт стенда.
|
||||
nginx:
|
||||
image: nginx:1.28.0-alpine
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- ./infra/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
depends_on:
|
||||
api-1:
|
||||
condition: service_healthy
|
||||
api-2:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.5'
|
||||
memory: 128M
|
||||
|
||||
# Две взаимозаменяемые API-реплики без локального состояния.
|
||||
api-1: *api
|
||||
api-2: *api
|
||||
|
||||
# Однократные миграции Flyway до запуска реплик; нужен только пароль БД.
|
||||
migration:
|
||||
<<: *api
|
||||
command: ["migrate"]
|
||||
secrets:
|
||||
- source: db_password
|
||||
target: spring.datasource.password
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
|
||||
# Сверка БД и S3 по требованию: docker compose run --rm reconciler [--apply] [--grace-seconds=N].
|
||||
reconciler:
|
||||
<<: *api
|
||||
# Режим в entrypoint: аргументы docker compose run дописываются к нему.
|
||||
entrypoint: ["java", "-Dfile.encoding=UTF-8", "-jar", "/app/messenger.jar", "reconcile"]
|
||||
command: []
|
||||
profiles: ["tools"]
|
||||
secrets:
|
||||
- source: db_password
|
||||
target: spring.datasource.password
|
||||
- source: s3_api_access_key
|
||||
target: s3.access-key
|
||||
- source: s3_api_secret_key
|
||||
target: s3.secret-key
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
|
||||
# PostgreSQL — источник истины: пользователи, чаты, сообщения, метаданные вложений.
|
||||
postgres:
|
||||
image: postgres:16.4
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
|
||||
secrets:
|
||||
- db_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '2.0'
|
||||
memory: 1G
|
||||
|
||||
# Redis хранит Bearer-сессии; пароль и AOF заданы в redis.conf из secrets/.
|
||||
redis:
|
||||
image: redis:7.4.1
|
||||
command: ["redis-server", "/run/secrets/redis.conf"]
|
||||
secrets:
|
||||
- source: redis_conf
|
||||
target: redis.conf
|
||||
- redis_password
|
||||
volumes:
|
||||
- redisdata:/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.5'
|
||||
memory: 256M
|
||||
|
||||
# SeaweedFS: master/volume/filer слушают только 127.0.0.1 внутри контейнера (их HTTP-админки
|
||||
# без авторизации), в сеть стенда открыт только S3 :8333. Наружу порты не публикуются.
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:3.99
|
||||
command:
|
||||
- server
|
||||
- -dir=/data
|
||||
- -ip=127.0.0.1
|
||||
- -ip.bind=127.0.0.1
|
||||
- -s3
|
||||
- -s3.ip.bind=0.0.0.0
|
||||
- -s3.port=8333
|
||||
- -s3.config=/run/secrets/s3_config.json
|
||||
- -master.volumeSizeLimitMB=1024
|
||||
- -volume.max=0
|
||||
# Мягкий предел кучи Go ниже лимита контейнера: без него при 10+ параллельных загрузках
|
||||
# SeaweedFS упирался в 512M (tests/upload_memory.py, REPORT.md).
|
||||
environment:
|
||||
GOMEMLIMIT: 320MiB
|
||||
secrets:
|
||||
- source: s3_config
|
||||
target: s3_config.json
|
||||
volumes:
|
||||
- s3data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8333/healthz"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.0'
|
||||
memory: 512M
|
||||
|
||||
# Однократный идемпотентный bootstrap приватного bucket (admin-ключи есть только здесь и в s3).
|
||||
s3-init:
|
||||
image: curlimages/curl:8.11.1
|
||||
entrypoint: ["/bin/sh", "/init/s3-init.sh"]
|
||||
environment:
|
||||
S3_ENDPOINT: http://s3:8333
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
volumes:
|
||||
- ./infra/s3/s3-init.sh:/init/s3-init.sh:ro
|
||||
secrets:
|
||||
- s3_admin_access_key
|
||||
- s3_admin_secret_key
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
# Клиент S3 мимо API для проверок и внедрения сбоев: scripts/check-private-bucket.sh,
|
||||
# tests/reconcile_experiment.py. Только ключи API; скрипт читается из stdin.
|
||||
s3-probe:
|
||||
image: curlimages/curl:8.11.1
|
||||
profiles: ["tools"]
|
||||
entrypoint: ["/bin/sh", "-eu", "-s"]
|
||||
secrets:
|
||||
- source: s3_api_access_key
|
||||
target: s3.access-key
|
||||
- source: s3_api_secret_key
|
||||
target: s3.secret-key
|
||||
restart: "no"
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
redisdata:
|
||||
s3data:
|
||||
|
||||
# Файлы создаёт scripts/init_secrets.py; в Git их нет.
|
||||
secrets:
|
||||
db_password:
|
||||
file: ./secrets/db_password
|
||||
redis_password:
|
||||
file: ./secrets/redis_password
|
||||
redis_conf:
|
||||
file: ./secrets/redis.conf
|
||||
s3_config:
|
||||
file: ./secrets/s3_config.json
|
||||
s3_api_access_key:
|
||||
file: ./secrets/s3_api_access_key
|
||||
s3_api_secret_key:
|
||||
file: ./secrets/s3_api_secret_key
|
||||
s3_admin_access_key:
|
||||
file: ./secrets/s3_admin_access_key
|
||||
s3_admin_secret_key:
|
||||
file: ./secrets/s3_admin_secret_key
|
||||
Reference in New Issue
Block a user