# Lab 4 stand: client -> HAProxy (lb) -> API x N (one scalable service `api`) # -> PostgreSQL (metadata, source of truth) + Valkey (sessions) + SeaweedFS S3 (bytes). # From a clean clone (details and Windows notes: REPORT.md, docs/WINDOWS.md): # python scripts/init_secrets.py # once: random local credentials into ./secrets (git-ignored) # cp .env.example .env # optional overrides (API_REPLICAS, limits, ...) # docker compose --env-file .env up -d --build --wait # Order: postgres/valkey/s3 healthy -> migrate + s3-init (ONE run each, exit 0) -> api healthy -> lb. # Images are pinned by version AND digest. Passwords/keys are passed only as secret files, and # every service gets only the secrets it needs. Only the LB publishes ports (loopback only). # Three networks: `edge` (lb <-> api), `data` (api/migrate/reconciler <-> postgres/valkey) and # `storage` (api/s3-init/reconciler <-> s3); data and storage are internal (no route outside). name: mtusi-lab4 x-api-env: &api-env APP_ENV: ${APP_ENV:-lab} LOG_LEVEL: ${LOG_LEVEL:-info} SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} SESSION_STORE: ${SESSION_STORE:-valkey} 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_TIMEOUT_SECONDS: ${DB_TIMEOUT_SECONDS:-3} CACHE_TIMEOUT_SECONDS: ${CACHE_TIMEOUT_SECONDS:-0.5} PASSWORD_HASH_CONCURRENCY: ${PASSWORD_HASH_CONCURRENCY:-2} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} # Graceful stop: 3 s drain (readiness 503, still serving) + up to 8 s for in-flight requests. DRAIN_SECONDS: ${DRAIN_SECONDS:-3} SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-8} DOCS_ENABLED: ${DOCS_ENABLED:-true} MESSAGE_WRITE_PATH: ${MESSAGE_WRITE_PATH:-function} # Lab 4: S3 (the API's own Read+Write key pair, see infra/seaweedfs/start.sh) and upload limits. 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_SECONDS: ${S3_TIMEOUT_SECONDS:-3} MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} UPLOAD_CONCURRENCY: ${UPLOAD_CONCURRENCY:-4} UPLOAD_QUEUE_TIMEOUT_SECONDS: ${UPLOAD_QUEUE_TIMEOUT_SECONDS:-5} UPLOAD_READ_TIMEOUT_SECONDS: ${UPLOAD_READ_TIMEOUT_SECONDS:-30} IMAGE_VERIFY_TIMEOUT_SECONDS: ${IMAGE_VERIFY_TIMEOUT_SECONDS:-10} JPEG_MAX_MEMORY_MB: ${JPEG_MAX_MEMORY_MB:-256} PENDING_GRACE_SECONDS: ${PENDING_GRACE_SECONDS:-900} JANITOR_INTERVAL_SECONDS: ${JANITOR_INTERVAL_SECONDS:-60} # Level 4 failure experiments only (compose.faults.yaml); empty = no injected failures. FAULT_POINTS: ${FAULT_POINTS:-} services: postgres: image: postgres:18.6-bookworm@sha256:afc7e2d441324c0388fa80c3d24f733b4194a4eb7f47dd8ee2b08eb1a24a647c environment: POSTGRES_USER: messenger POSTGRES_DB: messenger POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password # pg_stat_statements: per-statement counters for the experiments. command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "max_connections=${PG_MAX_CONNECTIONS:-100}"] secrets: [postgres_password] volumes: - pgdata:/var/lib/postgresql # PostgreSQL 18 image keeps PGDATA under this path healthcheck: test: ["CMD-SHELL", "pg_isready -U messenger -d messenger -q"] interval: 2s timeout: 3s retries: 30 start_period: 10s stop_grace_period: 30s # clean shutdown checkpoint instead of crash recovery restart: unless-stopped networks: [data] deploy: resources: limits: {cpus: "${PG_CPUS:-2.0}", memory: "${PG_MEMORY:-1g}"} valkey: image: valkey/valkey:8.1.3-bookworm@sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571 # The password goes into a config include readable only by the valkey user, # so it never appears in the process arguments. docker-entrypoint.sh drops root. command: - sh - -c - >- umask 077 && printf 'requirepass %s\n' "$$(cat /run/secrets/valkey_password)" > /tmp/requirepass.conf && chown valkey /tmp/requirepass.conf && exec docker-entrypoint.sh valkey-server /etc/valkey/valkey.conf secrets: [valkey_password] volumes: - ./infra/valkey/valkey.conf:/etc/valkey/valkey.conf:ro - valkeydata:/data healthcheck: test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/valkey_password)\" valkey-cli ping | grep -q PONG"] interval: 2s timeout: 3s retries: 30 start_period: 5s restart: unless-stopped networks: [data] deploy: resources: limits: {cpus: "${VALKEY_CPUS:-0.5}", memory: 256m} # Lab 4: S3-compatible object storage (SeaweedFS: master + volume + filer + S3 gateway in one # process). Bytes of the attachments live ONLY here, on the named volume s3data. Identities # and exposure: infra/seaweedfs/start.sh. Not published on the host; its web UIs listen on # 127.0.0.1 inside the container only. s3: image: chrislusf/seaweedfs:4.46@sha256:08d516132314207d10c8e37cbffc1f32b147d870169688734cc61c6231625b62 entrypoint: ["/bin/sh", "/etc/seaweedfs/start.sh"] environment: S3_BUCKET: ${S3_BUCKET:-messenger-attachments} secrets: - s3_admin_access_key_id - s3_admin_secret_access_key - s3_api_access_key_id - s3_api_secret_access_key - s3_reconciler_access_key_id - s3_reconciler_secret_access_key volumes: - ./infra/seaweedfs/start.sh:/etc/seaweedfs/start.sh:ro - s3data:/data healthcheck: # Unauthenticated health endpoint of the S3 gateway (200 once it serves requests). test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8333/healthz"] interval: 2s timeout: 3s retries: 30 start_period: 10s stop_grace_period: 30s restart: unless-stopped networks: [storage] deploy: resources: limits: {cpus: "${S3_CPUS:-1.0}", memory: "${S3_MEMORY:-512m}"} # One-shot schema migration: runs ONCE per `up`, before any API replica starts # (api depends_on service_completed_successfully). Replicas never migrate themselves. migrate: build: context: . args: BUILD_REVISION: ${BUILD_REVISION:-dev} image: ${API_IMAGE:-mtusi-messenger:lab4} command: ["python", "-m", "messenger.migrate"] environment: DATABASE_URL_FILE: /run/secrets/database_url secrets: [database_url] depends_on: postgres: condition: service_healthy healthcheck: disable: true restart: "no" networks: [data] deploy: resources: limits: {cpus: "0.5", memory: 256m} # One-shot, idempotent S3 bootstrap: bucket, sentinel object, verification of the access model # (private bucket, least-privilege keys). The ONLY holder of the admin key pair besides s3. s3-init: image: ${API_IMAGE:-mtusi-messenger:lab4} command: ["python", "-m", "messenger.s3_bootstrap"] 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 S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key S3_RECONCILER_ACCESS_KEY_ID_FILE: /run/secrets/s3_reconciler_access_key_id S3_RECONCILER_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_reconciler_secret_access_key secrets: - s3_admin_access_key_id - s3_admin_secret_access_key - s3_api_access_key_id - s3_api_secret_access_key - s3_reconciler_access_key_id - s3_reconciler_secret_access_key depends_on: s3: condition: service_healthy migrate: condition: service_completed_successfully # same image: built once by `migrate` healthcheck: disable: true restart: "no" networks: [storage] deploy: resources: limits: {cpus: "0.5", memory: 256m} api: build: context: . args: BUILD_REVISION: ${BUILD_REVISION:-dev} image: ${API_IMAGE:-mtusi-messenger:lab4} environment: *api-env secrets: - database_url - cache_url - source: s3_api_access_key_id target: s3_access_key_id - source: s3_api_secret_access_key target: s3_secret_access_key # No `ports` and no container_name: replicas are reachable only through `lb`, # and `--scale api=N` / deploy.replicas works. X-Instance-Id = container hostname. expose: ["8080"] healthcheck: test: ["CMD", "python", "-m", "messenger.healthcheck"] interval: 2s timeout: 2s retries: 3 start_period: 5s depends_on: postgres: condition: service_healthy valkey: condition: service_healthy s3: condition: service_healthy migrate: condition: service_completed_successfully s3-init: condition: service_completed_successfully stop_grace_period: 15s # > DRAIN_SECONDS + SHUTDOWN_TIMEOUT_SECONDS (11 s) restart: unless-stopped networks: [edge, data, storage] deploy: replicas: ${API_REPLICAS:-2} resources: # 512m: UPLOAD_CONCURRENCY (4) x 10 MiB bodies + one djpeg (<= JPEG_MAX_MEMORY_MB) + runtime; # measured in evidence/memory/ (bench/upload_memory.py). limits: {cpus: "${API_CPUS:-1.0}", memory: "${API_MEMORY:-512m}"} lb: image: haproxy:3.2.26-alpine@sha256:254e6370f01cd654e0744edf607c67f33777c1b6a9dc0d4b28bcba7171460122 volumes: - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro ports: # The ONLY public entry point, on the loopback interfaces only. Both IPv4 and IPv6: # on Windows `localhost` resolves to ::1 first, and a refused ::1 connection costs ~2 s # per request before the fallback to 127.0.0.1 (measured in lab 3: 2.0 s vs 0.007 s). - "127.0.0.1:${APP_PUBLISH_PORT:-8080}:8080" - "[::1]:${APP_PUBLISH_PORT:-8080}:8080" - "127.0.0.1:${LB_STATS_PORT:-8404}:8404" # local status page (UP/DOWN of replicas) healthcheck: # Config is valid and the process answers; whether any API is ready is the LB's own job. test: ["CMD-SHELL", "haproxy -c -q -f /usr/local/etc/haproxy/haproxy.cfg && wget -q -O /dev/null http://127.0.0.1:8404/"] interval: 5s timeout: 3s retries: 3 depends_on: api: condition: service_healthy restart: unless-stopped networks: [edge] deploy: resources: limits: {cpus: "${LB_CPUS:-1.0}", memory: 128m} # Track 2 (lab 4): reconciler of S3 objects vs PostgreSQL metadata. Not a daemon: started on # demand, dry-run by default (read-only on both stores): # docker compose run --rm reconciler # report only # docker compose run --rm reconciler --apply # fix: orphans, stale intents, quarantine reconciler: profiles: ["tools"] image: ${API_IMAGE:-mtusi-messenger:lab4} entrypoint: ["python", "-m", "messenger.reconcile"] command: [] environment: DATABASE_URL_FILE: /run/secrets/database_url DB_TIMEOUT_SECONDS: ${RECONCILE_DB_TIMEOUT_SECONDS:-10} 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_SECONDS: ${S3_TIMEOUT_SECONDS:-3} secrets: - database_url - source: s3_reconciler_access_key_id target: s3_access_key_id - source: s3_reconciler_secret_access_key target: s3_secret_access_key depends_on: postgres: condition: service_healthy s3: condition: service_healthy restart: "no" networks: [data, storage] deploy: resources: limits: {cpus: "${RECONCILER_CPUS:-1.0}", memory: 256m} networks: edge: data: internal: true # no route outside: stores talk only to api/migrate storage: internal: true # S3 API reachable only by api, s3-init, reconciler volumes: pgdata: valkeydata: s3data: secrets: postgres_password: file: ./secrets/postgres_password valkey_password: file: ./secrets/valkey_password database_url: file: ./secrets/database_url cache_url: file: ./secrets/cache_url s3_admin_access_key_id: file: ./secrets/s3_admin_access_key_id s3_admin_secret_access_key: file: ./secrets/s3_admin_secret_access_key s3_api_access_key_id: file: ./secrets/s3_api_access_key_id s3_api_secret_access_key: file: ./secrets/s3_api_secret_access_key s3_reconciler_access_key_id: file: ./secrets/s3_reconciler_access_key_id s3_reconciler_secret_access_key: file: ./secrets/s3_reconciler_secret_access_key