kubesphere备份与迁移

satuo20 9月前 ⋅ 278 阅读

目标

  • 实现两个集群中流水线数据的迁移

操作步骤

源环境:备份devops流水线的项目、piplines和三方项目(如springbootadmin)

backup.sh

#!/usr/bin/env bash

# 运行时命名空间,请根据实际修改
#namespance="kp-test"
# namespance="kp-dev"
namespance="kp-0731"
node="192.168.0.63"
echo "开始备份k8s系统,ns=$namespance"
# shellcheck disable=SC2154
backup_name=k8s_backup_"$node"_$(date +%Y%m%d)
mkdir "$backup_name"

cd "$backup_name"

mkdir projects pipelines secrets roles configmaps deploys
echo "开始[备份devopsprojects]......"
# 备份devopsprojects
for pname in $(kubectl get devopsprojects.devops.kubesphere.io|awk '{print $1}'); do
  if [ "$pname" != "NAME" ];
  then
    kubectl get devopsprojects.devops.kubesphere.io "$pname" -o=name -o yaml >projects/"$pname".yaml
  fi
done
echo "完成[备份devopsprojects]"


echo "开始[备份pipeline,secret,roles]......"
# 备份pipeline,secret,roles
for ns in $(kubectl get ns -ojsonpath='{.items..metadata.name}'); do
    pipelines=`kubectl get pipelines.devops.kubesphere.io -o=name -n $ns`
    if [ -z "$pipelines" ];
    then
      echo "$ns"
    else
        kubectl get pipelines.devops.kubesphere.io -o=name -n "$ns" -o yaml > pipelines/"${ns}"_backup_namesapce1-pip.yaml
        #备份凭证Secrets
        kubectl get secrets -n "$ns"  -o yaml > secrets/"${ns}"_backup_secrets.yaml
         #备份角色Roles和角色绑定RoleBindings
        kubectl get roles,rolebindings -n "$ns" -o yaml > roles/"${ns}"_backup_roles.yaml
    fi
done
echo "完成[备份pipeline,secret,roles]"

echo "开始[备份 configmap]......"
# 备份 configmap
kubectl get cm  -o yaml -n $namespance > configmaps/configmap-$namespance.yaml
echo "完成[备份 configmap]"

echo "开始[备份 deploy]......"
# 备份 deploy
kubectl get deploy -o yaml -n $namespance > deploys/deploy-$namespance.yaml
echo "完成[备份 deploy]"

echo "开始[备份service]......"
# 备份service
kubectl get rc,services -n $namespance -o yaml > services.yaml
echo "完成[备份service]"

echo "开始[备份ingress]......"
# 备份ingress
kubectl get ingress -n $namespance -o yaml > ingress-route.yaml
echo "完成[备份ingress]"

echo "备份k8s系统完成"

目标环境:

  • 创建和源环境一样的账号,企业空间并开启devops功能,并还原devops备份(账号和命名空间目前无法正常迁移)
  • 项目设置-》高级设置-》创建外网访问,然后将端口配置到nginx上

restore.sh

#!/bin/bash

echo "开始还原k8s......"

echo "开始还原projects......"
for file in projects/*; do
   kubectl apply -f "$file"
done

echo "开始还原pipelines......"
for file in pipelines/*; do
   kubectl apply -f "$file"
done

echo "开始还原secrets......"
for file in secrets/*; do
   kubectl apply -f "$file"
done

echo "开始还原roles......"
for file in roles/*; do
   kubectl apply -f "$file"
done

echo "开始还原configmaps......"
for file in configmaps/*; do
   kubectl apply -f "$file"
done

echo "开始还原deploys......"
for file in deploys/*; do
   kubectl apply -f "$file"
done

echo "开始还原services......"
kubectl apply -f services.yaml

echo "开始还原ingress......"
kubectl apply -f ingress-route.yaml

echo "还原k8s完成"

附加准备工作:

1. 数据库准备(mybatis根据代码反向生成表,解决旧版本sql维护问题)
2. 配置文件服务器准备(存放configMap和deployment的yaml配置,并使用nginx提供访问服务,类似66环境)
3. 代码仓库:用于存放代码

全部评论: 0

    我有话说: