一、备份的命名空间复制到另外一个
文件名k8s.ps1
shell
# 固定命名空间
$SourceNS = "dhgx-eplus"
$TargetNS = "dhgx-eplus-backup"
# 获取源命名空间所有 Deployment
$sourceDeployments = kubectl get deployment -n $SourceNS -o json | ConvertFrom-Json
foreach ($deploy in $sourceDeployments.items) {
$deployName = $deploy.metadata.name
Write-Output "Processing Deployment: $deployName"
# 获取容器名和镜像
$containers = @{}
foreach ($c in $deploy.spec.template.spec.containers) {
$containers[$c.name] = $c.image
}
# 先生成数组,再用 -join 拼接成字符串
$imageArgsArray = $containers.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }
$imageArgs = $imageArgsArray -join " "
if ($imageArgs) {
# 替换目标命名空间 Deployment 镜像
kubectl set image deployment/$deployName -n $TargetNS $imageArgs
Write-Output "Updated images for Deployment $deployName in $TargetNS"
} else {
Write-Output "No containers found for Deployment $deployName, skipping."
}
}
PowerShell执行./k8s.ps1
二、备份模板
shell
velero backup create (任务名称) `
--include-namespaces (命名空间) `
--selector "app notin (minio,nacos,mysql)" `
-n common
velero restore create (恢复任务名称) `
--from-backup (备份名称) `
--include-namespaces (旧的命名空间) `
--namespace-mappings (旧的命名空间):(新的命名空间) `
--namespace common `
--preserve-nodeports=false
三、链接环境
shell
ktctl -n dhgx-eplus connect