fix: switch API pools atomically

This commit is contained in:
2026-10-09 13:39:09 +03:00
parent 3fde1c324c
commit 37d93c340e
5 changed files with 144 additions and 59 deletions
+1 -1
View File
@@ -180,7 +180,7 @@ jobs:
git cat-file -e '$DEPLOY_SHA^{commit}' git cat-file -e '$DEPLOY_SHA^{commit}'
git reset --hard '$DEPLOY_SHA' git reset --hard '$DEPLOY_SHA'
manifest='/tmp/mtusi2026lab4-${{ github.run_id }}.env' manifest='/tmp/mtusi2026lab4-${{ github.run_id }}.env'
if scripts/rolling-update.sh \"\$manifest\"; then if DEPLOY_GENERATION='${{ github.run_id }}' scripts/rolling-update.sh \"\$manifest\"; then
rm -f \"\$manifest\" rm -f \"\$manifest\"
else else
status=\$? status=\$?
+2
View File
@@ -177,6 +177,8 @@ services:
image: ${NGINX_IMAGE:-mtusi-nginx:lab4} image: ${NGINX_IMAGE:-mtusi-nginx:lab4}
ports: ports:
- "${API_BIND_ADDRESS}:8080" - "${API_BIND_ADDRESS}:8080"
volumes:
- ${NGINX_UPSTREAM_FILE:-./nginx/upstream.conf}:/etc/nginx/conf.d/upstream.conf:ro
depends_on: depends_on:
api: api:
condition: service_healthy condition: service_healthy
+1 -1
View File
@@ -20,7 +20,7 @@ http {
upstream messenger { upstream messenger {
zone messenger 64k; zone messenger 64k;
server api:8080 resolve max_fails=1 fail_timeout=3s; include /etc/nginx/conf.d/upstream.conf;
keepalive 32; keepalive 32;
} }
+1
View File
@@ -0,0 +1 @@
server api:8080 resolve max_fails=1 fail_timeout=3s;
+139 -57
View File
@@ -10,12 +10,21 @@ fi
manifest=$1 manifest=$1
deploy_dir=${DEPLOY_STATE_DIR:-"$HOME/.local/state/mtusi2026lab4"} deploy_dir=${DEPLOY_STATE_DIR:-"$HOME/.local/state/mtusi2026lab4"}
current_manifest=$deploy_dir/current-images.env current_manifest=$deploy_dir/current-images.env
previous_manifest=$deploy_dir/previous-images.env active_containers=$deploy_dir/active-api-containers
previous_containers=$deploy_dir/previous-api-containers
upstream_file=$deploy_dir/upstream.conf
upstream_backup=$deploy_dir/upstream.previous.conf
candidate_file=$deploy_dir/candidate-api-containers
replicas=${API_REPLICAS:-2} replicas=${API_REPLICAS:-2}
rollback_ready=false project=${COMPOSE_PROJECT_NAME:-mtusi2026lab4}
switched=false switched=false
nginx_created=false
upstream_changed=false
old_pool=""
mkdir -p "$deploy_dir" mkdir -p "$deploy_dir"
: > "$candidate_file"
export NGINX_UPSTREAM_FILE=$upstream_file
load_manifest() { load_manifest() {
set -a set -a
@@ -28,12 +37,30 @@ compose() {
docker compose -f compose.yaml -f deploy/compose.images.yaml "$@" docker compose -f compose.yaml -f deploy/compose.images.yaml "$@"
} }
wait_for_replicas() { container_name() {
expected=$1 name=$(docker inspect --format '{{.Name}}' "$1")
printf '%s\n' "${name#/}"
}
running_container() {
[ "$(docker inspect --format '{{.State.Running}}' "$1" 2>/dev/null || true)" = true ]
}
write_upstream() {
output=$1
containers=$2
: > "$output"
for container in $containers; do
name=$(container_name "$container")
printf 'server %s:8080 resolve max_fails=1 fail_timeout=3s;\n' "$name" >> "$output"
done
}
wait_for_containers() {
containers=$1
deadline=$(($(date +%s) + 120)) deadline=$(($(date +%s) + 120))
while [ "$(date +%s)" -lt "$deadline" ]; do while [ "$(date +%s)" -lt "$deadline" ]; do
containers=$(compose ps -q api)
count=0 count=0
healthy=0 healthy=0
for container in $containers; do for container in $containers; do
@@ -43,13 +70,13 @@ wait_for_replicas() {
healthy=$((healthy + 1)) healthy=$((healthy + 1))
fi fi
done done
if [ "$count" -eq "$expected" ] && [ "$healthy" -eq "$expected" ]; then if [ "$count" -eq "$replicas" ] && [ "$healthy" -eq "$replicas" ]; then
return 0 return 0
fi fi
sleep 1 sleep 1
done done
printf 'API replicas did not become healthy: expected %s\n' "$expected" >&2 printf 'candidate API pool did not become healthy: expected %s\n' "$replicas" >&2
return 1 return 1
} }
@@ -57,53 +84,36 @@ smoke_check() {
compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready
} }
running_image() { remove_containers() {
containers=$(compose ps -q "$1") containers=$1
for container in $containers; do for container in $containers; do
docker inspect --format '{{.Config.Image}}' "$container" docker rm -f "$container" >/dev/null 2>&1 || true
return 0
done done
return 1
} }
capture_running_images() { restore_upstream() {
output=$1 if [ -s "$upstream_backup" ]; then
api_image=$(running_image api) || return 1 cp "$upstream_backup" "$upstream_file"
nginx_image=$(running_image nginx) || return 1 compose exec -T nginx nginx -t
swagger_image=$(running_image swagger) || swagger_image=$SWAGGER_IMAGE compose exec -T nginx nginx -s reload
prometheus_image=$(running_image prometheus) || prometheus_image=$PROMETHEUS_IMAGE fi
grafana_image=$(running_image grafana) || grafana_image=$GRAFANA_IMAGE
s3_image=$(running_image s3) || s3_image=$S3_IMAGE
s3_bootstrap_image=$(running_image s3-bootstrap) || s3_bootstrap_image=$S3_BOOTSTRAP_IMAGE
{
printf 'API_IMAGE=%s\n' "$api_image"
printf 'NGINX_IMAGE=%s\n' "$nginx_image"
printf 'SWAGGER_IMAGE=%s\n' "$swagger_image"
printf 'PROMETHEUS_IMAGE=%s\n' "$prometheus_image"
printf 'GRAFANA_IMAGE=%s\n' "$grafana_image"
printf 'S3_IMAGE=%s\n' "$s3_image"
printf 'S3_BOOTSTRAP_IMAGE=%s\n' "$s3_bootstrap_image"
} > "$output"
} }
rollback() { rollback() {
status=$? status=$?
trap - EXIT INT TERM trap - EXIT INT TERM
candidates=$(cat "$candidate_file" 2>/dev/null || true)
if [ "$switched" = true ] && [ "$rollback_ready" = true ]; then if [ "$upstream_changed" = true ] && [ -n "$old_pool" ]; then
printf '%s\n' 'Deployment failed; restoring the previous images.' >&2 printf '%s\n' 'Deployment failed; switching Nginx back to the previous API pool.' >&2
compose stop nginx api >/dev/null 2>&1 || true restore_upstream || true
compose rm -f nginx api >/dev/null 2>&1 || true if [ "$switched" = true ]; then
load_manifest "$previous_manifest" smoke_check || true
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api fi
wait_for_replicas "$replicas" elif [ "$nginx_created" = true ]; then
compose up -d --no-build --no-deps --force-recreate nginx compose stop nginx >/dev/null 2>&1 || true
smoke_check
elif [ "$switched" = true ]; then
printf '%s\n' 'Deployment failed and no previous image manifest is available.' >&2
fi fi
remove_containers "$candidates"
exit "$status" exit "$status"
} }
@@ -112,30 +122,102 @@ trap 'exit 130' INT
trap 'exit 143' TERM trap 'exit 143' TERM
load_manifest "$manifest" load_manifest "$manifest"
new_api_image=$API_IMAGE
if [ -f "$current_manifest" ]; then if [ -f "$previous_containers" ]; then
cp "$current_manifest" "$previous_manifest" remove_containers "$(cat "$previous_containers")"
rollback_ready=true rm -f "$previous_containers"
elif capture_running_images "$previous_manifest"; then
rollback_ready=true
fi fi
if [ -s "$active_containers" ]; then
for container in $(cat "$active_containers"); do
if running_container "$container"; then
old_pool="$old_pool $container"
fi
done
fi
if [ -z "$old_pool" ]; then
old_pool=$(compose ps -q api)
fi
if [ -s "$upstream_file" ]; then
:
elif [ -n "$old_pool" ]; then
write_upstream "$upstream_file" "$old_pool"
else
cp nginx/upstream.conf "$upstream_file"
fi
cp "$upstream_file" "$upstream_backup"
compose pull api nginx swagger prometheus grafana s3 s3-bootstrap compose pull api nginx swagger prometheus grafana s3 s3-bootstrap
compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3 compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3
compose run --rm --no-deps s3-bootstrap compose run --rm --no-deps s3-bootstrap
compose run --rm --no-deps migrate compose run --rm --no-deps migrate
# Stop the entry point and every old API replica before starting the new pool. nginx_container=$(compose ps -q nginx)
# This gives an atomic version boundary at the cost of a short maintenance window. if [ -n "$nginx_container" ] && ! docker inspect --format '{{range .Mounts}}{{println .Destination}}{{end}}' "$nginx_container" | grep -qx /etc/nginx/conf.d/upstream.conf; then
switched=true compose up -d --no-build --no-deps --force-recreate nginx
compose stop nginx api nginx_container=$(compose ps -q nginx)
compose rm -f nginx api fi
compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api if [ -n "$nginx_container" ]; then
wait_for_replicas "$replicas" running_nginx_image=$(docker inspect --format '{{.Config.Image}}' "$nginx_container")
compose up -d --no-build --no-deps --force-recreate nginx if [ "$running_nginx_image" != "$NGINX_IMAGE" ]; then
printf 'verified Nginx image differs from the running load balancer; deploy it in a dedicated maintenance operation\n' >&2
exit 1
fi
fi
API_IMAGE=$new_api_image
export API_IMAGE
short_digest=$(printf '%s' "$new_api_image" | sed 's/.*sha256://; s/[^0-9a-f].*//' | cut -c1-12)
if [ -z "$short_digest" ]; then
short_digest=$(date +%s)
fi
generation=${DEPLOY_GENERATION:-"$(date +%s)-$$"}
candidate_pool=""
index=1
while [ "$index" -le "$replicas" ]; do
name="${project}-api-${short_digest}-${generation}-${index}"
container=$(compose run -d --no-deps --name "$name" api)
candidate_pool="$candidate_pool $container"
printf '%s\n' "$container" >> "$candidate_file"
index=$((index + 1))
done
wait_for_containers "$candidate_pool"
write_upstream "$upstream_file.next" "$candidate_pool"
cp "$upstream_file.next" "$upstream_file"
upstream_changed=true
if [ -n "$(compose ps -q nginx)" ]; then
compose exec -T nginx nginx -t
switched=true
compose exec -T nginx nginx -s reload
else
nginx_created=true
compose up -d --no-build --no-deps nginx
switched=true
fi
smoke_check smoke_check
compose up -d --no-build swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana application_network="${project}_application"
for container in $candidate_pool; do
docker network disconnect "$application_network" "$container"
docker network connect --alias api "$application_network" "$container"
done
smoke_check
compose up -d --no-build --no-deps swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana
printf '%s\n' "$candidate_pool" > "$active_containers"
cp "$manifest" "$current_manifest" cp "$manifest" "$current_manifest"
rm -f "$candidate_file" "$upstream_file.next"
trap - EXIT INT TERM trap - EXIT INT TERM
if [ -n "$old_pool" ]; then
for container in $old_pool; do
docker stop --time 6 "$container" >/dev/null || true
done
printf '%s\n' "$old_pool" > "$previous_containers"
fi