k3s动态扩容

This commit is contained in:
2026-04-17 17:46:13 +08:00
parent 2d844645a0
commit 88e46d7877
5 changed files with 344 additions and 1 deletions

View File

@@ -48,11 +48,78 @@ if ($LASTEXITCODE -ne 0) {
Write-Host "[OK] Image imported" -ForegroundColor Green
Write-Host "`n[3.5/7] Check and fix metrics-server..." -ForegroundColor Yellow
$metricsPod = kubectl -n kube-system get pods -l k8s-app=metrics-server -o jsonpath='{.items[0].metadata.name}' 2>$null
if ($metricsPod) {
$podStatus = kubectl -n kube-system get pod $metricsPod -o jsonpath='{.status.phase}' 2>$null
if ($podStatus -ne "Running") {
Write-Host " metrics-server status: $podStatus, attempting to fix..." -ForegroundColor Yellow
$podDetail = kubectl -n kube-system describe pod $metricsPod 2>&1
if ($podDetail -match "ImagePullBackOff|ErrImagePull") {
Write-Host " Detected image pull issue, using domestic registry..." -ForegroundColor Cyan
try {
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3 2>$null
if ($LASTEXITCODE -eq 0) {
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3 rancher/mirrored-metrics-server:v0.8.0
$msTarPath = "$PWD\metrics-server.tar"
docker save -o $msTarPath rancher/mirrored-metrics-server:v0.8.0
if ($LASTEXITCODE -eq 0) {
docker cp $msTarPath "${k3sContainer}:/tmp/metrics-server.tar"
docker exec $k3sContainer ctr -n k8s.io images import /tmp/metrics-server.tar
docker exec $k3sContainer rm /tmp/metrics-server.tar
Remove-Item $msTarPath -ErrorAction SilentlyContinue
kubectl -n kube-system delete pod $metricsPod --force --grace-period=0
Write-Host " [OK] metrics-server image imported and pod restarted" -ForegroundColor Green
}
}
} catch {
Write-Host " [WARN] Failed to import metrics-server image" -ForegroundColor Yellow
}
} else {
kubectl -n kube-system delete pod $metricsPod
Write-Host " [OK] metrics-server pod restarted" -ForegroundColor Green
}
} else {
Write-Host " [OK] metrics-server is running" -ForegroundColor Green
}
} else {
Write-Host " metrics-server not found, deploying..." -ForegroundColor Yellow
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml 2>$null
}
Write-Host " Waiting for metrics-server to be ready..." -ForegroundColor Cyan
$retryCount = 0
$maxRetries = 40
while ($retryCount -lt $maxRetries) {
$msPod = kubectl -n kube-system get pods -l k8s-app=metrics-server -o jsonpath='{.items[0].status.phase}' 2>$null
$msReady = kubectl -n kube-system get pods -l k8s-app=metrics-server -o jsonpath="{.items[0].status.conditions[?(@.type=='Ready')].status}" 2>$null
if ($msPod -eq "Running" -and $msReady -eq "True") {
Write-Host " [OK] metrics-server is ready" -ForegroundColor Green
break
}
Start-Sleep -Seconds 5
$retryCount++
if ($retryCount % 8 -eq 0) {
Write-Host " Still waiting... ($retryCount/$maxRetries) - Status: $msPod" -ForegroundColor Gray
}
}
if ($retryCount -eq $maxRetries) {
Write-Host " [WARN] metrics-server may not be ready yet, HPA might not work immediately" -ForegroundColor Yellow
Write-Host " Run '.\fix-metrics-server.ps1' manually if needed" -ForegroundColor Gray
}
Write-Host "`n[4/7] Deploy to K3s..." -ForegroundColor Yellow
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/pvc.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl apply -f k8s/hpa.yaml
Write-Host "[OK] Resources created" -ForegroundColor Green
Write-Host "`n[5/7] Wait for Pod ready..." -ForegroundColor Yellow
@@ -66,11 +133,25 @@ kubectl get svc data-engine-service
Write-Host "`n[7/7] Logs:" -ForegroundColor Yellow
kubectl logs -l app=data-engine --tail=50
Write-Host "`nHPA Status:" -ForegroundColor Cyan
try {
$hpaExists = kubectl get hpa data-engine-hpa 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host $hpaExists -ForegroundColor Green
kubectl describe hpa data-engine-hpa | Select-String "Metrics|Conditions" -Context 0,2
} else {
Write-Host "[WARN] HPA not available yet, please check manually later" -ForegroundColor Yellow
}
} catch {
Write-Host "[WARN] HPA status check failed, please check manually later" -ForegroundColor Yellow
}
Write-Host "`n=== Deployment Success ===" -ForegroundColor Green
Write-Host "Access: http://<node-ip>:30301" -ForegroundColor Cyan
Write-Host "`nCommands:" -ForegroundColor Yellow
Write-Host " Logs: kubectl logs -l app=data-engine -f" -ForegroundColor White
Write-Host " Status: kubectl get pods -l app=data-engine" -ForegroundColor White
Write-Host " HPA: kubectl get hpa data-engine-hpa -w" -ForegroundColor White
Write-Host " Delete: kubectl delete -f k8s/" -ForegroundColor White
Write-Host "`nCleanup..." -ForegroundColor Yellow