Files
mtusi2026lab3/compose.yaml

121 lines
3.2 KiB
YAML

name: mtusi2026lab3
# One shared configuration for all API replicas.
x-api-env: &api-env
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD:-messenger_dev_password}@db:5432/messenger
SESSION_TTL_SECONDS: "3600"
IDEM_TTL_SECONDS: "86400"
APP_ENV: lab
services:
# ---------------------------------------------------------------------
# PostgreSQL — single source of truth, persistent volume
# ---------------------------------------------------------------------
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: messenger
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger_dev_password}
POSTGRES_DB: messenger
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 3s
timeout: 3s
retries: 20
restart: unless-stopped
# ---------------------------------------------------------------------
# One-shot migration runner — applied once before API replicas start
# ---------------------------------------------------------------------
migrate:
build: .
command: ["python", "-m", "app.migrate"]
environment:
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD:-messenger_dev_password}@db:5432/messenger
depends_on:
db:
condition: service_healthy
restart: "no"
# ---------------------------------------------------------------------
# API replicas — no published ports (traffic must go through the LB)
# ---------------------------------------------------------------------
api-1: &api-service
build: .
init: true
cpus: "0.50"
mem_limit: 256M
healthcheck:
test: ["CMD-SHELL", "python -c \"import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2); raise SystemExit(0 if r.status==200 else 1)\""]
interval: 5s
timeout: 3s
start_period: 10s
retries: 3
environment:
<<: *api-env
INSTANCE_ID: api-1
expose:
- "8080"
depends_on:
db:
condition: service_healthy
migrate:
condition: service_completed_successfully
restart: unless-stopped
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
api-2:
<<: *api-service
environment:
<<: *api-env
INSTANCE_ID: api-2
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
api-3:
<<: *api-service
environment:
<<: *api-env
INSTANCE_ID: api-3
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
api-4:
<<: *api-service
environment:
<<: *api-env
INSTANCE_ID: api-4
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
# ---------------------------------------------------------------------
# Load balancer — single public entry point on the host
# ---------------------------------------------------------------------
lb:
image: haproxy:2.9-alpine
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "8080:8080"
depends_on:
- api-1
- api-2
restart: unless-stopped
volumes:
pgdata: