#!/bin/sh # Rolling update of the API replicas under load (lab3, level 4). # # The image is rebuilt, then every replica is deleted and recreated one at a # time. Between the steps the script waits until the replacement is healthy and # until the load balancer really serves through the declared number of replicas, # so two replicas are never replaced at once. Nginx resolves the shared network # alias `api-replica`, therefore recreated containers may get new names and IPs. # # scripts/rolling_update.sh # requires a running stand (make up) # DURATION=45 CONCURRENCY=8 scripts/rolling_update.sh set -eu ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) cd "$ROOT_DIR" COMPOSE_BIN=${COMPOSE_BIN:-docker compose} PYTHON=${PYTHON:-python3} DURATION=${DURATION:-60} CONCURRENCY=${CONCURRENCY:-8} BASE_URL=${BASE_URL:-http://localhost:8080} OUT=${OUT:-evidence/rolling-update} # Профиль без насыщения: 2 реплики по 1 CPU. На насыщающем профиле (0.5 CPU на # реплику) проба готовности сама подходит к таймауту, и «unhealthy» перестаёт # означать «сломан». API_REPLICAS=${API_REPLICAS:-2} API_CPU_LIMIT=${API_CPU_LIMIT:-1.0} API_MEM_LIMIT=${API_MEM_LIMIT:-512M} export API_REPLICAS API_CPU_LIMIT API_MEM_LIMIT NO_PROXY_ENV="env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy" NO_PROXY_ENV="$NO_PROXY_ENV NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1" mkdir -p "$OUT" rm -f "$OUT/load.json" "$OUT/load.txt" compose() { # shellcheck disable=SC2086 $COMPOSE_BIN --env-file .env -f compose.yaml "$@" } api_containers() { docker ps -q --filter 'label=com.docker.compose.service=api' } healthy_replicas() { docker ps -q --filter 'label=com.docker.compose.service=api' --filter health=healthy | wc -l | tr -d ' ' } lb_container() { docker ps -q --filter 'label=com.docker.compose.service=lb' | head -n 1 } observe_instances() { tries=${1:-10} attempt=0 while [ "$attempt" -lt "$tries" ]; do curl -fsS --noproxy '*' -D - -o /dev/null "$BASE_URL/health/live" 2>/dev/null \ | awk 'tolower($1) == "x-instance-id:" { print $2 }' attempt=$((attempt + 1)) done | sort -u | wc -l | tr -d ' ' } wait_for() { # wait_for : healthy containers and LB-visible replicas want=$1 attempt=0 while [ "$attempt" -lt 60 ]; do if [ "$(healthy_replicas)" -ge "$want" ] && [ "$(observe_instances 10)" -ge "$want" ]; then return 0 fi attempt=$((attempt + 1)) sleep 1 done return 1 } { echo "# Rolling update under load" echo "# generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)" echo "# load: tests/load.py duration=${DURATION}s concurrency=${CONCURRENCY} (closed loop)" echo "# steps: delete one replica, recreate it from the freshly built image, wait for health + LB" echo compose build api compose up -d --no-deps api replicas=$(api_containers | wc -l | tr -d ' ') if ! wait_for "$replicas"; then echo "stand is not ready on the declared profile" >&2 exit 1 fi echo "replicas before: ${replicas}; instances seen through LB: $(observe_instances 20)" lb_5xx_before=$(docker logs "$(lb_container)" 2>&1 | grep -c '" 50[0-9] ' || true) # Load keeps writing while replicas are replaced one by one. The tolerant # generator counts transitional errors instead of failing on them. # shellcheck disable=SC2086 $NO_PROXY_ENV $PYTHON student-tests/rolling_update_load.py --base-url "$BASE_URL" \ --duration "$DURATION" --concurrency "$CONCURRENCY" \ --output "$OUT/load.json" > "$OUT/load.txt" 2>&1 & load_pid=$! step=0 for container in $(api_containers); do step=$((step + 1)) instance=$(docker inspect --format '{{.Config.Hostname}}' "$container") started=$(date -u +%Y-%m-%dT%H:%M:%SZ) docker rm -f "$container" >/dev/null compose up -d --no-deps api >/dev/null if ! wait_for "$replicas"; then echo " step ${step}: replacement of ${instance} did not become ready" >&2 exit 1 fi printf ' step %s: removed %s at %s, %s healthy replicas served again at %s\n' \ "$step" "$instance" "$started" "$(healthy_replicas)" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" done wait "$load_pid" || true lb_5xx_after=$(docker logs "$(lb_container)" 2>&1 | grep -c '" 50[0-9] ' || true) echo echo "load summary (tolerant generator):" cat "$OUT/load.txt" echo echo "LB responses with 5xx during the update: $((lb_5xx_after - lb_5xx_before))" echo echo "API containers after the update:" docker ps --format '{{.Names}} {{.Status}}' --filter 'label=com.docker.compose.service=api' } > "$OUT/rolling-update.txt" 2>&1 cat "$OUT/rolling-update.txt" printf 'rolling_update.sh: evidence in %s\n' "$OUT"