ЛР3: балансировка, отказоустойчивость и идемпотентность
This commit is contained in:
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
project_dir=$(dirname -- "$script_dir")
|
||||
cd "$project_dir"
|
||||
|
||||
# Останавливаем публичный маршрут до перезапуска общих хранилищ.
|
||||
docker compose --env-file .env stop lb api >/dev/null
|
||||
docker compose --env-file .env restart postgres redis >/dev/null
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 postgres redis >/dev/null
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 api lb >/dev/null
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
project_dir=$(dirname -- "$script_dir")
|
||||
cd "$project_dir"
|
||||
|
||||
wait_for_two_healthy() {
|
||||
deadline=$(( $(date +%s) + 60 ))
|
||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||
ids=$(docker compose --env-file .env ps -q api)
|
||||
set -- $ids
|
||||
if [ "$#" -eq 2 ]; then
|
||||
all_healthy=true
|
||||
for container_id in "$@"; do
|
||||
status=$(docker inspect --format '{{.State.Health.Status}}' "$container_id")
|
||||
if [ "$status" != 'healthy' ]; then
|
||||
all_healthy=false
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$all_healthy" = true ]; then
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
printf '%s\n' 'two healthy API replicas did not appear within 60 seconds' >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
wait_for_two_healthy
|
||||
original_ids=$(docker compose --env-file .env ps -q api)
|
||||
|
||||
for old_id in $original_ids; do
|
||||
old_instance=$(docker inspect --format '{{.Config.Hostname}}' "$old_id")
|
||||
docker stop --time 5 "$old_id" >/dev/null
|
||||
docker rm "$old_id" >/dev/null
|
||||
docker compose --env-file .env up -d --no-deps --no-recreate --scale api=2 api >/dev/null
|
||||
wait_for_two_healthy
|
||||
printf 'replaced API instance %s\n' "$old_instance"
|
||||
done
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
project_dir=$(dirname -- "$script_dir")
|
||||
cd "$project_dir"
|
||||
|
||||
target_container=''
|
||||
for container_id in $(docker compose --env-file .env ps -q api); do
|
||||
instance_id=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
|
||||
if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then
|
||||
target_container=$container_id
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$target_container" ]; then
|
||||
printf '%s\n' 'API instance from TARGET_INSTANCE_ID was not found' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker stop --time 5 "$target_container" >/dev/null
|
||||
Reference in New Issue
Block a user