Поставить несколько реплик API за общий балансировщик.

Сессия и ключ идемпотентности общие для реплик, миграция выполняется один раз, остановка одной реплики не теряет подтверждённые сообщения.
This commit is contained in:
2026-10-30 01:15:28 +03:00
parent 0ab3a341aa
commit 59f8c55f93
45 changed files with 3367 additions and 36 deletions
+3
View File
@@ -0,0 +1,3 @@
@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1"
exit /b %ERRORLEVEL%
+14
View File
@@ -0,0 +1,14 @@
$ErrorActionPreference = 'Stop'
Set-Location (Split-Path $PSScriptRoot -Parent)
docker compose --env-file .env restart postgres redis
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$deadline = (Get-Date).AddSeconds(60)
do {
$status = docker compose --env-file .env ps --format '{{.Service}} {{.Health}}'
if ($status -match 'postgres healthy' -and $status -match 'redis healthy') { break }
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
docker compose --env-file .env restart api
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
docker compose --env-file .env restart lb
exit $LASTEXITCODE
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
# Restart the API and every store it uses. Volumes stay in place.
# Does not seed, flush, or run compose down -v.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
docker compose --env-file .env restart postgres redis
i=0
while [ "$i" -lt 60 ]; do
status=$(docker compose --env-file .env ps --format '{{.Service}} {{.Health}}')
if printf '%s\n' "$status" | grep -q 'postgres healthy' && printf '%s\n' "$status" | grep -q 'redis healthy'; then
break
fi
i=$((i + 1))
sleep 1
done
docker compose --env-file .env restart api
docker compose --env-file .env restart lb
+16
View File
@@ -0,0 +1,16 @@
$ErrorActionPreference = 'Stop'
Set-Location (Split-Path $PSScriptRoot -Parent)
$ids = @(docker compose --env-file .env ps -q api | Where-Object { $_.Trim() -ne '' })
if ($ids.Count -lt 2) {
Write-Error 'need at least 2 running api replicas'
exit 1
}
foreach ($raw in $ids) {
$cid = "$raw".Trim()
docker stop -t 6 $cid | Out-Null
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
docker rm $cid | Out-Null
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
docker compose --env-file .env up -d --no-deps --no-recreate --scale "api=$($ids.Count)" api
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
# Replace API containers one at a time. The other replica keeps serving.
# Volumes and the load balancer stay up. Replica count is unchanged.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
ids=$(docker compose --env-file .env ps -q api)
count=$(printf '%s\n' "$ids" | grep -c .)
if [ "$count" -lt 2 ]; then
echo "need at least 2 running api replicas" >&2
exit 1
fi
printf '%s\n' "$ids" | while IFS= read -r cid; do
[ -n "$cid" ] || continue
docker stop -t 6 "$cid" >/dev/null
docker rm "$cid" >/dev/null
docker compose --env-file .env up -d --no-deps --no-recreate --scale "api=$count" api
done
+3
View File
@@ -0,0 +1,3 @@
@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1"
exit /b %ERRORLEVEL%
+25
View File
@@ -0,0 +1,25 @@
$ErrorActionPreference = 'Stop'
if (-not $env:TARGET_INSTANCE_ID) {
Write-Error 'TARGET_INSTANCE_ID is required'
exit 1
}
Set-Location (Split-Path $PSScriptRoot -Parent)
$ids = @(docker compose --env-file .env ps -q api)
if (-not $ids) {
Write-Error 'no running api containers'
exit 1
}
foreach ($raw in $ids) {
$cid = "$raw".Trim()
if (-not $cid) { continue }
$hostName = "$((docker inspect -f '{{.Config.Hostname}}' $cid))".Trim()
$full = "$((docker inspect -f '{{.Id}}' $cid))".Trim()
$name = "$((docker inspect -f '{{.Name}}' $cid))".Trim().TrimStart('/')
$short = $full.Substring(0, [Math]::Min(12, $full.Length))
if ($hostName -eq $env:TARGET_INSTANCE_ID -or $short -eq $env:TARGET_INSTANCE_ID -or $name -eq $env:TARGET_INSTANCE_ID) {
docker stop -t 6 $cid | Out-Null
exit $LASTEXITCODE
}
}
Write-Error 'no running api container matches the instance'
exit 1
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
# Stop the API replica named by TARGET_INSTANCE_ID and leave it stopped.
# Does not stop the database, Redis, the load balancer, or any volume.
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$ROOT"
found=""
for cid in $(docker compose --env-file .env ps -q api); do
host=$(docker inspect -f '{{.Config.Hostname}}' "$cid")
short=$(docker inspect -f '{{.Id}}' "$cid" | cut -c1-12)
name=$(docker inspect -f '{{.Name}}' "$cid" | sed 's#^/##')
if [ "$host" = "$TARGET_INSTANCE_ID" ] || [ "$short" = "$TARGET_INSTANCE_ID" ] || [ "$name" = "$TARGET_INSTANCE_ID" ]; then
found=$cid
break
fi
done
if [ -z "$found" ]; then
echo "no running api container matches the instance" >&2
exit 1
fi
docker stop -t 6 "$found" >/dev/null