159 lines
4.4 KiB
YAML
159 lines
4.4 KiB
YAML
# Local stand: HAProxy in front of interchangeable API replicas over shared
|
|
# PostgreSQL and Valkey. Only the balancer is published, on loopback; the data
|
|
# network has no way out. The load-test knobs are in compose.bench.yaml.
|
|
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
|
|
ports:
|
|
- "${PUBLISH:-127.0.0.1:8080}:8080"
|
|
- "${STATS_PUBLISH:-127.0.0.1:8404}:8404"
|
|
networks:
|
|
- edge
|
|
healthcheck:
|
|
# Through the balancer: `up --wait` returns once a replica is reachable.
|
|
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: "0.5"
|
|
memory: 128M
|
|
|
|
api:
|
|
build: .
|
|
image: mtusi-messenger:lab3
|
|
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}
|
|
# Go does not read the cgroup memory limit.
|
|
GOMEMLIMIT: 230MiB
|
|
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 plus shutdown timeout 8 s, before SIGKILL.
|
|
stop_grace_period: 15s
|
|
deploy:
|
|
replicas: ${API_REPLICAS:-2}
|
|
resources:
|
|
limits:
|
|
cpus: "1.0"
|
|
memory: 256M
|
|
|
|
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
|
|
# 10 balancer slots x DB_MAX_CONNS 10, plus migrate and psql.
|
|
command: ["postgres", "-c", "max_connections=150"]
|
|
environment:
|
|
POSTGRES_DB: messenger
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
secrets:
|
|
- postgres_password
|
|
# PostgreSQL 18 keeps PGDATA in a versioned directory below this one.
|
|
volumes:
|
|
- pgdata:/var/lib/postgresql
|
|
networks:
|
|
- data
|
|
healthcheck:
|
|
# Over TCP: the first initialisation runs a temporary socket-only server.
|
|
test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
start_period: 10s
|
|
stop_grace_period: 30s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "1.0"
|
|
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
|