azureazure-aksazure-powershellazure-automationazure-runbook

Disable Auto-Scaling of an AKS Node pool via Azure Powershell


I have an AKS with 3 Node pools - 2 USER type and 1 SYSTEM type. I want to create an automation via Azure runbooks that will scale the 2 USER type Node pools to zero during off-hours and then re-enable auto-scaling to the Node pools with their previous Min and Max count.

As for re-enabling the auto-scaling I found the command for that:

Update-AzAksNodePool -ResourceGroupName $ResourceGroupName -ClusterName $AKSCluster -Name $NodePool -MinCount <Min number> -MaxCount <Max number> -EnableAutoScaling

But as for scaling them to zero, the following commands didn't work:

Update-AzAksNodePool -ResourceGroupName $ResourceGroupName -ClusterName $AKSCluster -Name $NodePool -MinCount 0 -MaxCount 0 

^ For this I get an error that MaxCount value can't be '0'

Update-AzAksNodePool -ResourceGroupName $ResourceGroupName -ClusterName $AKSCluster -Name $NodePool -NodeCount 0

^ For the I don't get any error but it doesn't scale it to 0, basically does nothing.

So after this I realized that the Node pool must be in Manual scaling mode and not Autoscale for the former command to work, which means my script needs to disable the Node pool's auto-scaling or switch it to Manual scaling mode.

This is the documentation I used of the Update-AzAksNodePool command: https://learn.microsoft.com/en-us/powershell/module/az.aks/update-azaksnodepool?view=azps-7.3.2

The documentation does not mention any parameter to disable auto-scaling, only to enable it. I tried running it twice with the -EnableAutoScaling parameter, didn't switch it.

I tried running it with -EnableAutoScaling $false, didn't work as well as it doesn't receive value.

There was nothing in the documentation that mentioned disabling the auto-scaling. The only way I found doing it was via Azure CLI - which isn't available in a runbook, or via the Azure Portal - which can't be automated.

Anyone knows how to turn a Node pool's auto-scaling mode off via Powershell?


Solution

  • With the features that are currently available, to disable an AKS node pool's auto-scaling via Powershell, I would recommend to leverage Agent Pools - Create Or Update REST API and Invoke-RestMethod PowerShell cmdlets and create an Azure Automation runbook something as shown below.

    $SubscriptionID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $ResourceGroupName = "xxxxxxxxxxxxxxxxxxxxxx"
    $AKSClusterName = "xxxxxxxxxxxxxxxxxxx"
    $AgentPoolName = "xxxxxxxxxxxxxxxxxxx"
    
    $URI = "https://management.azure.com/subscriptions/$SubscriptionID/resourceGroups/$ResourceGroupName/providers/Microsoft.ContainerService/managedClusters/$AKSClusterName/agentPools/$AgentPoolName"+"?api-version=2022-01-01"
    
    $Body = @{
      properties = @{
        enableAutoScaling = "false";
        mode = "System"
      }
    }
    $Body.properties.enableAutoScaling=$false
    $JSONBody = $Body | ConvertTo-Json
    
    $azContext = (Connect-AzAccount -Identity).context
    $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
    $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
    $token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
    $authHeader = @{
        'Content-Type'='application/json'
        'Authorization'='Bearer ' + $token.AccessToken
    }
    
    $response = Invoke-RestMethod -Uri $URI -Method PUT -Headers $authHeader -Body $JSONBody
    $response | fl *
    

    enter image description here

    enter image description here

    On the other hand, as we have feature to disable AKS node pool's auto-scaling via Agent Pools - Create Or Update REST API and az aks nodepool update CLI commands so we are supposed to have the same feature via direct Azure PowerShell cmdlet as well. So, I am reaching out to our internal team to check on this and will keep you updated as I hear more information.

    UPDATE-1: enter image description here enter image description here For more information with regards to it, please refer this and this documents.