Files
mtusi2026lab3/compose.yaml
T

140 lines
3.7 KiB
YAML

# Lab3 topology: one public entrypoint (nginx LB) -> 2+ interchangeable API
# replicas -> shared PostgreSQL (source of truth) and Valkey (session cache).
# Versions are pinned; no API replica publishes a port, so the only reachable
# path is through the balancer. Secrets are file-based and live in ./secrets
# (gitignored). Migrations run once in the dedicated `migrate` service.
name: messenger-lab3
x-api-env: &api-env
APP_HOST: "0.0.0.0"
APP_PORT: "8080"
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_STORE: ${SESSION_STORE:-valkey}
SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-60}
CACHE_INVALIDATION_INTERVAL_SECONDS: ${CACHE_INVALIDATION_INTERVAL_SECONDS:-0.5}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
DB_POOL_MIN: ${DB_POOL_MIN:-1}
DB_POOL_MAX: ${DB_POOL_MAX:-10}
DB_POOL_TIMEOUT: ${DB_POOL_TIMEOUT:-5}
DB_STATEMENT_TIMEOUT_MS: ${DB_STATEMENT_TIMEOUT_MS:-5000}
LOG_LEVEL: ${LOG_LEVEL:-INFO}
services:
lb:
image: nginx:1.27.4-alpine
volumes:
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "127.0.0.1:${PUBLISH_PORT:-8080}:8080"
depends_on:
- api
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/health/live >/dev/null 2>&1 || exit 1"]
interval: 10s
timeout: 3s
retries: 5
start_period: 10s
deploy:
resources:
limits:
cpus: "0.50"
memory: 128M
api:
build:
context: .
image: messenger-api:lab3
environment: *api-env
secrets:
- database_url
- cache_url
expose:
- "8080"
depends_on:
migrate:
condition: service_completed_successfully
valkey:
condition: service_healthy
restart: unless-stopped
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).status == 200 else 1)"]
interval: 5s
timeout: 3s
retries: 5
start_period: 20s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "0.75"
memory: 256M
# One-shot schema migration; runs once before replicas start.
migrate:
build:
context: .
image: messenger-api:lab3
command: ["python", "-m", "src.migrate"]
environment: *api-env
secrets:
- database_url
depends_on:
postgres:
condition: service_healthy
restart: "no"
postgres:
image: postgres:16.6-bookworm
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/database_password
secrets:
- database_password
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 5s
timeout: 3s
retries: 12
restart: unless-stopped
deploy:
resources:
limits:
cpus: "1.00"
memory: 512M
valkey:
image: valkey/valkey:8.0.2-alpine
command: ["valkey-server", "--appendonly", "yes"]
volumes:
- valkeydata:/data
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 5s
timeout: 3s
retries: 12
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.50"
memory: 128M
volumes:
pgdata:
valkeydata:
secrets:
database_url:
file: ./secrets/database_url
database_password:
file: ./secrets/database_password
cache_url:
file: ./secrets/cache_url