Aks delete node

From UVOO Tech Wiki
Revision as of 18:40, 20 February 2025 by Busk (talk | contribs) (Created page with "``` #!/bin/bash # Variables RESOURCE_GROUP="your-resource-group" CLUSTER_NAME="your-aks-cluster" NODE_NAME="your-node-name" # Function to get the node resource group get_nod...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#!/bin/bash

# Variables
RESOURCE_GROUP="your-resource-group"
CLUSTER_NAME="your-aks-cluster"
NODE_NAME="your-node-name"

# Function to get the node resource group
get_node_resource_group() {
  az aks show --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --query "nodeResourceGroup" -o tsv
}

# Function to get the VMSS instance ID
get_vmss_instance_id() {
  local node_resource_group=$1
  az vmss list-instances --resource-group $node_resource_group --query "[].{Name:osProfile.computerName, ID:instanceId}" -o tsv | grep $NODE_NAME | awk '{print $2}'
}

# Cordon the node
echo "Cordoning the node $NODE_NAME..."
kubectl cordon $NODE_NAME

# Drain the node
echo "Draining the node $NODE_NAME..."
kubectl drain $NODE_NAME --ignore-daemonsets --delete-local-data

# Delete the node from the Kubernetes cluster
echo "Deleting the node $NODE_NAME from the Kubernetes cluster..."
kubectl delete node $NODE_NAME

# Get the node resource group
NODE_RESOURCE_GROUP=$(get_node_resource_group)
echo "Node resource group: $NODE_RESOURCE_GROUP"

# Get the VMSS instance ID
VMSS_INSTANCE_ID=$(get_vmss_instance_id $NODE_RESOURCE_GROUP)
echo "VMSS instance ID: $VMSS_INSTANCE_ID"

# Delete the node from the AKS VM scale set
echo "Deleting the node $NODE_NAME from the AKS VM scale set..."
az vmss delete-instances --resource-group $NODE_RESOURCE_GROUP --name $CLUSTER_NAME --instance-ids $VMSS_INSTANCE_ID

echo "Node $NODE_NAME has been successfully removed."