Реализовать lab3 на оценку 4: HAProxy, реплики API и общая идемпотентность.

This commit is contained in:
2026-10-30 15:27:54 +03:00
parent 0ab3a341aa
commit 5946fe214c
47 changed files with 3035 additions and 31 deletions
+147
View File
@@ -0,0 +1,147 @@
# Lab3: Client → HAProxy → API×N → shared PostgreSQL + Redis (sessions).
# Public entrypoint only via lb:8080. API replicas have no host ports.
services:
postgres:
image: postgres:16.6-bookworm
environment:
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
POSTGRES_DB: messenger
secrets:
- postgres_password
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
mem_limit: 512m
cpus: 0.50
networks:
- labnet
redis:
image: redis:7.4.2-bookworm
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
- --dir
- /data
volumes:
- redisdata:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
mem_limit: 128m
cpus: 0.25
networks:
- labnet
migrate:
build:
context: .
dockerfile: Dockerfile
image: mtusi-messenger:lab3
command: ["python", "-m", "messenger.migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
MIGRATIONS_DIR: /app/migrations
secrets:
- database_url
depends_on:
postgres:
condition: service_healthy
restart: "no"
mem_limit: 256m
cpus: 0.25
networks:
- labnet
api:
build:
context: .
dockerfile: Dockerfile
image: mtusi-messenger:lab3
environment:
APP_HOST: "0.0.0.0"
APP_PORT: "8080"
APP_ENV: lab
SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}"
IDEMPOTENCY_TTL_SECONDS: "${IDEMPOTENCY_TTL_SECONDS:-86400}"
SESSION_STORE: redis
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
MIGRATIONS_DIR: /app/migrations
secrets:
- database_url
- cache_url
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
# No host ports: only reachable via labnet / HAProxy.
expose:
- "8080"
healthcheck:
test:
[
"CMD",
"python",
"-c",
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)",
]
interval: 5s
timeout: 3s
retries: 12
start_period: 20s
stop_grace_period: 12s
mem_limit: 256m
cpus: 0.50
networks:
- labnet
lb:
image: haproxy:2.9.14-bookworm
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:8080:8080"
depends_on:
api:
condition: service_healthy
healthcheck:
# Image has no curl/wget; validate config + process. Public readiness is /health/ready via LB.
test: ["CMD", "haproxy", "-c", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
interval: 10s
timeout: 3s
retries: 5
start_period: 5s
mem_limit: 128m
cpus: 0.25
networks:
- labnet
secrets:
postgres_password:
file: ./secrets/postgres_password
database_url:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
volumes:
pgdata:
redisdata:
networks:
labnet:
driver: bridge