level 4, changed to HAProxy
This commit is contained in:
+87
-20
@@ -1,57 +1,103 @@
|
||||
# Client -> lb (nginx) -> api x API_REPLICAS -> postgres.
|
||||
# Secrets come from .env, which is not committed. Only the LB publishes a port.
|
||||
# Client -> lb (HAProxy) -> api x API_REPLICAS -> postgres.
|
||||
# Only the LB publishes a port. Networks: edge = lb<->api, data = api/migrate<->postgres (no LB, no internet).
|
||||
# The DB password comes from .env (not committed) as a Compose secret file, never as a container env var.
|
||||
name: messenger-lab3
|
||||
|
||||
x-app-env: &app-env
|
||||
x-db-client: &db-client
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
LOG_LEVEL: ${LOG_LEVEL:-info}
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:?set in .env}:${POSTGRES_PASSWORD:?set in .env}@postgres:5432/${POSTGRES_DB:?set in .env}
|
||||
DB_HOST: postgres
|
||||
DB_USER: ${POSTGRES_USER:?set in .env}
|
||||
DB_NAME: ${POSTGRES_DB:?set in .env}
|
||||
DB_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
|
||||
x-app-build: &app-build
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
APP_VERSION: ${APP_VERSION:-dev}
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.10-alpine@sha256:029660641a0cfc575b14f336ba448fb8a75fd595d42e1fa316b9fb4378742297
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
networks:
|
||||
- data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
|
||||
interval: 2s
|
||||
timeout: 5s
|
||||
retries: 30
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
reservations:
|
||||
cpus: "0.25"
|
||||
memory: 256M
|
||||
restart: unless-stopped
|
||||
|
||||
# One-shot schema step. Replicas never migrate on startup, so there is no race between them;
|
||||
# an advisory lock additionally serializes accidental parallel runs.
|
||||
migrate:
|
||||
image: messenger-lab3-app:local
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: messenger-lab3-app:${APP_VERSION:-dev}
|
||||
build: *app-build
|
||||
command: ["python", "-m", "app.migrate"]
|
||||
environment:
|
||||
<<: *app-env
|
||||
<<: *db-client
|
||||
secrets:
|
||||
- source: postgres_password
|
||||
uid: "10001"
|
||||
gid: "10001"
|
||||
mode: 0400
|
||||
networks:
|
||||
- data
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
image: messenger-lab3-app:local
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: messenger-lab3-app:${APP_VERSION:-dev}
|
||||
build: *app-build
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 256M
|
||||
reservations:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
environment:
|
||||
<<: *app-env
|
||||
<<: *db-client
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: "8080"
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-5}
|
||||
DRAIN_SECONDS: ${DRAIN_SECONDS:-4}
|
||||
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-3}
|
||||
secrets:
|
||||
- source: postgres_password
|
||||
uid: "10001"
|
||||
gid: "10001"
|
||||
mode: 0400
|
||||
networks:
|
||||
- edge
|
||||
- data
|
||||
expose:
|
||||
- "8080"
|
||||
depends_on:
|
||||
@@ -59,6 +105,7 @@ services:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
# Docker health gates startup order and rolling update; the LB runs its own checks (infra/haproxy/haproxy.cfg).
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
@@ -69,19 +116,22 @@ services:
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
start_period: 20s
|
||||
# SIGTERM -> uvicorn stops accepting, finishes in-flight requests within SHUTDOWN_TIMEOUT_SECONDS.
|
||||
# SIGTERM -> readiness 503 for DRAIN_SECONDS (LB drops the replica in <= 3 s) -> uvicorn finishes
|
||||
# in-flight requests within SHUTDOWN_TIMEOUT_SECONDS. 4 + 3 < 10, so SIGKILL is not reached.
|
||||
stop_signal: SIGTERM
|
||||
stop_grace_period: 8s
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
|
||||
lb:
|
||||
image: nginx:1.28.0-alpine@sha256:30f1c0d78e0ad60901648be663a710bdadf19e4c10ac6782c235200619158284
|
||||
image: haproxy:3.2.26-alpine@sha256:96ec3c75a9264ba94e733eeacb44051ed6e7b79bb77a73c2c36f236b6423f1f0
|
||||
# Loopback only. Both families: on Windows "localhost" tries ::1 first and waits ~2 s if nothing listens.
|
||||
ports:
|
||||
- "127.0.0.1:${LB_PORT:-8080}:8080"
|
||||
- "[::1]:${LB_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
networks:
|
||||
- edge
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
@@ -90,7 +140,24 @@ services:
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
reservations:
|
||||
cpus: "0.1"
|
||||
memory: 32M
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
edge: {}
|
||||
data:
|
||||
internal: true
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
environment: POSTGRES_PASSWORD
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
|
||||
Reference in New Issue
Block a user