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 小时前
Agent 观测与优化三城巡演丨第二站深圳精彩回顾 & PPT 下载
云原生
Henry-SAP13 小时前
AI产业迎来标准化与商业化双突破
人工智能·云原生·sap·erp
IT大白鼠13 小时前
Docker 实战 ELKF 日志监控:容器日志全栈收集分析方案
运维·docker·容器
报错小能手1 天前
Kubernetes入门实战课 3
云原生·容器·kubernetes
2601_962219011 天前
版本平滑升级架构:万象生鲜系统 V4.3.3 迭代无停机更新技术拆解
微服务·云原生·架构
小张同学a.1 天前
Docker 容器实战 1—— docker 基础与镜像构建
linux·运维·docker·容器
NJCloud1 天前
Docker 容器技术入门:部署、核心概念与基础命令
运维·docker·容器
2601_962072331 天前
docker自建rustdesk-server远程桌面
运维·docker·容器
阿里云云原生1 天前
深入解析 STAROps 瑶池 Agent:如何实现从应用告警到数据库命令级的全栈 RCA?
云原生
阿里云云原生1 天前
告别手工配置:利用 Migration Skill 实现 Nginx Ingress 注解的智能分析与自动转换
云原生