Files
mtusi2026lab3/compose.yaml
drunkdiente 3aba500a0e Реализовать стенд лабораторной 3: две API-реплики и балансировка
FastAPI + PostgreSQL как единый источник истины: пользователи, Basic/Bearer-сессии, чаты, сообщения. Сессии и идемпотентные ключи хранятся в БД, поэтому реплики взаимозаменяемы. Общая идемпотентность POST сообщения по Idempotency-Key, включая конкурентный повтор через разные реплики. compose.yaml: postgres, one-shot migrate, api1, api2 и nginx как единственная публичная точка входа. Добавлены scripts/stop-one.sh и scripts/restart.sh, student-tests, evidence и заполненный REPORT.md.
2026-11-07 16:04:29 +03:00

124 lines
2.9 KiB
YAML

# Laboratory 3 stand: one public entry point, two interchangeable API
# replicas, one shared PostgreSQL (data + sessions + idempotency keys).
#
# Usage:
# cp .env.example .env
# docker compose --env-file .env up -d --build
# curl -fsS http://localhost:8080/health/ready
name: mtusi-lab3
x-api-environment: &api-environment
APP_ENV: ${APP_ENV:-lab}
APP_HOST: 0.0.0.0
APP_PORT: "8080"
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger}
x-api-base: &api-base
build:
context: .
dockerfile: Dockerfile
image: mtusi-lab3-api:1.0.0
restart: unless-stopped
environment: *api-environment
depends_on:
postgres:
condition: service_healthy
migrate:
condition: service_completed_successfully
healthcheck:
test:
- CMD
- python
- -c
- import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
stop_grace_period: 10s
mem_limit: 256m
cpus: 1.0
networks:
- appnet
services:
postgres:
image: postgres:16.4-alpine
restart: unless-stopped
environment:
POSTGRES_DB: ${POSTGRES_DB:-messenger}
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
stop_grace_period: 20s
mem_limit: 512m
cpus: 1.0
networks:
- appnet
# Runs once and exits 0 before any API replica starts. Replicas never run DDL.
migrate:
build:
context: .
dockerfile: Dockerfile
image: mtusi-lab3-api:1.0.0
command: ["python", "-m", "app.migrate"]
restart: "no"
environment: *api-environment
depends_on:
postgres:
condition: service_healthy
networks:
- appnet
api1:
<<: *api-base
environment:
<<: *api-environment
INSTANCE_ID: api-1
api2:
<<: *api-base
environment:
<<: *api-environment
INSTANCE_ID: api-2
lb:
image: nginx:1.26.2-alpine
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
api1:
condition: service_healthy
api2:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8080/health/live || exit 1"]
interval: 5s
timeout: 3s
retries: 5
start_period: 5s
mem_limit: 128m
cpus: 0.5
networks:
- appnet
volumes:
pgdata:
networks:
appnet:
driver: bridge