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
相关推荐
阿里云云原生3 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
蝎子莱莱爱打怪3 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀4 天前
Docker部署Nacos
docker·容器
缓解AI焦虑4 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
阿里云云原生4 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生4 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生4 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生4 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter4 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
~莫子4 天前
Haproxy七层负载详解+实验详细代码
云原生