Files
data-engine/fix-metrics-server.ps1

60 lines
2.4 KiB
PowerShell
Raw Normal View History

2026-04-17 17:46:13 +08:00
Write-Host "=== Fix metrics-server ===" -ForegroundColor Cyan
Write-Host ""
$k3sContainer = "k3s-server"
$TARGET_IMAGE = "rancher/mirrored-metrics-server:v0.8.0"
Write-Host "[Step 1] Pull image from domestic registry..." -ForegroundColor Yellow
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3
if ($LASTEXITCODE -ne 0) {
Write-Host "[ERROR] Failed to pull image" -ForegroundColor Red
exit 1
}
Write-Host "[Step 2] Tag image to match K3s requirement..." -ForegroundColor Yellow
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3 $TARGET_IMAGE
Write-Host "[OK] Image tagged as $TARGET_IMAGE" -ForegroundColor Green
Write-Host "[Step 3] Save to tar file..." -ForegroundColor Yellow
$tarPath = "$PWD\metrics-server.tar"
docker save -o $tarPath $TARGET_IMAGE
Write-Host "[OK] Saved: $([math]::Round((Get-Item $tarPath).Length / 1MB, 2)) MB" -ForegroundColor Green
Write-Host "[Step 4] Copy to K3s container..." -ForegroundColor Yellow
docker cp $tarPath "${k3sContainer}:/tmp/metrics-server.tar"
Write-Host "[OK] Copied" -ForegroundColor Green
Write-Host "[Step 5] Import in K3s..." -ForegroundColor Yellow
docker exec $k3sContainer ctr -n k8s.io images import /tmp/metrics-server.tar
docker exec $k3sContainer rm /tmp/metrics-server.tar
Remove-Item $tarPath
Write-Host "[OK] Imported" -ForegroundColor Green
Write-Host "[Step 6] Restart metrics-server pod..." -ForegroundColor Yellow
kubectl -n kube-system delete pod -l k8s-app=metrics-server --force --grace-period=0
Write-Host "Waiting for pod to start..." -ForegroundColor Cyan
Start-Sleep -Seconds 15
for ($i = 1; $i -le 30; $i++) {
$status = kubectl -n kube-system get pods -l k8s-app=metrics-server -o jsonpath='{.items[0].status.phase}' 2>$null
$ready = kubectl -n kube-system get pods -l k8s-app=metrics-server -o jsonpath='{.items[0].status.conditions[?(@.type=="Ready")].status}' 2>$null
if ($status -eq "Running" -and $ready -eq "True") {
Write-Host "[SUCCESS] metrics-server is ready!" -ForegroundColor Green
break
}
if ($i % 5 -eq 0) {
Write-Host " Waiting... ($i/30) Current: $status" -ForegroundColor Gray
}
Start-Sleep -Seconds 5
}
Write-Host ""
Write-Host "Testing metrics API..." -ForegroundColor Cyan
Start-Sleep -Seconds 5
kubectl top pods
Write-Host ""
Write-Host "=== Done ===" -ForegroundColor Green