Last active
December 22, 2023 12:09
-
-
Save gadiener/7f654f24334a775c13bd620f889fecb9 to your computer and use it in GitHub Desktop.
Replace EKS nodes
This file contains 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 | |
region=$(kubectl get nodes -o jsonpath="{.items[0].metadata.labels['topology\.kubernetes\.io/region']}") | |
nodes=$(kubectl get nodes -o jsonpath="{.items[*].metadata.name}") | |
for node in ${nodes[@]}; do | |
ec2_instance_id=$(kubectl get nodes -o jsonpath="{.items[0].spec.providerID}" | cut -d "/" -f 5) | |
echo "[INFO] Starting node '${node}' with instance ID '${ec2_instance_id}' in the '${region}' region" | |
echo "[INFO] Draining node '${node}'" | |
kubectl drain --ignore-daemonsets --delete-emptydir-data "${node}" | |
sleep 30 | |
echo "[INFO] Deleting node '${node}'" | |
kubectl delete node "${node}" | |
sleep 30 | |
echo "[INFO] Terminating EC2 instance '${ec2_instance_id}' in the '${region}' region" | |
aws ec2 terminate-instances --instance-id "${ec2_instance_id}" --region "${region}" | |
sleep 30 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment