211 lines
5.7 KiB
YAML
211 lines
5.7 KiB
YAML
# Lab4 topology: nginx LB -> 2+ interchangeable API replicas -> shared
|
|
# PostgreSQL (metadata, source of truth) + Valkey (session cache) + a private
|
|
# S3-compatible store (SeaweedFS) for attachment bytes. No API replica or S3
|
|
# port is published, so the only reachable path is through the balancer.
|
|
# Secrets are file-based and live in ./secrets (gitignored). Schema migrations
|
|
# and the bucket bootstrap run in one-shot services before replicas start.
|
|
name: messenger-lab4
|
|
|
|
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
|
|
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
|
|
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
|
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
|
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:lab4
|
|
environment: *api-env
|
|
secrets:
|
|
- database_url
|
|
- cache_url
|
|
- s3_access_key_id
|
|
- s3_secret_access_key
|
|
expose:
|
|
- "8080"
|
|
depends_on:
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
s3-init:
|
|
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:lab4
|
|
command: ["python", "-m", "src.migrate"]
|
|
environment: *api-env
|
|
secrets:
|
|
- database_url
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
restart: "no"
|
|
|
|
# One-shot, idempotent bucket bootstrap with the root credentials.
|
|
s3-init:
|
|
build:
|
|
context: .
|
|
image: messenger-api:lab4
|
|
command: ["python", "-m", "src.s3_bootstrap"]
|
|
environment:
|
|
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:8333}
|
|
S3_REGION: ${S3_REGION:-us-east-1}
|
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
|
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key_id
|
|
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_access_key
|
|
secrets:
|
|
- s3_root_access_key_id
|
|
- s3_root_secret_access_key
|
|
depends_on:
|
|
s3:
|
|
condition: service_healthy
|
|
restart: "no"
|
|
|
|
s3:
|
|
image: chrislusf/seaweedfs:3.80
|
|
command:
|
|
- server
|
|
- -dir=/data
|
|
- -ip=0.0.0.0
|
|
- -s3
|
|
- -s3.port=8333
|
|
- -s3.config=/run/secrets/s3_config
|
|
volumes:
|
|
- s3data:/data
|
|
secrets:
|
|
- s3_config
|
|
expose:
|
|
- "8333"
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9333/cluster/status || exit 1"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 20
|
|
start_period: 15s
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "1.00"
|
|
memory: 512M
|
|
|
|
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:
|
|
s3data:
|
|
|
|
secrets:
|
|
database_url:
|
|
file: ./secrets/database_url
|
|
database_password:
|
|
file: ./secrets/database_password
|
|
cache_url:
|
|
file: ./secrets/cache_url
|
|
s3_config:
|
|
file: ./secrets/s3_config.json
|
|
s3_root_access_key_id:
|
|
file: ./secrets/s3_root_access_key_id
|
|
s3_root_secret_access_key:
|
|
file: ./secrets/s3_root_secret_access_key
|
|
s3_access_key_id:
|
|
file: ./secrets/s3_access_key_id
|
|
s3_secret_access_key:
|
|
file: ./secrets/s3_secret_access_key
|