28
votes

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?

5
Just keep in mind, if you can, always use the kubectl scale-down or remove node-pools commands. Using the az vm/az vmms commands directly might break your cluster. I happened to me. Do not do it on prod. - Skarab

5 Answers

20
votes

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)
12
votes

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.

5
votes

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}

4
votes

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

0
votes

In your AKS cluster, goto properties and find your Resource group name. search for the Resource group and when you select it, it will list your virtual machines. For each Virtual Machine, select the Operations > Auto-Shutdown option and turn it on. This will turn the VM off saving you money when you aren't developing! To turn them back on again, you will need to follow the advice on previous answers or the answer here