Stop all compute in AKS (Azure Managed Kubernetes)
Solution 1
Only VMs cost money out of all AKS resources (well, VHDs as well, but you cannot really stop those). So you only need to take care of those. Edit: Public Ips also cost money, but you cannot stop those either.
For my AKS cluster I just use portal and issue stop\deallocate command. And start those back when I need them (everything seems to be working fine).
You can use REST API\powershell\cli\various SKDs to achieve the same result in an automated fashion.
Solution 2
You could use the Azure CLI to stop the the entire cluster:
az aks stop --name myAksCluster --resource-group myResourceGroup
And start it again with
az aks start --name myAksCluster --resource-group myResourceGroup
Before this feature, it was possible to stop the virtual machines via Powershell:
az vm deallocate --ids $(az vm list -g MC_my_resourcegroup_westeurope --query "[].id" -o tsv)
Replace MC_my_resourcegroup_westeurope
with the name of your resource group that contains the VM(s).
When you want to start the VM(s) again, run:
az vm start --ids $(az vm list -g MC_my_resourcegroup_westeurope --query "[].id" -o tsv)
Solution 3
Above method (az vm <deallocate|start> --ids $(...)
) no longer seems to work.
Solved by first listing the VM scale sets and use these to deallocate/start:
$ResourceGroup = "MyResourceGroup"
$ClusterName = "MyAKSCluster"
$Location = "westeurope"
$vmssResourceGroup="MC_${ResourceGroup}_${ClusterName}_${Location}"
# List all VM scale sets
$vmssNames=(az vmss list --resource-group $vmssResourceGroup --query "[].id" -o tsv | Split-Path -Leaf)
# Deallocate first instance for each VM scale set
$vmssNames | ForEach-Object { az vmss deallocate --resource-group $vmssResourceGroup --name $_ --instance-ids 0}
# Start first instance for each VM scale set
$vmssNames | ForEach-Object { az vmss start --resource-group $vmssResourceGroup --name $_ --instance-ids 0}
Solution 4
There is a new feature just added to AKS:
The AKS Stop/Start cluster feature now in public preview allows AKS customers to completely pause an AKS cluster and pick up where they left off later with a switch of a button, saving time and cost. Previously, a customer had to take multiple steps to stop or start a cluster, adding to operations time and wasting compute resources. The stop/start feature keeps cluster configurations in place and customers can pick up where they left off without reconfiguring the clusters.
https://docs.microsoft.com/en-gb/azure/aks/start-stop-cluster
Dan O'Leary
Developer with 5 years experience across .net and the jvm, mainly in C#, Kotlin and Java.
Updated on June 06, 2022Comments
-
Dan O'Leary almost 2 years
I have created a managed Kubernetes cluster in Azure, but it's only for learning purposes and so I only want to pay for the compute whilst I'm actually using it.
Is there a easy way to gracefully shut down and start up the VMs, availablity sets and load balancers?