Skip to content

Instantly share code, notes, and snippets.

@mgoodness
Last active November 11, 2017 09:13
Show Gist options
  • Save mgoodness/f1b598857ed2b7c31fa194a0d503136d to your computer and use it in GitHub Desktop.
Save mgoodness/f1b598857ed2b7c31fa194a0d503136d to your computer and use it in GitHub Desktop.
Bash script for draining & terminating EC2 Kubernetes nodes
#!/usr/bin/env bash
k8s_node=$(echo $1 | cut -f2 -d '/')
kubectl drain --force --ignore-daemonsets --delete-local-data ${k8s_node}
if [[ $? -eq 0 ]]; then
kubectl delete node ${k8s_node}
fi
if [[ $? -eq 0 ]]; then
instance_id=$(aws ec2 describe-instances --out=text\
--filters Name=network-interface.private-dns-name,Values=${k8s_node} \
--query 'Reservations[0].Instances[0].{InstanceID:InstanceId}')
aws ec2 terminate-instances --instance-ids ${instance_id}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment