Skip to content

Instantly share code, notes, and snippets.

@gadiener
Last active December 22, 2023 12:09
Show Gist options
  • Save gadiener/7f654f24334a775c13bd620f889fecb9 to your computer and use it in GitHub Desktop.
Save gadiener/7f654f24334a775c13bd620f889fecb9 to your computer and use it in GitHub Desktop.
Replace EKS nodes
#!/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