Files
mtusi2026lab3/compose.yaml
T
Mlvn b1972bcfd8 Реализовать lab3: HAProxy, API×N, идемпотентность и трек 1.
Горизонтальное масштабирование за LB с общими сессиями Redis, X-Instance-Id,
обязательным Idempotency-Key в PostgreSQL, сценариями failover/persistence и
измерением цены масштабирования при фиксированном CPU/RAM бюджете.
2026-11-06 21:07:00 +03:00

129 lines
3.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Локальный стенд lab3: Клиент → HAProxy → API×N → общие PostgreSQL и Redis.
# Публикуется только LB (:8080). Порты API наружу не публикуются.
# Запуск: bash scripts/up.sh (миграции однократно через compose run, затем --scale api=N)
# container_name не задаём: иначе --scale одного сервиса невозможен.
x-api-env: &api-env
APP_HOST: 0.0.0.0
APP_PORT: 8080
APP_ENV: ${APP_ENV:-lab}
APP_ACCESS_LOG: ${APP_ACCESS_LOG:-1}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
DB_MAX_CONNS: ${DB_MAX_CONNS:-5}
DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-messenger}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable
REDIS_URL: redis://redis:6379/0
services:
postgres:
image: postgres:16.6
environment:
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger}
POSTGRES_DB: ${POSTGRES_DB:-messenger}
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "127.0.0.1:5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}"]
interval: 5s
timeout: 3s
retries: 20
stop_grace_period: 10s
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
redis:
image: redis:7.4.1
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
volumes:
- redisdata:/data
ports:
- "127.0.0.1:6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 20
stop_grace_period: 10s
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
# Одноразовый мигратор. Не в depends_on API: иначе compose спотыкается о restart:no.
# Запускается явно из scripts/up.sh: docker compose run --rm migrate
migrate:
build: .
command: ["-migrate"]
environment:
<<: *api-env
depends_on:
postgres:
condition: service_healthy
restart: "no"
healthcheck:
disable: true
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
api:
build: .
environment:
<<: *api-env
expose:
- "8080"
depends_on:
redis:
condition: service_healthy
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "/server", "-healthcheck"]
interval: 2s
timeout: 2s
retries: 10
start_period: 15s
stop_grace_period: 10s
# no: иначе после docker stop в stop-one.sh демон может снова поднять реплику,
# и failover-тест увидит TARGET_INSTANCE_ID среди живых ответов.
restart: "no"
deploy:
resources:
limits:
cpus: ${API_CPUS:-0.50}
memory: ${API_MEMORY:-256M}
lb:
image: haproxy:2.9.14-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:8080:8080"
depends_on:
api:
condition: service_healthy
healthcheck:
test: ["CMD", "haproxy", "-c", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
interval: 10s
timeout: 3s
retries: 5
start_period: 5s
stop_grace_period: 10s
deploy:
resources:
limits:
cpus: "0.50"
memory: 128M
volumes:
pgdata:
redisdata: