Files
mtusi2026lab3/compose.yaml
T
vika 59f8c55f93 Поставить несколько реплик API за общий балансировщик.
Сессия и ключ идемпотентности общие для реплик, миграция выполняется один раз, остановка одной реплики не теряет подтверждённые сообщения.
2026-10-30 01:15:28 +03:00

170 lines
4.2 KiB
YAML

# Local stand for lab3. One compose service "api" is scaled; it has no container_name
# and publishes no host port. Clients enter only through lb.
# Volumes survive scripts/restart.sh. scripts/stop-one.sh stops a single api container.
name: mtusi-lab3
x-logging: &logging
driver: json-file
options:
max-size: "10m"
max-file: "2"
x-api-image: &api-image mtusi-messenger:lab3
services:
postgres:
image: postgres:16.8-bookworm
environment:
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
POSTGRES_DB: ${POSTGRES_DB:-messenger}
command:
- postgres
- -c
- synchronous_commit=on
- -c
- fsync=on
- -c
- full_page_writes=on
- -c
- max_connections=100
- -c
- shared_buffers=128MB
volumes:
- pg-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 5s
timeout: 3s
retries: 20
start_period: 10s
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
logging: *logging
networks: [lab]
restart: unless-stopped
redis:
image: redis:7.4.2-alpine
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD:?set REDIS_PASSWORD in .env}
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- always
- --dir
- /data
- --requirepass
- ${REDIS_PASSWORD}
volumes:
- redis-data:/data
healthcheck:
test: ["CMD-SHELL", "redis-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep -q PONG"]
interval: 5s
timeout: 3s
retries: 20
start_period: 5s
deploy:
resources:
limits:
cpus: "0.25"
memory: 128M
logging: *logging
networks: [lab]
restart: unless-stopped
migrate:
image: *api-image
build: .
command: ["python", "/app/src/migrate.py"]
environment:
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
logging: *logging
networks: [lab]
restart: "no"
api:
image: *api-image
build: .
environment:
APP_HOST: 0.0.0.0
APP_PORT: "8080"
DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger
REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
MAX_MESSAGES: ${MAX_MESSAGES:-100000}
MAX_PER_SECOND: ${MAX_PER_SECOND:-10000}
MAX_INFLIGHT_BODIES: ${MAX_INFLIGHT_BODIES:-32}
POOL_MAX: ${POOL_MAX:-4}
CAP_MODE: ${CAP_MODE:-off}
DRAIN_SECONDS: ${DRAIN_SECONDS:-2}
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()"]
interval: 2s
timeout: 2s
retries: 10
start_period: 20s
stop_grace_period: 8s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "${API_CPUS:-0.50}"
memory: ${API_MEMORY:-512M}
logging: *logging
networks: [lab]
restart: unless-stopped
lb:
image: haproxy:2.8.14-alpine
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:${APP_PORT:-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: 3
start_period: 5s
deploy:
resources:
limits:
cpus: "0.50"
memory: 128M
logging: *logging
networks: [lab]
restart: unless-stopped
networks:
lab:
name: mtusi-lab3-net
volumes:
pg-data:
redis-data: