Files
mtusi2026lab3/compose.yaml
T

186 lines
6.0 KiB
YAML

# Local stand: HAProxy in front of interchangeable API replicas over shared
# PostgreSQL (users, chats, messages, idempotency keys) and Valkey (sessions).
# Only the balancer is published, on loopback. Credentials are files in
# ./secrets created by `make secrets`; named volumes keep the data across
# restarts, and only `make destroy` removes them.
#
# Two networks: edge joins the balancer and the API, data joins the API, the
# migrations and the stores and has no way out. The balancer cannot reach the
# stores, and the stores cannot reach anything outside.
name: lab3
services:
lb:
image: haproxy:3.4.7-alpine3.24@sha256:2caaf8b1a1ef67f3f1ea9b721f2821675d3ba4500fb518e23884fecb93e8dbaa
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
environment:
# 1 silences the log line of every successful request during load runs.
LB_QUIET: ${LB_QUIET:-0}
# One HAProxy thread per CPU of the limit below.
LB_THREADS: ${LB_THREADS:-1}
ports:
# Loopback only; override with PUBLISH=127.0.0.1:28080 when 8080 is busy.
- "${PUBLISH:-127.0.0.1:8080}:8080"
# Read-only statistics page: state and checks of every replica.
- "${STATS_PUBLISH:-127.0.0.1:8404}:8404"
networks:
- edge
healthcheck:
# Through the balancer itself: healthy once it can route to a ready
# replica, so `compose up --wait` means "ready through the entry point".
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/health/ready"]
interval: 5s
timeout: 3s
retries: 3
start_period: 20s
start_interval: 1s
deploy:
resources:
limits:
cpus: "${LB_CPUS:-0.5}"
memory: 128M
api:
build: .
image: mtusi-messenger:lab3
# No container_name and no ports: the service scales, and clients reach
# the replicas only through the balancer.
env_file:
- path: .env
required: false
environment:
DATABASE_URL_FILE: /run/secrets/database_url
SESSION_STORE_URL_FILE: /run/secrets/session_store_url
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
# Above the balancer's detection time of 1.5 s (haproxy.cfg).
APP_DRAIN_DELAY: ${APP_DRAIN_DELAY:-2s}
DB_MAX_CONNS: ${DB_MAX_CONNS:-10}
# About 90 % of the memory limit below; Go does not read the cgroup limit.
GOMEMLIMIT: ${API_GOMEMLIMIT:-230MiB}
# Empty disables the internal expvar listener; the benchmark sets :9090.
APP_DEBUG_ADDR: ${APP_DEBUG_ADDR:-}
secrets:
- database_url
- session_store_url
networks:
- edge
- data
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
valkey:
condition: service_healthy
# Drain delay 2 s + shutdown timeout 8 s fit before Docker sends SIGKILL.
stop_grace_period: 15s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "${API_CPUS:-1.0}"
memory: ${API_MEMORY:-256M}
# One-shot: applies the schema once before the replicas start, then exits.
migrate:
image: mtusi-messenger:lab3
entrypoint: ["/usr/local/bin/migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets:
- database_url
networks:
- data
depends_on:
postgres:
condition: service_healthy
restart: "no"
healthcheck:
# The image's check probes the API port, which migrate never opens.
disable: true
deploy:
resources:
limits:
cpus: "0.5"
memory: 128M
postgres:
image: postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873
# Ten replicas with ten connections each fit during a rolling update of
# five, plus migrate and psql sessions.
command: ["postgres", "-c", "max_connections=150"]
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
# Since PostgreSQL 18 the image keeps PGDATA in a versioned directory
# below /var/lib/postgresql, so the volume is mounted on the parent.
volumes:
- pgdata:/var/lib/postgresql
networks:
- data
healthcheck:
# Over TCP: during first initialisation the image runs a temporary
# server on the Unix socket only, which must not count as ready.
test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
# A clean shutdown writes a checkpoint instead of leaving crash recovery
# to the next start.
stop_grace_period: 30s
deploy:
resources:
limits:
cpus: "${PG_CPUS:-1.0}"
memory: ${PG_MEMORY:-512M}
valkey:
image: valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
secrets:
- valkey_auth
volumes:
- ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro
- valkeydata:/data
networks:
- data
healthcheck:
# valkey-cli exits 0 even on NOAUTH, so the reply itself is checked.
test: ["CMD-SHELL", "VALKEYCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" valkey-cli ping | grep -q PONG"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
stop_grace_period: 10s
deploy:
resources:
limits:
cpus: "0.5"
# maxmemory 256mb (valkey.conf) plus the server's own overhead.
memory: 320M
volumes:
pgdata:
valkeydata:
networks:
edge: {}
data:
internal: true
secrets:
database_url:
file: ./secrets/database_url
session_store_url:
file: ./secrets/session_store_url
postgres_password:
file: ./secrets/postgres_password
valkey_auth:
file: ./secrets/valkey_auth.conf