Last active
May 7, 2020 11:44
-
-
Save uncycler/9c45e5e10b8fc341bbfd83ab93532920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Drain all nodes gracefully and rely on cluster autoscaler to create new one! | |
set -e | |
# Retrieve ASG with tag application=kubernetes | |
for i in $(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`application`].Value, `kubernetes`)].AutoScalingGroupName' --output text) ; do | |
cluster=$(echo $i | cut -d'-' -f1) | |
echo "Working on cluster $cluster" | |
# Add cluster context for kubectl | |
aws eks update-kubeconfig --name $cluster | |
for j in $(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[].Instances[].InstanceId' --auto-scaling-group-names $i --output text) ; do | |
node=$(aws ec2 describe-instances --instance-ids=$j --query 'Reservations[].Instances[].PrivateDnsName' --output text) | |
echo "Working on node $node" | |
kubectl cordon $node | |
#Get all pods running on the node | |
for k in $(kubectl get pods --all-namespaces -o=jsonpath="{range .items[?(@.spec.nodeName==\"$node\")]}{.metadata.ownerReferences[0].kind},{.metadata.namespace},{.metadata.ownerReferences[0].name} {end}") ; do | |
unset tocheck | |
readarray -td, podref <<<"$k"; declare ownerinfo | |
if [ ${podref[0]} = "ReplicaSet" ] ; then | |
readarray -td, rsref <<<$(kubectl get rs/${podref[2]} -n ${podref[1]} -o=jsonpath="{.metadata.ownerReferences[0].kind},{.metadata.ownerReferences[0].name},{.status.readyReplicas}"); declare rsref | |
# Only restart RS that have a single pod | |
rollout="kubectl rollout restart ${rsref[0]}/${rsref[1]} -n ${podref[1]}" | |
[ x${rsref[2]} = "x1" ] && eval $rollout && tocheck="$rollout ; $tocheck" | |
elif [[ ${podref[0]} == "StatefulSet" ]] ; then | |
ssreplicas=$(kubectl get statefulset/${podref[2]} -n ${podref[1]} -o=jsonpath="{.status.readyReplicas}") | |
# Only restart SS that have a single pod | |
rollout="kubectl rollout restart statefulset/${podref[2]} -n ${podref[1]}" | |
[ x$ssreplicas = "x1" ] && eval $rollout && tocheck="$rollout ; $tocheck" | |
fi | |
done | |
# Make sure that everything was restarted | |
eval $(echo $tocheck | sed 's/restart/status/g') | |
# Finally drain the node and delete it | |
kubectl drain --force --ignore-daemonsets --delete-local-data $node && \ | |
aws autoscaling terminate-instance-in-auto-scaling-group --no-should-decrement-desired-capacity --instance-id=$j | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment