lab3: балансировка, идемпотентность, отказ реплики, трек 1

- X-Instance-Id, обязательный Idempotency-Key (PostgreSQL, одна транзакция)
- HAProxy + 2..N реплик API, readiness/drain по SIGTERM, однократные миграции
- скрипты stop-one/restore/restart/rolling-update/collect-evidence
- трек 1: параметризованный стенд, cmd/bench, оркестратор и анализ
- workflow реального прогона Docker-стенда на GitHub Actions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-23 17:24:54 +03:00
co-authored by Claude Opus 5.5
parent 0ab3a341aa
commit 2e7a80be92
68 changed files with 4747 additions and 17 deletions
+131
View File
@@ -0,0 +1,131 @@
# Lab3: клиент → HAProxy → API × N → общие PostgreSQL (данные, идемпотентность) и Redis (сессии, кэш).
# Запуск: scripts/init-secrets.sh && docker compose --env-file .env up -d --build --wait
name: mtusi2026lab3
x-api-env: &api-env
APP_HOST: ${APP_HOST:-0.0.0.0}
APP_PORT: ${APP_PORT:-8080}
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_STORE: ${SESSION_STORE:-redis}
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
DRAIN_DELAY: ${DRAIN_DELAY:-3s}
SHUTDOWN_TIMEOUT: ${SHUTDOWN_TIMEOUT:-8s}
DB_POOL_MAX: ${DB_POOL_MAX:-20}
CACHE_INVALIDATION: ${CACHE_INVALIDATION:-version}
services:
lb:
image: haproxy:3.0.5-alpine
ports:
- "127.0.0.1:${LB_PORT:-8080}:8080"
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404"
environment:
LB_BALANCE: ${LB_BALANCE:-roundrobin}
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api:
condition: service_healthy
restart: unless-stopped
networks: [front]
deploy:
resources:
limits: { cpus: "${LB_CPUS:-0.50}", memory: 128M }
api:
build: .
image: mtusi2026lab3-api:local
# Без container_name и без ports: реплики доступны только через lb.
environment: *api-env
secrets: [database_url, cache_url]
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health/ready"]
interval: 5s
timeout: 2s
retries: 3
start_period: 5s
# DRAIN_DELAY + SHUTDOWN_TIMEOUT < stop_grace_period, иначе SIGKILL оборвёт запросы.
stop_grace_period: 15s
restart: unless-stopped
networks: [front, back]
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: { cpus: "${API_CPUS:-0.50}", memory: "${API_MEMORY:-256M}" }
# Однократные миграции до старта API: реплики не гоняются за схему.
migrate:
image: migrate/migrate:v4.17.0
volumes:
- ./migrations:/migrations:ro
secrets: [database_url]
entrypoint: ["/bin/sh", "-c", "exec migrate -path /migrations -database \"$$(cat /run/secrets/database_url)\" up"]
depends_on:
postgres:
condition: service_healthy
restart: "no"
networks: [back]
postgres:
image: postgres:16.4-alpine
command: ["postgres", "-c", "max_connections=${PG_MAX_CONNECTIONS:-100}"]
environment:
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
POSTGRES_DB: messenger
secrets: [postgres_password]
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 5s
timeout: 3s
retries: 5
restart: unless-stopped
networks: [back]
deploy:
resources:
limits: { cpus: "${PG_CPUS:-1.00}", memory: "${PG_MEMORY:-512M}" }
redis:
image: redis:7.4.1-alpine
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
restart: unless-stopped
networks: [back]
deploy:
resources:
limits: { cpus: "${REDIS_CPUS:-0.50}", memory: 256M }
networks:
# front: lb ↔ api; back: api ↔ хранилища. LB не видит БД.
front:
back:
secrets:
postgres_password:
file: ./secrets/postgres_password
database_url:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
volumes:
pg_data:
redis_data: