Implement lab3 load balancing and failover

This commit is contained in:
2026-11-16 22:34:30 +03:00
parent 0ab3a341aa
commit 23f4fd6d41
25 changed files with 1327 additions and 18 deletions
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
echo "Stopping API replicas..."
docker compose stop api1 api2
echo "Restarting PostgreSQL..."
docker compose restart db
docker compose up -d --wait db
echo "Starting API replicas..."
docker compose up -d --wait api1 api2
echo "Restart complete. Volumes preserved."
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
if [[ -z "${TARGET_INSTANCE_ID:-}" ]]; then
echo "TARGET_INSTANCE_ID is not set" >&2
exit 1
fi
for service in api1 api2; do
instance_id=$(
docker compose exec -T "$service" python -c '
import urllib.request
response = urllib.request.urlopen(
"http://127.0.0.1:8080/health/live",
timeout=2
)
print(response.headers.get("X-Instance-Id", ""))
' 2>/dev/null
) || continue
if [[ "$instance_id" == "$TARGET_INSTANCE_ID" ]]; then
echo "Stopping $service"
docker compose stop -t 2 "$service"
exit 0
fi
done
echo "Target API instance not found" >&2
exit 1