Files
mtusi2026lab3-Chinyaeva/compose.yaml

149 lines
4.0 KiB
YAML

# Laboratory 3 stand: one public load balancer, N API replicas, shared
# PostgreSQL and Redis. Copy .env.example to .env and choose local passwords.
# Only the load balancer publishes a host port; API/database ports stay internal.
name: mtusi-lab3
x-api-environment: &api-environment
APP_ENV: lab
DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL in .env}
CACHE_URL: ${CACHE_URL:-redis://redis:6379/0}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
DB_POOL_MIN: ${DB_POOL_MIN:-1}
DB_POOL_MAX: ${DB_POOL_MAX:-10}
DB_POOL_ACQUIRE_TIMEOUT_SECONDS: ${DB_POOL_ACQUIRE_TIMEOUT_SECONDS:-3}
DB_CONNECT_TIMEOUT_SECONDS: ${DB_CONNECT_TIMEOUT_SECONDS:-3}
DB_STATEMENT_TIMEOUT_MS: ${DB_STATEMENT_TIMEOUT_MS:-5000}
REDIS_CONNECT_TIMEOUT_SECONDS: ${REDIS_CONNECT_TIMEOUT_SECONDS:-2}
REDIS_SOCKET_TIMEOUT_SECONDS: ${REDIS_SOCKET_TIMEOUT_SECONDS:-2}
MAX_BODY_BYTES: ${MAX_BODY_BYTES:-1048576}
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: 3s
timeout: 3s
retries: 30
deploy:
resources:
limits:
cpus: "${POSTGRES_CPU_LIMIT:-1.0}"
memory: "${POSTGRES_MEM_LIMIT:-512M}"
networks:
- backend
redis:
image: redis:7.4.0-alpine
restart: unless-stopped
# Sessions are the only Redis data. AOF + the named volume keep active
# sessions across an API/Redis restart; TTL remains the expiry authority.
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
- --dir
- /data
volumes:
- redisdata:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 3s
timeout: 3s
retries: 30
deploy:
resources:
limits:
cpus: "${REDIS_CPU_LIMIT:-0.5}"
memory: "${REDIS_MEM_LIMIT:-256M}"
networks:
- backend
# Exactly one migration runner, not a race between API replicas. API
# containers wait for this service to exit successfully.
migrate:
build:
context: .
dockerfile: Dockerfile
command: ["python", "-m", "app.migrate"]
restart: "no"
environment: *api-environment
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits:
cpus: "${MIGRATE_CPU_LIMIT:-0.5}"
memory: "${MIGRATE_MEM_LIMIT:-256M}"
networks:
- backend
api:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
environment: *api-environment
expose:
- "8080"
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()"]
interval: 2s
timeout: 2s
start_period: 15s
retries: 15
stop_grace_period: 30s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "${API_CPU_LIMIT:-1.0}"
memory: "${API_MEM_LIMIT:-512M}"
networks:
- backend
lb:
image: nginx:1.27.5-alpine
restart: unless-stopped
volumes:
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "${API_PORT:-8080}:8080"
depends_on:
api:
condition: service_healthy
deploy:
resources:
limits:
cpus: "${LB_CPU_LIMIT:-0.5}"
memory: "${LB_MEM_LIMIT:-128M}"
networks:
- backend
volumes:
pgdata:
redisdata:
networks:
backend:
driver: bridge