Skip to content

Instantly share code, notes, and snippets.

@abdennour
Last active July 16, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdennour/cb7cf2927740a5bd9ecdb51f5a96af0f to your computer and use it in GitHub Desktop.
Save abdennour/cb7cf2927740a5bd9ecdb51f5a96af0f to your computer and use it in GitHub Desktop.
Rolling Update EKS Worker Nodes

Pre usage

export AWS_PROFILE=xxx
export AWS_REGION=xxx
export KUBECONFIG=xxx
kubectl config use-context xxx 

Usage

eks-workers-rolling-update.sh
#!/bin/bash
echo "kubectl must be installed"
echo "aws cli must be installed"
## @envvars
echo AWS_PROFILE or other AWS credentials env vars must be populated
echo AWS_REGION .. hope you already populated
echo KUBECONFIG env var is exposed
## TODO read k config current-context .. validate with user if he is sure
function drain() {
node_name=$1
kubectl drain ${node_name} --ignore-daemonsets --force --delete-local-data &
sleep 120
}
function terminate() {
ec2_instance_id=$1;
aws ec2 terminate-instances --instance-ids $ec2_instance_id &
aws ec2 wait instance-terminated --instance-ids $ec2_instance_id
}
function run() {
echo "TODO manage dry run here"
}
### main
kubectl get no -o go-template='{{range .items}}{{printf "%s::%s\n" .metadata.name .spec.providerID}} {{end}}' | while read ec2_instance;do
ec2_instance_id=$(echo $ec2_instance | awk -F/ '{print $NF}');
node_name=$(echo $ec2_instance | awk -F'::' '{print $1}')
drain $node_name
terminate $ec2_instance_id
sleep 300
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment