Поставить реплики API за HAProxy и описать остановку одной из них.
Внешний порт остаётся только у балансировщика, миграции применяются один раз, а stop-one оставляет выбранную реплику остановленной.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
python "%~dp0restart.py"
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -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())
|
||||
Executable
+10
@@ -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
|
||||
Executable
+55
@@ -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"
|
||||
Executable
+32
@@ -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
|
||||
Reference in New Issue
Block a user