Prove per-replica failover and rolling replacement under load

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 13:03:54 +03:00
parent a2021b4e25
commit 23ed63f593
11 changed files with 429 additions and 4 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/bin/sh
# Replace the two original API processes one at a time. No schema changes here.
set -eu
cd "$(dirname "$0")/.."
original=$(docker compose --env-file .env ps -q api)
set -- $original
if [ "$#" -ne 2 ]; then
echo "expected exactly two running API replicas" >&2
exit 1
fi
for container in "$@"; do
if [ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" != healthy ]; then
echo "API replica is not healthy before replacement" >&2
exit 1
fi
done
for container in "$@"; do
docker stop -t 10 "$container" >/dev/null
docker rm "$container" >/dev/null
# --no-recreate preserves the other old replica while Compose fills the gap.
docker compose --env-file .env up -d --no-deps --no-recreate --wait --scale api=2 api >/dev/null
current=$(docker compose --env-file .env ps -q api)
set -- $current
if [ "$#" -ne 2 ]; then
echo "replacement did not restore two API replicas" >&2
exit 1
fi
for running in "$@"; do
if [ "$(docker inspect -f '{{.State.Health.Status}}' "$running")" != healthy ]; then
echo "replacement API is not healthy" >&2
exit 1
fi
done
echo "replaced API container $container; two healthy replicas now serve traffic"
done
+1 -1
View File
@@ -5,7 +5,7 @@ cd "$(dirname "$0")/.."
for container in $(docker compose --env-file .env ps -q api); do
instance=$(docker exec "$container" python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).headers['X-Instance-Id'])")
if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then
docker stop -t 5 "$container" >/dev/null
docker stop -t 10 "$container" >/dev/null
exit 0
fi
done