ЛР 3 уровень 4: drain при SIGTERM, лимиты ресурсов, нагрузка с отказами, rolling update

This commit is contained in:
2026-11-10 04:17:24 +03:00
parent de55dd1d8a
commit 32442d06db
24 changed files with 1429 additions and 13 deletions
+31
View File
@@ -0,0 +1,31 @@
#!/bin/sh
# Поочерёдная замена API-реплик новой версией образа (APP_VERSION=v2 scripts/rolling-update.sh).
# Миграции запускаются ОДИН раз до замены; migrations/002 только добавляет таблицу, поэтому старая версия с ней совместима.
set -eu
cd "$(dirname "$0")/.."
: "${APP_VERSION:?set new version, e.g. APP_VERSION=v2}"
export APP_VERSION
COMPOSE="docker compose --env-file .env"
$COMPOSE build migrate
$COMPOSE run --rm migrate
N=$($COMPOSE ps -q api | wc -l | tr -d ' ')
wait_healthy() {
i=0
while [ "$i" -lt 90 ]; do
c=$($COMPOSE ps api --format '{{.Health}}' | grep -c '^healthy$' || true)
[ "$c" -ge "$N" ] && return 0
i=$((i + 1)); sleep 1
done
echo "replicas did not become healthy" >&2; return 1
}
for cid in $($COMPOSE ps -q api); do
echo "replacing $cid" >&2
docker stop -t 20 "$cid" >/dev/null # SIGTERM: readiness 503, drain 5 с, затем завершение
docker rm "$cid" >/dev/null
$COMPOSE up -d --no-deps --no-recreate --scale api="$N" api # создаётся реплика из нового образа
wait_healthy
sleep 5 # LB добавляет новую реплику после 2 успешных проверок
done
+25
View File
@@ -0,0 +1,25 @@
#!/bin/sh
# Базовая нагрузка, затем по очереди отказ каждой API-реплики под нагрузкой с восстановлением между ними.
set -eu
cd "$(dirname "$0")/.."
COMPOSE="docker compose --env-file .env"
wait_healthy() {
i=0
while [ "$i" -lt 90 ]; do
c=$($COMPOSE ps api --format '{{.Health}}' | grep -c '^healthy$' || true)
[ "$c" -ge 2 ] && return 0
i=$((i + 1)); sleep 1
done
echo "replicas did not become healthy" >&2; return 1
}
python3 student_tests/loadgen.py --duration 30 --output evidence/load-baseline.json
for idx in 0 1; do
python3 student_tests/loadgen.py --duration 40 --action-script scripts/stop-one.sh \
--action-at 10 --target-index "$idx" --output "evidence/load-stop-$idx.json"
$COMPOSE ps -a > "evidence/load-stop-$idx-ps.txt"
$COMPOSE start api
wait_healthy
sleep 10 # LB возвращает реплику после 2 успешных проверок
done