From fd54119d3fbd436229d14bcd40d64731086ed058 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 7 Nov 2026 12:46:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=80=D0=B5=D0=BF=D0=BB=D0=B8=D0=BA=D0=B8=20API?= =?UTF-8?q?=20=D0=B7=D0=B0=20HAProxy=20=D0=B8=20=D0=BE=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=BE=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=BD=D0=B8=D1=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Внешний порт остаётся только у балансировщика, миграции применяются один раз, а stop-one оставляет выбранную реплику остановленной. --- .env.example | 16 ++++- compose.yaml | 131 ++++++++++++++++++++++++++++++++++++++ infra/haproxy.cfg | 35 ++++++++++ scripts/restart.bat | 3 + scripts/restart.py | 20 ++++++ scripts/restart.sh | 10 +++ scripts/rolling-update.sh | 55 ++++++++++++++++ scripts/stop-one.sh | 32 ++++++++++ 8 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 compose.yaml create mode 100644 infra/haproxy.cfg create mode 100644 scripts/restart.bat create mode 100644 scripts/restart.py create mode 100755 scripts/restart.sh create mode 100755 scripts/rolling-update.sh create mode 100755 scripts/stop-one.sh diff --git a/.env.example b/.env.example index d32b9be0..f6739e98 100644 --- a/.env.example +++ b/.env.example @@ -3,11 +3,25 @@ APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +LOG_LEVEL=info +SHUTDOWN_GRACE_SECONDS=10 SESSION_TTL_SECONDS=3600 +CACHE_TTL_SECONDS=60 # Choose credentials locally. File paths contain no secret values. DATABASE_URL_FILE=/run/secrets/database_url # Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres +SESSION_STORE=redis CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 +API_CPUS=0.50 +API_MEM=256m +POSTGRES_CPUS=1.0 +POSTGRES_MEM=512m +DB_POOL_MAX=4 +LB_DRAIN_SECONDS=2 IDEMPOTENCY_TTL_SECONDS=86400 + +# Compose interpolates these. Synthetic local values only; replace after copy. +POSTGRES_PASSWORD=lab2-dev-only-change-me +DATABASE_URL=postgresql://messenger:lab2-dev-only-change-me@postgres:5432/messenger +REDIS_URL=redis://redis:6379/0 diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..a1ee4f8c --- /dev/null +++ b/compose.yaml @@ -0,0 +1,131 @@ +# Client -> lb:8080 -> api (scale) -> shared Postgres and Redis. +# API replicas do not publish host ports. Migrations run once in `migrate`. +# Check period: HAProxy inter 1s, fall 2, rise 1. Worst detection of a dead +# or not-ready replica is about 2s, plus the 1s DNS hold. API on SIGTERM +# fails /health/ready immediately and exits after LB_DRAIN_SECONDS (default 2). + +services: + migrate: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab3 + command: ["python", "-m", "app.migrate"] + environment: + APP_ENV: lab + LOG_LEVEL: info + DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger + DB_POOL_MIN: "1" + DB_POOL_MAX: "2" + MIGRATIONS_ON_START: "1" + depends_on: + postgres: + condition: service_healthy + restart: "no" + mem_limit: 256m + cpus: "0.50" + + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab3 + environment: + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + APP_ENV: lab + LOG_LEVEL: info + SHUTDOWN_GRACE_SECONDS: "3" + LB_DRAIN_SECONDS: ${LB_DRAIN_SECONDS:-2} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + CACHE_TTL_SECONDS: ${CACHE_TTL_SECONDS:-60} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + SESSION_STORE: redis + DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger + REDIS_URL: redis://redis:6379/0 + DB_POOL_MIN: "1" + DB_POOL_MAX: ${DB_POOL_MAX:-4} + DB_COMMAND_TIMEOUT: "5" + DB_CONNECT_TIMEOUT: "5" + MIGRATIONS_ON_START: "0" + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + redis: + condition: service_healthy + expose: + - "8080" + restart: unless-stopped + mem_limit: ${API_MEM:-256m} + cpus: ${API_CPUS:-0.50} + stop_grace_period: 8s + 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: 6 + start_period: 5s + + lb: + image: haproxy:2.8.14-alpine + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + ports: + - "127.0.0.1:8080:8080" + depends_on: + api: + condition: service_started + restart: unless-stopped + mem_limit: 128m + cpus: "0.25" + + postgres: + image: postgres:16.6-alpine + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: messenger + volumes: + - pg_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 3s + timeout: 3s + retries: 20 + restart: unless-stopped + mem_limit: ${POSTGRES_MEM:-512m} + cpus: ${POSTGRES_CPUS:-1.0} + + redis: + image: redis:7.4.2-alpine + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + - --save + - "60" + - "1" + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 20 + restart: unless-stopped + mem_limit: 128m + cpus: "0.50" + +volumes: + pg_data: + redis_data: diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..24c99b80 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,35 @@ +global + log stdout format raw local0 info + maxconn 200 + +defaults + log global + mode http + option httplog + timeout connect 2s + timeout client 10s + timeout server 10s + timeout http-request 5s + timeout check 2s + timeout queue 2s + retries 1 + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 1s + accepted_payload_size 8192 + +frontend fe_http + bind *:8080 + default_backend be_api + +backend be_api + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr host api + http-check expect status 200 + default-server inter 1s fall 2 rise 1 + server-template srv 8 api:8080 check resolvers docker init-addr none resolve-prefer ipv4 diff --git a/scripts/restart.bat b/scripts/restart.bat new file mode 100644 index 00000000..d96ae9d7 --- /dev/null +++ b/scripts/restart.bat @@ -0,0 +1,3 @@ +@echo off +python "%~dp0restart.py" +exit /b %ERRORLEVEL% diff --git a/scripts/restart.py b/scripts/restart.py new file mode 100644 index 00000000..639651e8 --- /dev/null +++ b/scripts/restart.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +"""Restart API, PostgreSQL and Redis without removing named volumes.""" +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] + + +def main() -> int: + env_file = ROOT / ".env" + cmd = ["docker", "compose"] + if env_file.is_file(): + cmd.extend(["--env-file", str(env_file)]) + cmd.extend(["restart", "postgres", "redis", "api"]) + return subprocess.run(cmd, cwd=ROOT).returncode + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..6a3be3ad --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,10 @@ +#!/bin/sh +# Restart API replicas and every store they use. Volumes are kept. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$ROOT" +if [ -f .env ]; then + docker compose --env-file .env restart postgres redis api +else + docker compose restart postgres redis api +fi diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..d22d4a10 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# Replace API replicas one at a time. The other replica keeps serving. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$ROOT" + +compose() { + if [ -f .env ]; then + docker compose --env-file .env "$@" + else + docker compose "$@" + fi +} + +if command -v python3 >/dev/null 2>&1; then + PY=python3 +else + PY=python +fi + +ids=$(compose ps -q api) +count=0 +for id in $ids; do + count=$((count + 1)) +done +if [ "$count" -lt 2 ]; then + echo "rolling update needs at least 2 running API replicas" >&2 + exit 1 +fi + +for id in $ids; do + echo "replacing $(docker inspect -f '{{.Config.Hostname}}' "$id")" + docker stop -t 8 "$id" >/dev/null + docker rm "$id" >/dev/null + compose up -d --scale "api=$count" --no-deps --no-recreate api >/dev/null + ready=0 + i=0 + while [ "$i" -lt 40 ]; do + running=0 + for cid in $(compose ps -q api); do + running=$((running + 1)) + done + if [ "$running" -ge "$count" ] && "$PY" -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" >/dev/null 2>&1; then + ready=1 + break + fi + i=$((i + 1)) + sleep 1 + done + if [ "$ready" -ne 1 ]; then + echo "replacement did not become ready" >&2 + exit 1 + fi +done +echo "rolling update finished; replicas=$count" diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..707e422d --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Stop the single API replica whose X-Instance-Id is TARGET_INSTANCE_ID. +# Leaves it stopped. Does not touch the database, Redis, the load balancer, or volumes. +set -eu +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$ROOT" + +compose() { + if [ -f .env ]; then + docker compose --env-file .env "$@" + else + docker compose "$@" + fi +} + +found=0 +for id in $(compose ps -q api); do + iid=$(docker exec "$id" cat /tmp/instance-id 2>/dev/null | tr -d '\r\n' || true) + host=$(docker inspect -f '{{.Config.Hostname}}' "$id") + if [ "$iid" = "$TARGET_INSTANCE_ID" ] || [ "$host" = "$TARGET_INSTANCE_ID" ]; then + docker stop -t 5 "$id" >/dev/null + found=1 + break + fi +done + +if [ "$found" -ne 1 ]; then + echo "no running API replica matched TARGET_INSTANCE_ID" >&2 + exit 1 +fi