通过 Azure Powershell 禁用 AKS 节点池的自动缩放

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

我有一个带有 3 个节点池的 AKS - 2 个用户类型和 1 个系统类型。 我想通过 Azure 运行手册创建一个自动化,它将在下班时间将 2 个用户类型的节点池缩放为零,然后重新启用自动缩放到具有之前最小和最大计数的节点池。

至于重新启用自动缩放,我找到了相应的命令:

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

但至于将它们缩放为零,以下命令不起作用:

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

^ 为此,我收到一个错误消息,即 MaxCount 值不能为“0”

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

^ 对于我没有得到任何错误,但它不会将其缩放为 0,基本上什么都不做。

所以在这之后我意识到节点池必须处于手动缩放模式而不是自动缩放才能使前一个命令起作用,这意味着我的脚本需要禁用节点池的自动缩放或将其切换到手动缩放模式.

这是我使用的 Update-AzAksNodePool 命令的文档: https://docs.microsoft.com/en-us/powershell/module/az.aks/update-azaksnodepool?view=azps-7.3.2

文档没有提到任何禁用自动缩放的参数,只是启用它。 我用 -EnableAutoScaling 参数尝试了 运行 两次,没有切换它。

我用 -EnableAutoScaling $false 试过 运行,效果不佳,因为它没有收到价值。

文档中没有提到禁用自动缩放。 我发现执行此操作的唯一方法是通过 Azure CLI(运行手册中不可用)或通过 Azure 门户(无法自动化)。

有人知道如何通过 Powershell 关闭节点池的自动缩放模式吗?

使用当前可用的功能,要通过 Powershell 禁用 AKS 节点池的 auto-scaling,我建议利用 Agent Pools - Create Or Update REST API and Invoke-RestMethod PowerShell cmdlet 并创建 Azure 自动化运行手册,如下所示。

$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 *

另一方面,由于我们具有通过 Agent Pools - Create Or Update REST API and az aks nodepool update CLI 命令禁用 AKS 节点池 auto-scaling 的功能,因此我们也应该通过直接 Azure PowerShell cmdlet 具有相同的功能。因此,我正在与我们的内部团队联系以核实此事,并会在听到更多信息后及时通知您。

更新一: 有关它的更多信息,请参阅 this and this 文档。