forked from mtusi2026/mtusi2026lab2
145 lines
4.6 KiB
YAML
145 lines
4.6 KiB
YAML
# Учебный стенд lab2: API + PostgreSQL (источник истины) + Redis (кэш сессий).
|
|
# Запуск: scripts/init-secrets.sh && cp .env.example .env && docker compose --env-file .env up -d --build --wait
|
|
name: mtusi-lab2
|
|
|
|
x-api-common: &api-common
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
image: mtusi-lab2-api:local
|
|
environment: &api-env
|
|
APP_HOST: 0.0.0.0
|
|
APP_PORT: "8080"
|
|
APP_ENV: ${APP_ENV:-lab}
|
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
|
SESSION_STORE: postgres
|
|
SESSION_CACHE: ${SESSION_CACHE:-redis}
|
|
SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-60}
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
CACHE_URL_FILE: /run/secrets/cache_url
|
|
DB_POOL_MIN: "1"
|
|
DB_POOL_MAX: ${DB_POOL_MAX:-10}
|
|
DB_POOL_TIMEOUT_SECONDS: "2"
|
|
DB_STATEMENT_TIMEOUT_MS: "5000"
|
|
CACHE_TIMEOUT_SECONDS: "0.2"
|
|
REQUEST_TIMEOUT_SECONDS: "10"
|
|
MIGRATE_ON_START: "true"
|
|
secrets: [database_url, cache_url]
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_started # Redis — кэш: API стартует и работает и без него
|
|
networks: [frontend, backend]
|
|
read_only: true
|
|
tmpfs: [/tmp]
|
|
cap_drop: [ALL]
|
|
security_opt: ["no-new-privileges:true"]
|
|
restart: unless-stopped
|
|
stop_grace_period: 15s
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "1.0", memory: 512M}
|
|
|
|
services:
|
|
api:
|
|
<<: *api-common
|
|
ports: ["127.0.0.1:${API_PORT:-8080}:8080"]
|
|
|
|
# Отдельный экземпляр с коротким TTL только для проверки истечения сессий (make test-expiry).
|
|
api-ttl:
|
|
<<: *api-common
|
|
profiles: [ttl]
|
|
environment:
|
|
<<: *api-env
|
|
SESSION_TTL_SECONDS: "2"
|
|
ports: ["127.0.0.1:${API_TTL_PORT:-8081}:8080"]
|
|
|
|
postgres:
|
|
image: postgres:16.10-alpine
|
|
environment:
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_DB: messenger
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --data-checksums"
|
|
secrets: [postgres_password]
|
|
volumes:
|
|
- pgdata:/var/lib/postgresql/data
|
|
- ./backups:/backups # только для трека 5 (pg_dump/pg_restore)
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger -h 127.0.0.1"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
start_period: 10s
|
|
networks: [backend]
|
|
shm_size: 256m
|
|
restart: unless-stopped
|
|
stop_grace_period: 30s
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "2.0", memory: 1G}
|
|
|
|
# Redis — кэш сессий без persistence: после restart/flush кэш холодный, сессии остаются в PostgreSQL.
|
|
redis:
|
|
image: redis:7.4.2-alpine
|
|
command:
|
|
- sh
|
|
- -c
|
|
- |
|
|
umask 077
|
|
printf 'requirepass %s\n' "$$(cat /run/secrets/redis_password)" > /tmp/redis.conf
|
|
exec redis-server /tmp/redis.conf --save '' --appendonly no --maxmemory 64mb --maxmemory-policy allkeys-lru
|
|
user: redis # не root: entrypoint образа не понижает права для команды sh
|
|
secrets: [redis_password]
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"]
|
|
interval: 2s
|
|
timeout: 2s
|
|
retries: 15
|
|
networks: [backend]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: {cpus: "0.5", memory: 128M}
|
|
|
|
# «Новое хранилище» для эксперимента backup → restore (трек 5.2). Отдельный volume.
|
|
postgres-restore:
|
|
image: postgres:16.10-alpine
|
|
profiles: [research]
|
|
environment:
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_DB: restore_check
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --data-checksums"
|
|
secrets: [postgres_password]
|
|
volumes:
|
|
- pgrestore:/var/lib/postgresql/data
|
|
- ./backups:/backups:ro
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U messenger -d restore_check -h 127.0.0.1"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
networks: [backend]
|
|
shm_size: 256m
|
|
|
|
networks:
|
|
frontend: {}
|
|
backend:
|
|
internal: true # PostgreSQL и Redis недоступны с хоста и из внешней сети
|
|
|
|
volumes:
|
|
pgdata: {}
|
|
pgrestore: {}
|
|
|
|
secrets:
|
|
postgres_password:
|
|
file: ./secrets/postgres_password
|
|
redis_password:
|
|
file: ./secrets/redis_password
|
|
database_url:
|
|
file: ./secrets/database_url
|
|
cache_url:
|
|
file: ./secrets/cache_url
|