export AWS_PROFILE=xxx
export AWS_REGION=xxx
export KUBECONFIG=xxx
kubectl config use-context xxx
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 |