forked from mtusi2026/mtusi2026lab3
84 lines
2.4 KiB
YAML
84 lines
2.4 KiB
YAML
x-app-env: &app-env
|
|
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-messenger_db}
|
|
REDIS_URL: redis://valkey:6379/0
|
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
|
SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-30}
|
|
DB_POOL_SIZE: ${DB_POOL_SIZE:-10}
|
|
DB_MAX_OVERFLOW: ${DB_MAX_OVERFLOW:-10}
|
|
|
|
services:
|
|
db:
|
|
image: postgres:16-alpine
|
|
environment:
|
|
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
|
|
POSTGRES_DB: ${POSTGRES_DB:-messenger_db}
|
|
volumes:
|
|
- postgres_data:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 5
|
|
|
|
valkey:
|
|
image: valkey/valkey:8.0-alpine
|
|
# Кэш сессий: источник истины - PostgreSQL, persistence не нужен.
|
|
command: ["valkey-server", "--save", "", "--appendonly", "no",
|
|
"--maxmemory", "64mb", "--maxmemory-policy", "allkeys-lru"]
|
|
healthcheck:
|
|
test: ["CMD", "valkey-cli", "ping"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 5
|
|
|
|
# Миграции выполняются один раз, до старта любой реплики API
|
|
migrate:
|
|
build: .
|
|
image: messenger-api:lab3
|
|
command: ["alembic", "upgrade", "head"]
|
|
environment: *app-env
|
|
depends_on:
|
|
db:
|
|
condition: service_healthy
|
|
restart: "no"
|
|
|
|
app:
|
|
build: .
|
|
image: messenger-api:lab3
|
|
environment: *app-env
|
|
expose:
|
|
- "8080" # порт наружу не публикуем: вход только через lb
|
|
deploy:
|
|
replicas: 2
|
|
resources:
|
|
limits:
|
|
cpus: "${APP_CPUS:-0.50}"
|
|
memory: 256M
|
|
stop_grace_period: 15s
|
|
depends_on:
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
valkey:
|
|
condition: service_started
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "curl", "-f", "http://localhost:8080/health/ready"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 3
|
|
|
|
lb:
|
|
image: haproxy:2.9-alpine
|
|
ports:
|
|
- "8080:8080"
|
|
volumes:
|
|
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
depends_on:
|
|
app:
|
|
condition: service_healthy
|
|
restart: unless-stopped
|
|
|
|
volumes:
|
|
postgres_data:
|