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
相关推荐
爱宇阳17 小时前
WSL2 完整开发环境配置指南
容器
hopsky17 小时前
Docker Desktop 报 500
运维·docker·容器
only_Klein17 小时前
kubernetes Pod 通信过程演示
网络·kubernetes·tcpdump
曦云沐17 小时前
一键部署ROS2开发环境!Docker run命令详解与实战
docker·容器·ros2
为什么不问问神奇的海螺呢丶17 小时前
n9e categraf k8s监控配置 -cadvisor
云原生·容器·kubernetes
H Journey17 小时前
Docker swarm 集群搭建实战
运维·docker·容器
炸裂狸花猫17 小时前
开源域名代理与流量限制方案 - Cloudflare + Ingress + 自签名证书
运维·云原生·容器·kubernetes·cloudflare·waf·免费域名证书
江湖有缘18 小时前
Docker环境下使用RustScan端口扫描工具教程
运维·docker·容器
70asunflower18 小时前
镜像仓库(Image Registries)详解
linux·docker·容器
only_Klein18 小时前
jenkins流水线报错:Connection reset by peer
ci/cd·kubernetes·gitlab·jenkins·ssl