Files
mtusi2026lab4/compose.yaml
T
2026-10-21 21:22:42 +03:00

174 lines
5.8 KiB
YAML

# Lab3 stand: two API replicas behind a HAProxy load balancer.
#
# cp -n .env.example .env
# docker compose --env-file .env up -d --build
# curl http://localhost:8080/health/ready
#
# Only the load balancer is published (127.0.0.1). Stores and API replicas are
# reachable from the host via `docker compose exec` / `docker compose logs`.
# No container_name anywhere: replica hostnames stay the short container IDs,
# which the application reports back as X-Instance-Id.
name: mtusi-messenger-lab3
x-app: &app
build:
context: .
dockerfile: Dockerfile
image: mtusi-messenger:lab3
services:
postgres:
image: postgres:18-alpine@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873
environment:
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
POSTGRES_DB: ${POSTGRES_DB:-messenger}
volumes:
- postgres18-data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
start_period: 5s
deploy:
resources:
limits:
# Saturation point of the stand (see REPORT.md, track 1): the database
# runs at its quota while API replicas idle. Raise this knob to give the
# shared dependency more of the host CPU.
cpus: "${POSTGRES_CPU_LIMIT:-0.50}"
memory: ${POSTGRES_MEM_LIMIT:-384M}
restart: unless-stopped
redis:
image: redis:7.4-alpine
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
- --save
- "60"
- "1"
volumes:
- redis7-data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 3s
deploy:
resources:
limits:
cpus: "0.25"
memory: 192M
restart: unless-stopped
# One-shot schema bootstrap; runs once to completion before any API replica.
migrate:
<<: *app
entrypoint: ["/usr/local/bin/migrate"]
command: ["up"]
environment:
APP_CONFIG: /etc/messenger/dev/config.yaml
POSTGRES_HOST: postgres
POSTGRES_PORT: "5432"
POSTGRES_DATABASE: ${POSTGRES_DB:-messenger}
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
POSTGRES_SSL_MODE: disable
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
# One-shot: healthcheck из образа (проверка /health/live) для него бессмыслен.
healthcheck:
disable: true
restart: "no"
api:
<<: *app
# Общий сетевой alias: LB резолвит одно имя в адреса всех реплик, поэтому
# масштабирование и замена контейнеров не зависят от их номеров/ID.
networks:
default:
aliases:
- api-replica
env_file:
- path: .env
required: false
# Explicit values win over localhost defaults coming from .env (see .env.example).
environment:
APP_CONFIG: /etc/messenger/dev/config.yaml
POSTGRES_HOST: postgres
POSTGRES_PORT: "5432"
POSTGRES_DATABASE: ${POSTGRES_DB:-messenger}
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
POSTGRES_SSL_MODE: disable
REDIS_ENABLED: "true"
REDIS_HOST: redis
REDIS_PORT: "6379"
CACHE_ENABLED: "true"
# Размер пула соединений ОДНОЙ реплики; общий расход = реплики × значение.
# Это один из общих ресурсов, который масштабирование расходует первым.
POSTGRES_MAX_CONNS: "${POSTGRES_MAX_CONNS:-10}"
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "${API_CPU_LIMIT:-0.50}"
memory: ${API_MEM_LIMIT:-256M}
# The app drains on SIGTERM within SERVER_SHUTDOWN_TIMEOUT; keep the grace
# period above it so Docker does not SIGKILL an in-flight request.
stop_grace_period: 15s
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
migrate:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
interval: 10s
# Готовность проверяет PostgreSQL (критичную зависимость; кэш деградирует
# безопасно): под насыщающей нагрузкой ответ может подойти к секундам, и
# слишком строгий таймаут помечает занятую, но работающую реплику как
# unhealthy.
timeout: 5s
retries: 6
start_period: 5s
restart: unless-stopped
# Public entry point. Active /health/ready probes remove a not-ready replica from
# rotation entirely; infra/lb/haproxy.cfg holds the upstream, checks and retries.
lb:
image: haproxy:3.2-alpine
volumes:
- ./infra/lb/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:${SERVER_PORT:-8080}:80"
# Operator-only stats page on loopback: shows which replica is UP/DOWN and why.
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404"
depends_on:
api:
condition: service_healthy
deploy:
resources:
limits:
cpus: "${LB_CPU_LIMIT:-0.25}"
memory: 64M
restart: unless-stopped
volumes:
postgres18-data:
redis7-data: