How to start/stop your AKS cluster on schedule
Introduction
If you are serious about constantly learning Azure Kubernetes 🤓, there is no way around having a development cluster at hand. However, costs can quickly add up when not closely monitoring the cluster and Virtual Machine Scale Sets. 💸
So let's better be safe than sorry and start and stop the development cluster one schedule to keep costs low. 💪🏻
This article will walk you through setting up an Azure Automation Account with a scheduled runbook to change the state of your Azure Kubernetes Cluster. 😎
Steps
Create an Azure Automation Account
To manage an Azure Automation Account, you must first install an extension on Azure CLI
, which is as easy as...
az extension add --name automation
Next, we need to create the Automation Account
az automation account create -n auto-azureblue -g rg-demo
After the account got created, we need to enable a System assigned managed identity on the account. This principal will later be given the required permissions to start and stop the cluster.
Currently, the automation
extension for the Azure CLI is in preview, and I guess that's why we cannot make the beforementioned change via CLI.
Now, we need to assign the Contributor
role to this managed identity. Make a note of the object id that got created for the automation account and issue the following commands
# Retrieve AKS id and make a note
az aks show -n aks-azureblue -g rg-demo --query-id
# Assign role
az role assignment create \
--role "Contributor" \
--assignee-object-id "<object-id>" \
--scope "<aks-id>"
Import a runbook from the gallery
Luckily enough, the gallery provides a ready-to-use runbook we can use. So navigate to Browse gallery, and search for aks-cluster-changestate
.
Select 5.1 as the runtime version, and name the runbook appropriately.
Don't forget to publish the runbook
Create and link schedules
The only thing left is creating two schedules, one that starts the AKS cluster in the morning and another that shuts it down in the evenings.
These are my two recurring schedules... of course, your mileage might vary. Ensure you pass the correct parameters for starting or stopping the cluster.
Conclusion
And there you have it. Your AKS cluster will start every day at 7:00 and shut down at midnight. Mission accomplished! 💵