k8s环境脚本

一、备份的命名空间复制到另外一个

文件名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
相关推荐
阿里云云原生9 小时前
Higress v2.2.3 发布:正式入驻 CNCF Sandbox,AI Gateway 与 Ingress 迁移能力双向加固
云原生
lichenyang45315 小时前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang45315 小时前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang45315 小时前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
阿里云云原生1 天前
香港站【企业 AI Agent 工程化实战专场】来啦,邀您7月9日见!
云原生·agent
阿里云云原生1 天前
研发域与运维域的“数字握手”:通过 Agentic Skills 实现 DevOps 全链路自动化
云原生
运维开发故事3 天前
基于 Arthas 的多集群在线诊断系统设计与实现
kubernetes
Patrick_Wilson5 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
阿里云云原生5 天前
AI 开发新常态:当 Cursor、Claude、Codex 并行,如何统一管理散落的 Skill 资产?
云原生·ai编程
探索云原生6 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes