You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AKS集群升级脚本异常:无法升级至1.24.3却尝试降级

AKS升级脚本错误选择降级版本问题排查

问题描述

我编写了PowerShell脚本用于AKS集群版本升级,目标是从1.23.12升级到1.24.3,但脚本错误选择了1.23.8这个降级版本,导致升级失败。以下是脚本及执行输出:

脚本代码

Param 
(    
    [Parameter(Mandatory=$true)]
    [String] 
    $AKSName,
    [Parameter(Mandatory=$true)]
    [String] 
    $SubscriptionID
 )
Connect-AzAccount -Identity > $null
Set-AzContext -SubscriptionId $SubscriptionID > $null

$AKsResourceGroup= Get-AzResource -Name $AKSName | foreach {$_.ResourceGroupName}

$AKsLocation= Get-AzResource -Name $AKSName | foreach {$_.Location}

#Check Current Version Of Cluster
$CurrentVersion = Get-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName | select -ExpandProperty KubernetesVersion -WarningAction Ignore #> $null
 "The CurrentVersion is $CurrentVersion" | ConvertTo-Json

#Check the availability versions to upgrade
$Versions = Get-AzAksVersion -Location $AKsLocation | where-Object {($_.OrchestratorVersion -gt $CurrentVersion) -and ($_.IsPreview -ne 'True')} | foreach {$_.OrchestratorVersion} -WarningAction Ignore
"These are the available versions $Versions" | ConvertTo-Json

$attempt3 =$versions.Replace(' ', ' , ')
$attempt3=$attempt3.replace(' " ' , " ")
$attempt3=$attempt3.replace(" ' "," ")
#for upgrading the latest version
$finalversion=$attempt3.Split(",")[0]

#AKS Upgrade
Set-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName -KubernetesVersion $finalversion -WarningAction Ignore > $null

#To check the status of AKS
Get-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName | Format-Table -Property Name, Location, KubernetesVersion, ProvisioningState 

执行输出

"The CurrentVersion is 1.23.12"
"These are the available versions 1.23.8 1.24.3 1.24.6"
**Downgrading Kubernetes version 1.23.12 to 1.23.8 is not allowed. Available upgrades: 1.24.3,1.24.6.**

问题原因

  1. 版本号字符串比较错误:脚本直接用字符串比较$_.OrchestratorVersion -gt $CurrentVersion,但字符串比较是按字符顺序判断的。比如"1.23.12"和"1.23.8",第三个部分"12"的第一个字符"1"小于"8",导致字符串比较会认为"1.23.12" < "1.23.8",不符合升级条件的1.23.8被错误纳入可用版本列表。
  2. 版本列表处理逻辑错误:$Versions原本是版本字符串数组,但输出时被拼接成空格分隔的字符串,后续通过替换空格为逗号再拆分的方式取第一个元素,导致拿到了错误的版本1.23.8。

修复后的脚本

Param 
(    
    [Parameter(Mandatory=$true)]
    [String] 
    $AKSName,
    [Parameter(Mandatory=$true)]
    [String] 
    $SubscriptionID
 )
Connect-AzAccount -Identity > $null
Set-AzContext -SubscriptionId $SubscriptionID > $null

$AKsResourceGroup= Get-AzResource -Name $AKSName | foreach {$_.ResourceGroupName}
$AKsLocation= Get-AzResource -Name $AKSName | foreach {$_.Location}

# 转换当前版本为Version对象
$CurrentVersion = [version](Get-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName | select -ExpandProperty KubernetesVersion -WarningAction Ignore)
"The CurrentVersion is $CurrentVersion" | ConvertTo-Json

# 筛选可用升级版本(转换为Version对象进行正确比较)
$AvailableVersions = Get-AzAksVersion -Location $AKsLocation | 
    Where-Object { 
        [version]$_.OrchestratorVersion -gt $CurrentVersion -and $_.IsPreview -ne $true 
    } | 
    Select-Object -ExpandProperty OrchestratorVersion |
    Sort-Object { [version]$_ }  # 按版本号升序排序

"These are the available versions $($AvailableVersions -join ', ')" | ConvertTo-Json

# 选择最新版本(取排序后的最后一个元素)
$finalversion = $AvailableVersions[-1]

# 执行AKS升级
Set-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName -KubernetesVersion $finalversion -WarningAction Ignore

# 检查升级状态
Get-AzAksCluster -ResourceGroupName $AKsResourceGroup -Name $AKSName | Format-Table -Property Name, Location, KubernetesVersion, ProvisioningState 

修复说明

  1. 使用Version对象比较:将版本字符串转换为[version]类型,确保版本号按语义比较(比如1.23.12 > 1.23.8)。
  2. 正确处理版本数组:保留$AvailableVersions为数组类型,通过Sort-Object按版本号排序,直接取最后一个元素作为最新版本,避免字符串拼接拆分带来的错误。
  3. 优化输出格式:用-join ', '将版本数组转换为逗号分隔的字符串,输出更清晰。

内容的提问来源于stack exchange,提问作者User

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:10:12