Files
mtusi2026lab3/compose.yaml
T
2026-10-18 01:14:08 +03:00

184 lines
5.4 KiB
YAML

#file: noinspection ComposeUnknownValues
# Lab3: client -> HAProxy -> API x N -> PostgreSQL (source of truth: data,
# sessions, idempotency keys) + Valkey (shared session cache from lab2).
# Start: see REPORT.md, "Запуск из чистого clone".
name: messenger-lab3
x-api-env: &api-env
APP_HOST: 0.0.0.0
APP_PORT: "8080"
DATABASE_URL_FILE: /run/secrets/database_url
SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}"
SESSION_STORE: valkey
CACHE_URL_FILE: /run/secrets/cache_url
SESSION_CACHE_TTL_SECONDS: "60"
CACHE_TIMEOUT_SECONDS: "0.25"
CACHE_MAX_CONNECTIONS: "20"
CACHE_RETRY_AFTER_SECONDS: "5"
DB_POOL_SIZE: "10"
DB_MAX_OVERFLOW: "5"
DB_POOL_TIMEOUT_SECONDS: "3"
DB_CONNECT_TIMEOUT_SECONDS: "3"
DB_STATEMENT_TIMEOUT_MS: "5000"
IDEMPOTENCY_TTL_SECONDS: "${IDEMPOTENCY_TTL_SECONDS:-86400}"
SHUTDOWN_DRAIN_SECONDS: "3"
SHUTDOWN_TIMEOUT_SECONDS: "8"
services:
db:
image: postgres:16.4-alpine
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets: [postgres_password]
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 2s
timeout: 3s
retries: 20
start_period: 5s
networks: [backend]
stop_grace_period: 30s
restart: unless-stopped
deploy:
resources:
limits: { cpus: "1.0", memory: 512M }
# Session cache only (lab2). PostgreSQL stays the source of truth, so the
# cache is intentionally not persisted. It is shared by all API replicas:
# a logout on one replica writes the "revoked" tombstone seen by all.
valkey:
image: valkey/valkey:8.0.1-alpine
command:
- sh
- -c
- >-
exec valkey-server
--requirepass "$$(cat /run/secrets/valkey_password)"
--save ""
--appendonly no
--maxmemory 64mb
--maxmemory-policy allkeys-lru
secrets: [valkey_password]
healthcheck:
test:
- CMD-SHELL
- valkey-cli -a "$$(cat /run/secrets/valkey_password)" --no-auth-warning ping | grep -q PONG
interval: 2s
timeout: 3s
retries: 20
networks: [backend]
restart: unless-stopped
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
# Runs the migrations once, before any API replica starts (no race of
# replicas). A repeated run only prints SKIP.
migrate:
build: .
image: messenger-lab3-api:${APP_VERSION:-dev}
command: ["python", "scripts/migrate.py"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets: [database_url]
depends_on:
db: { condition: service_healthy }
networks: [backend]
restart: "no"
deploy:
resources:
limits: { cpus: "0.5", memory: 128M }
api:
build: .
image: messenger-lab3-api:${APP_VERSION:-dev}
environment: *api-env
secrets: [database_url, cache_url]
# No "ports": replicas are reachable only through the LB.
# No "container_name": the service is scaled by deploy.replicas / --scale.
networks: [edge, backend]
depends_on:
db: { condition: service_healthy }
valkey: { condition: service_healthy }
migrate: { condition: service_completed_successfully }
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: 2s
timeout: 3s
retries: 2
start_period: 5s
stop_signal: SIGTERM
# > SHUTDOWN_DRAIN_SECONDS + SHUTDOWN_TIMEOUT_SECONDS (3 + 8 s).
stop_grace_period: 15s
restart: unless-stopped
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: { cpus: "0.5", memory: 256M }
lb:
image: haproxy:3.0.11-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "${LB_PORT:-8080}:8080"
# Backend state page for the operator, only on the host loopback.
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404"
networks: [edge]
depends_on:
api: { condition: service_healthy }
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/lb-health"]
interval: 5s
timeout: 2s
retries: 3
restart: unless-stopped
deploy:
resources:
limits: { cpus: "0.5", memory: 128M }
# Runs the public and own checks in Linux (needed on Windows: the hooks are
# POSIX .sh scripts that call the docker CLI). Profile "tools": not started
# by a plain "up". The Docker socket grants control over the Docker host —
# use it only on the lab stand.
tester:
profiles: [tools]
build:
context: .
dockerfile: student-tests/Dockerfile.tester
working_dir: /work
environment:
BASE_URL: http://lb:8080
COMPOSE_PROJECT: messenger-lab3
volumes:
- ./:/work
- /var/run/docker.sock:/var/run/docker.sock
networks: [edge]
networks:
edge: {}
# PostgreSQL and Valkey: no route outside, the LB is not attached.
backend:
internal: true
volumes:
postgres_data: {}
secrets:
postgres_password:
file: ./infra/secrets/postgres_password.txt
database_url:
file: ./infra/secrets/database_url.txt
valkey_password:
file: ./infra/secrets/valkey_password.txt
cache_url:
file: ./infra/secrets/cache_url.txt