Files
data-engine/deploy-k3s.ps1

163 lines
7.0 KiB
PowerShell
Raw Normal View History

2026-04-16 09:26:00 +08:00
Write-Host "=== Data Engine K3s Deploy ===" -ForegroundColor Cyan
$ErrorActionPreference = "Stop"
Write-Host "`n[1/7] Handle private dependencies..." -ForegroundColor Yellow
if (Test-Path "..\common") {
Copy-Item -Path "..\common" -Destination ".\common" -Recurse -Force
$content = Get-Content go.mod -Raw
2026-06-10 16:10:09 +08:00
$content = $content -replace 'replace gitea\.com/red-future/common => \.\./common', 'replace gitea.redpowerfuture.com/red-future/common => ./common'
2026-04-16 09:26:00 +08:00
Set-Content go.mod $content -NoNewline
Write-Host "[OK] Dependencies ready" -ForegroundColor Green
} else {
Write-Host "[ERROR] common directory not found" -ForegroundColor Red
exit 1
}
if (-not (Test-Path "k8s")) {
New-Item -ItemType Directory -Path "k8s" | Out-Null
}
Write-Host "`n[2/7] Build Docker image..." -ForegroundColor Yellow
docker build -t data-engine:latest .
if ($LASTEXITCODE -ne 0) {
Write-Host "[ERROR] Build failed" -ForegroundColor Red
exit 1
}
Write-Host "[OK] Image built" -ForegroundColor Green
Write-Host "`n[3/7] Import image to k3s container..." -ForegroundColor Yellow
$tarPath = "$PWD\data-engine.tar"
Write-Host "Saving image to tar..." -ForegroundColor Cyan
docker save -o $tarPath data-engine:latest
$k3sContainer = "k3s-server"
Write-Host "Copying to k3s container..." -ForegroundColor Cyan
docker cp $tarPath "${k3sContainer}:/tmp/data-engine.tar"
Write-Host "Importing in container..." -ForegroundColor Cyan
docker exec $k3sContainer ctr -n k8s.io images import /tmp/data-engine.tar
docker exec $k3sContainer rm /tmp/data-engine.tar
Remove-Item $tarPath -ErrorAction SilentlyContinue
if ($LASTEXITCODE -ne 0) {
Write-Host "[ERROR] Import failed" -ForegroundColor Red
exit 1
}
Write-Host "[OK] Image imported" -ForegroundColor Green
2026-04-17 17:46:13 +08:00
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
}
2026-04-16 09:26:00 +08:00
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
2026-04-17 17:46:13 +08:00
kubectl apply -f k8s/hpa.yaml
2026-04-16 09:26:00 +08:00
Write-Host "[OK] Resources created" -ForegroundColor Green
Write-Host "`n[5/7] Wait for Pod ready..." -ForegroundColor Yellow
kubectl wait --for=condition=ready --timeout=180s pod -l app=data-engine
Write-Host "[OK] Pod is ready" -ForegroundColor Green
Write-Host "`n[6/7] Status:" -ForegroundColor Yellow
kubectl get pods -l app=data-engine
kubectl get svc data-engine-service
Write-Host "`n[7/7] Logs:" -ForegroundColor Yellow
kubectl logs -l app=data-engine --tail=50
2026-04-17 17:46:13 +08:00
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
}
2026-04-16 09:26:00 +08:00
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
2026-04-17 17:46:13 +08:00
Write-Host " HPA: kubectl get hpa data-engine-hpa -w" -ForegroundColor White
2026-04-16 09:26:00 +08:00
Write-Host " Delete: kubectl delete -f k8s/" -ForegroundColor White
Write-Host "`nCleanup..." -ForegroundColor Yellow
if (Test-Path ".\common") {
Remove-Item -Path ".\common" -Recurse -Force
}
git checkout go.mod 2>$null
Write-Host "[OK] Done" -ForegroundColor Green