Skip to content

Instantly share code, notes, and snippets.

@jlcrow
Last active January 18, 2020 16:06
Show Gist options
  • Save jlcrow/45dcf47d71891e180911ce4e43240c9d to your computer and use it in GitHub Desktop.
Save jlcrow/45dcf47d71891e180911ce4e43240c9d to your computer and use it in GitHub Desktop.
rollStatefulset.sh
#!/usr/bin/env bash
NAMESPACE=$1
STATEFULSET=$2
CONTAINER=$3
COMMAND=$4
GREP=$5
REPLICAS=$(kubectl -n $NAMESPACE get statefulset $STATEFULSET -o=jsonpath='{.status.currentReplicas}')
COUNT=$(($REPLICAS-1))
while [ $COUNT -gt -1 ]; do
DELCMD=$(kubectl -n $NAMESPACE delete pod $STATEFULSET-$COUNT)
echo $DELCMD
STATUS="-1"
while [ $STATUS -ne "0" ]; do
TEST=$(kubectl -n $NAMESPACE exec -it -c $CONTAINER $STATEFULSET-$COUNT -- $COMMAND | grep $GREP)
let STATUS=$?
echo "$TEST - $STATUS"
SLEEP 2
done
RESULT=$(kubectl -n $NAMESPACE get statefulset $STATEFULSET)
let COUNT=COUNT-1
done
@jlcrow
Copy link
Author

jlcrow commented Jan 18, 2020

This is an attempt to do a controlled roll of a statefulset by deleting the pods in reverse order but allowing a check command to run so that the next pod is not rolled until some sort of ready state is reached - this requires kubectl to be intialized to a cluster before running.

ex. ./rollStatefulset.sh redis rfr-redis-hybris-dev redis "redis-cli role" "master\|slave"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment