Поднять API в нескольких репликах за HAProxy и добавить скрипты отказа реплики и rolling update

This commit is contained in:
2026-11-02 17:20:38 +03:00
parent 951fd0d517
commit c5cdcb3377
9 changed files with 312 additions and 10 deletions
+34
View File
@@ -0,0 +1,34 @@
#!/bin/sh
# Lists the API containers of this compose project, one per line:
# <container id> <name> <state> <alias> <image id>
# The alias is what the process sends as X-Instance-Id. It is random per
# process, so it is asked from the process itself, over the same wget the
# Docker healthcheck uses, from inside the container. A container that is not
# running has no alias and shows "-". The replicas are asked in parallel: the
# failover hook that relies on this list must return within 10 seconds.
set -eu
cd "$(dirname "$0")/.."
ids=$(docker compose ps -aq api)
[ -n "$ids" ] || exit 0
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
docker inspect --format '{{.Id}} {{.Name}} {{.State.Status}} {{.Image}}' $ids >"$tmp/containers"
while read -r id name state image; do
if [ "$state" = running ]; then
# wget prints the response headers on stderr; the body is dropped.
(docker exec "$id" wget -S -q -O - http://127.0.0.1:8080/health/live 2>&1 >/dev/null |
sed -n 's/^ *[Xx]-[Ii]nstance-[Ii]d: *//p' | tr -d '\r' | head -n 1 >"$tmp/$id") &
fi
done <"$tmp/containers"
wait
while read -r id name state image; do
alias=-
if [ -s "$tmp/$id" ]; then
alias=$(cat "$tmp/$id")
fi
image=${image#sha256:}
printf '%s %s %s %s %s\n' "$(printf '%s' "$id" | cut -c1-12)" "${name#/}" "$state" "$alias" "$(printf '%s' "$image" | cut -c1-12)"
done <"$tmp/containers"
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
# Shows which container answers under which X-Instance-Id: the proof on the
# defense that the header values belong to different running processes, and
# which container a failover test actually stopped.
set -eu
cd "$(dirname "$0")/.."
printf '%-12s %-14s %-8s %-16s %s\n' CONTAINER NAME STATE X-INSTANCE-ID IMAGE
sh scripts/api-instances.sh | while read -r id name state alias image; do
printf '%-12s %-14s %-8s %-16s %s\n' "$id" "$name" "$state" "$alias" "$image"
done
+44
View File
@@ -0,0 +1,44 @@
#!/bin/sh
# Rolling update of the API without downtime, with docker rollout
# (https://github.com/wowu/docker-rollout, MIT): it starts as many new
# replicas as are running, waits until all of them are healthy, then stops
# and removes the old ones. If a new replica does not become healthy it
# removes the new ones and leaves the old ones serving, so the stand is never
# left with a mix of versions.
#
# The migrations of the new version run first, once, while the old replicas
# serve: they only add to the schema. docker rollout scales with `compose up`,
# which also starts the dependencies of the API and would rerun the old,
# exited migrate container. Recreating it beforehand from the new image,
# without starting it, makes that `up` run the new migrations exactly once,
# before any new replica starts.
set -eu
cd "$(dirname "$0")/.."
version=v0.14
checksum=cdeaba6ae9eee3b0b606286e585bbda6787283d801a6ad6d9b9d2bc347fda05b
tool=bin/docker-rollout
if ! printf '%s %s\n' "$checksum" "$tool" | sha256sum -c - >/dev/null 2>&1; then
mkdir -p bin
curl -fsSL -o "$tool.download" "https://github.com/wowu/docker-rollout/releases/download/$version/docker-rollout"
if ! printf '%s %s\n' "$checksum" "$tool.download" | sha256sum -c - >/dev/null 2>&1; then
rm -f "$tool.download"
echo "docker-rollout $version: checksum mismatch, not used" >&2
exit 1
fi
mv "$tool.download" "$tool"
fi
docker compose build api
docker compose up --no-start --no-deps --force-recreate migrate
# Run through sh: Windows cannot use the script as a docker CLI plugin.
if ! sh "$tool" rollout --wait-after-healthy 4 api; then
# A failed migration stops the scale-up after the new containers were
# created but before they started; remove them for a clean next attempt.
created=$(docker compose ps -aq --status created api)
if [ -n "$created" ]; then
docker rm $created >/dev/null
fi
exit 1
fi
+5
View File
@@ -0,0 +1,5 @@
@echo off
rem Windows entry point of scripts/stop-one.sh: tests/resilience.py starts the
rem hook without a shell, and Windows cannot execute a .sh file directly.
sh "%~dp0stop-one.sh"
exit /b %errorlevel%
+31
View File
@@ -0,0 +1,31 @@
#!/bin/sh
# Failover hook of `make test-failover` (tests/resilience.py): stops the one
# API replica whose X-Instance-Id is $TARGET_INSTANCE_ID and leaves it
# stopped. The stop is graceful: on SIGTERM the replica turns unready, keeps
# serving for its drain delay while HAProxy takes it out of rotation, then
# exits; `docker stop` returns once it has. Nothing else is touched, and an
# alias that does not name exactly one running replica stops nothing.
# Restore the replica with: docker compose start api
set -eu
cd "$(dirname "$0")/.."
target=${TARGET_INSTANCE_ID:-}
case "$target" in
'' | *[!0-9a-f]*)
echo "TARGET_INSTANCE_ID must be the 16 lowercase hex characters of an X-Instance-Id" >&2
exit 2
;;
esac
if [ ${#target} -ne 16 ]; then
echo "TARGET_INSTANCE_ID must be the 16 lowercase hex characters of an X-Instance-Id" >&2
exit 2
fi
matches=$(sh scripts/api-instances.sh | awk -v t="$target" '$3 == "running" && $4 == t { print $1 }')
count=$(printf '%s\n' "$matches" | grep -c . || true)
if [ "$count" -ne 1 ]; then
echo "expected one running API replica with X-Instance-Id $target, found $count" >&2
exit 2
fi
docker stop "$matches" >/dev/null
echo "stopped API container $matches (X-Instance-Id $target)"