Files
mtusi2026lab4/compose.yaml
2026-10-29 15:00:36 +03:00

249 lines
7.3 KiB
YAML

name: mtusi-lab4
x-api-env: &api-env
APP_HOST: 0.0.0.0
APP_PORT: "8080"
APP_ENV: ${APP_ENV:-lab}
SESSION_STORE: ${SESSION_STORE:-valkey}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_CACHE_MAX_TTL_SECONDS: ${SESSION_CACHE_MAX_TTL_SECONDS:-60}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
SHUTDOWN_DRAIN_MS: ${SHUTDOWN_DRAIN_MS:-3000}
DB_MAX_CONNS: ${DB_MAX_CONNS:-10}
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
S3_TIMEOUT: ${S3_TIMEOUT:-15s}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
UPLOAD_CONCURRENCY: ${UPLOAD_CONCURRENCY:-8}
DECODE_BUDGET_BYTES: ${DECODE_BUDGET_BYTES:-134217728}
UPLOAD_TIMEOUT: ${UPLOAD_TIMEOUT:-60s}
GOMEMLIMIT: ${API_GOMEMLIMIT:-400MiB}
services:
postgres:
image: postgres:17.2-alpine3.21
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
command: ["postgres", "-c", "max_connections=${PG_MAX_CONNECTIONS:-100}"]
secrets: [pg_password]
volumes:
- pgdata:/var/lib/postgresql/data
networks: [data]
shm_size: 256mb
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger -h 127.0.0.1"]
interval: 2s
timeout: 3s
retries: 30
deploy:
resources:
limits: {cpus: "${PG_CPUS:-2.0}", memory: "${PG_MEMORY:-1G}"}
stop_grace_period: 30s
restart: unless-stopped
valkey:
image: valkey/valkey:8.0.2-alpine3.21
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
volumes:
- ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro
secrets: [valkey_auth]
networks: [data]
healthcheck:
test: ["CMD-SHELL", "valkey-cli ping | grep -qE 'PONG|NOAUTH'"]
interval: 2s
timeout: 3s
retries: 30
deploy:
resources:
limits: {cpus: "0.5", memory: 128M}
restart: unless-stopped
# SeaweedFS (master + volume + filer + S3 gateway in one process). Only the S3
# port is used, and only inside the data network: nothing is published.
s3:
image: chrislusf/seaweedfs:3.80
command:
- server
- -dir=/data
- -s3
- -s3.port=8333
- -s3.config=/run/secrets/s3_identities
- -master.volumeSizeLimitMB=64
- -volume.max=100
secrets: [s3_identities]
volumes:
- s3data:/data
networks: [data]
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8333/healthz"]
interval: 2s
timeout: 3s
start_period: 60s
retries: 60
deploy:
resources:
# 512M was OOM-killed under 20 parallel 10 MiB PUTs (evidence/memory.json)
limits: {cpus: "1.0", memory: "${S3_MEMORY:-1G}"}
stop_grace_period: 20s
restart: unless-stopped
# Idempotent bucket bootstrap with the admin identity; the API never sees it.
s3-init:
image: mtusi-messenger:lab4
pull_policy: never
command: ["s3-init"]
healthcheck:
disable: true
environment:
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
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
secrets: [s3_admin_access_key_id, s3_admin_secret_access_key]
networks: [data]
depends_on:
s3:
condition: service_healthy
migrate:
condition: service_completed_successfully
deploy:
resources:
limits: {cpus: "0.5", memory: 64M}
restart: "no"
migrate:
build:
context: .
args:
APP_BUILD: ${APP_BUILD:-dev}
image: mtusi-messenger:lab4
command: ["migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets: [database_url]
networks: [data]
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits: {cpus: "0.5", memory: 128M}
restart: "no"
api:
image: mtusi-messenger:lab4
pull_policy: never
environment: *api-env
# No writable filesystem: originals cannot end up on the replica's disk.
read_only: true
tmpfs:
- /tmp:size=1m
secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key]
networks: [app, data]
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
postgres:
condition: service_healthy
valkey:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
interval: 2s
timeout: 2s
start_period: 5s
retries: 3
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: {cpus: "${API_CPUS:-1.0}", memory: "${API_MEMORY:-512M}"}
stop_grace_period: 15s
restart: unless-stopped
# Periodic DB<->S3 reconciliation (orphan objects, stale pending uploads,
# missing objects). Never touches anything younger than RECONCILE_GRACE.
reconciler:
image: mtusi-messenger:lab4
pull_policy: never
command: ["reconcile", "--apply", "--every", "${RECONCILE_EVERY:-10m}"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
DB_MAX_CONNS: "2"
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
RECONCILE_GRACE: ${RECONCILE_GRACE:-15m}
healthcheck:
disable: true
secrets: [database_url, s3_access_key_id, s3_secret_access_key]
networks: [data]
depends_on:
s3-init:
condition: service_completed_successfully
deploy:
resources:
limits: {cpus: "0.5", memory: 128M}
restart: unless-stopped
lb:
image: haproxy:3.0.5-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:${APP_PORT:-8080}:8080"
- "[::1]:${APP_PORT:-8080}:8080"
networks: [app]
depends_on:
api:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/healthz"]
interval: 2s
timeout: 2s
retries: 15
deploy:
resources:
limits: {cpus: "${LB_CPUS:-1.0}", memory: 128M}
restart: unless-stopped
networks:
app: {}
data: {}
volumes:
pgdata: {}
s3data: {}
secrets:
pg_password:
file: ./secrets/pg_password
database_url:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
valkey_auth:
file: ./secrets/valkey_auth.conf
s3_identities:
file: ./secrets/s3_identities.json
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
s3_admin_access_key_id:
file: ./secrets/s3_admin_access_key_id
s3_admin_secret_access_key:
file: ./secrets/s3_admin_secret_access_key