Skip to content

Instantly share code, notes, and snippets.

@simonswine
Created February 5, 2025 10:28
Show Gist options
  • Save simonswine/e46f1dbd446bf9c43bc30aabb1024135 to your computer and use it in GitHub Desktop.
Save simonswine/e46f1dbd446bf9c43bc30aabb1024135 to your computer and use it in GitHub Desktop.
A little script to scale down a stateful set one by one
#!/usr/bin/env bash
# This script scales down a stateful set in a controlled way one by one.
set -euo pipefail
set -x
KUBERNETES_CONTEXT=${KUBERNETES_CONTEXT:-dev-us-central-0}
KUBERNETES_NAMESPACE=${KUBERNETES_NAMESPACE:-fire-dev-001}
k="kubectl --namespace ${KUBERNETES_NAMESPACE} --context ${KUBERNETES_CONTEXT} "
NAME=pyroscope-ingester
TARGET_REPLICAS=3
while true; do
CURRENT_REPLICAS=$($k get "sts/${NAME}" -o jsonpath='{.spec.replicas}')
if [ "${CURRENT_REPLICAS}" -lt "$((TARGET_REPLICAS + 1))" ]; then
echo "done"
exit 0
fi
next=$((CURRENT_REPLICAS - 1))
$k scale "--current-replicas=$CURRENT_REPLICAS" "--replicas=${next}" "sts/${NAME}"
# wait extra for luck
$k wait --timeout 600s --for=delete "pod/${NAME}-${next}"
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment