Yes you can do this, I recommend automating this if possible, so you can turn off lower environment clusters overnight and at the weekends, it saves a lot of money.
Here is an example below using Azure Pipelines to turn clusters off and on every weekday at 7AM & 7PM.
pool:
vmImage: ubuntu-22.04
pr: none
trigger: none
schedules:
- cron: '0 07,19 * * 1,2,3,4,5' # run on weekdays only at 7AM & 7PM.
displayName: Weekday Start & Stop AKS
branches:
include:
- main
always: true
variables:
- name: serviceConnection
value: <SERVICE CONNECTION>
- name: aksResourceGroup
value: <AKS RESOURCE GROUP>
- name: aksCluster
value: <AKS CLUSTER NAME>
jobs:
- job: StartStopAKS
displayName: Start/Stop AKS
steps:
- task: AzureCLI@2
displayName: Get AKS Power Status
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$aksResourceGroup = "$(aksResourceGroup)"
$aksCluster = "$(aksCluster)"
$status = $(az aks show --resource-group $aksResourceGroup --name $aksCluster --query agentPoolProfiles[0].powerState.code -o tsv)
Write-Host "##[warning]AKS Cluster $aksCluster is $status"
Write-Host "##vso[task.setvariable variable=aksStatus;]$status"
- task: AzureCLI@2
displayName: Power On/Off AKS
inputs:
azureSubscription: ${{ variables.serviceConnection }}
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$aksResourceGroup = "$(aksResourceGroup)"
$aksCluster = "$(aksCluster)"
$status = "$(aksStatus)"
if ($status -eq "Stopped") {
Write-Host "##[warning]Cluster Status is $status, Starting Cluster..."
az aks start --resource-group $aksResourceGroup --name $aksCluster --verbose
}
else {
echo "##[warning]Cluster Status is $status, Stopping Cluster..."
az aks stop --resource-group $aksResourceGroup --name $aksCluster --verbose
}
ref: https://jimferrari.com/2023/04/25/auto-shutdown-azure-kubernetes-service-clusters-aks/