Files
data-engine/deploy-k3s.ps1
2026-04-16 09:26:00 +08:00

82 lines
3.0 KiB
PowerShell

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
$content = $content -replace 'replace gitea\.com/red-future/common => \.\./common', 'replace gitea.com/red-future/common => ./common'
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
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
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
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 " 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