1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function Invoke-K8sNodeOrchestration { [CmdletBinding()] param( [ValidateSet('ScaleUp','ScaleDown','Maintenance')] [string]$Operation, [int]$NodeCount = 1 )
$nodePool = Get-AzAksNodePool -ClusterName 'prod-cluster' $metrics = Invoke-RestMethod -Uri 'http://k8s-metrics:8080/api/v1/nodes'
switch ($Operation) { 'ScaleUp' { $newCount = $nodePool.Count + $NodeCount Update-AzAksNodePool -Name $nodePool.Name -Count $newCount Write-Host "节点池已扩容至$newCount个节点" } 'ScaleDown' { $nodesToRemove = $metrics.Nodes | Where-Object { $_.CpuUsage -lt 20 } | Select-Object -First $NodeCount $nodesToRemove | ForEach-Object { Set-AzAksNode -Name $_.Name -State Draining } } 'Maintenance' { $metrics.Nodes | Where-Object { $_.HealthStatus -ne 'Healthy' } | ForEach-Object { Add-K8sNodeLabel -Node $_.Name -Label @{ 'maintenance' = (Get-Date).ToString('yyyyMMdd') } } } } }
|