Files
mtusi2026lab4/compose.yaml
T
2026-10-30 16:53:49 +03:00

278 lines
9.0 KiB
YAML

#file: noinspection ComposeUnknownValues
# Lab4: client -> HAProxy -> API x N -> PostgreSQL (source of truth: data,
# sessions, idempotency keys, attachment metadata) + Valkey (session cache)
# + SeaweedFS S3 (private bucket with attachment bytes).
# Start: see REPORT.md, "Запуск из чистого clone".
name: messenger-lab4
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"
# Lab4: S3 with the API's own restricted credentials (not admin).
S3_ENDPOINT: http://s3:9000
S3_REGION: us-east-1
S3_BUCKET: messenger-attachments
S3_OBJECT_PREFIX: originals/
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
S3_CONNECT_TIMEOUT_SECONDS: "2"
S3_READ_TIMEOUT_SECONDS: "5"
S3_OPERATION_TIMEOUT_SECONDS: "6"
S3_PUT_MIN_BYTES_PER_SECOND: "1048576"
MAX_UPLOAD_BYTES: "10485760"
MAX_IMAGE_PIXELS: "40000000"
UPLOAD_SPOOL_MEMORY_BYTES: "1048576"
UPLOAD_TIMEOUT_SECONDS: "60"
MAX_CONCURRENT_UPLOADS: "8"
UPLOAD_SLOT_WAIT_SECONDS: "10"
UPLOAD_GRACE_SECONDS: "${UPLOAD_GRACE_SECONDS:-900}"
JANITOR_INTERVAL_SECONDS: "${JANITOR_INTERVAL_SECONDS:-60}"
# Experiments only: crash_after_s3_put | db_error_after_s3_put (REPORT.md).
FAULT_INJECTION: "${FAULT_INJECTION:-}"
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 }
# S3: SeaweedFS all-in-one. No "ports": reachable only from the internal
# backend network; its admin web UIs are bound to 127.0.0.1 inside the
# container (infra/seaweedfs/start.sh). Bytes persist in the s3_data volume.
s3:
image: chrislusf/seaweedfs:3.97
entrypoint: ["/bin/sh", "/opt/lab/start.sh"]
environment:
# Soft limit of the Go heap: without it the GC lets SeaweedFS grow to
# the container limit under parallel 10 MiB PUTs (measured, REPORT.md).
GOMEMLIMIT: 600MiB
volumes:
- s3_data:/data
- ./infra/seaweedfs:/opt/lab:ro
secrets:
- s3_admin_access_key_id
- s3_admin_secret_access_key
- s3_access_key_id
- s3_secret_access_key
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9000/healthz"]
interval: 2s
timeout: 3s
retries: 30
start_period: 60s
networks: [backend]
stop_grace_period: 30s
restart: unless-stopped
deploy:
resources:
# 512M was measured to be too little: 12 parallel 10 MiB PUTs pushed
# SeaweedFS to the limit and the kernel killed it (REPORT.md).
limits: { cpus: "1.0", memory: 1G }
# Idempotent bucket bootstrap + self-checks (private bucket, least
# privilege of the API key). The only container with the S3 admin key.
s3-init:
build: .
image: messenger-lab4-api:${APP_VERSION:-dev}
command: ["python", "scripts/s3_bootstrap.py"]
environment:
S3_ENDPOINT: http://s3:9000
S3_BUCKET: messenger-attachments
S3_OBJECT_PREFIX: originals/
S3_ADMIN_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access_key_id
S3_ADMIN_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret_access_key
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
secrets:
- s3_admin_access_key_id
- s3_admin_secret_access_key
- s3_access_key_id
- s3_secret_access_key
depends_on:
s3: { condition: service_healthy }
networks: [backend]
restart: "no"
deploy:
resources:
limits: { cpus: "0.5", 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-lab4-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-lab4-api:${APP_VERSION:-dev}
environment: *api-env
secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key]
# 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 }
s3-init: { 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-lab4
S3_ENDPOINT: http://s3:9000
volumes:
- ./:/work
- /var/run/docker.sock:/var/run/docker.sock
# backend: only for the "private bucket from outside the API" check.
networks: [edge, backend]
networks:
edge: {}
# PostgreSQL, Valkey and S3: no route outside, the LB is not attached.
backend:
internal: true
volumes:
postgres_data: {}
s3_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
s3_admin_access_key_id:
file: ./infra/secrets/s3_admin_access_key_id.txt
s3_admin_secret_access_key:
file: ./infra/secrets/s3_admin_secret_access_key.txt
s3_access_key_id:
file: ./infra/secrets/s3_access_key_id.txt
s3_secret_access_key:
file: ./infra/secrets/s3_secret_access_key.txt