k8s集群备份脚本

背景说明

k8s集群迁移主要有2种:1.通过etcd mirror迁移数据; 2.通过导出k8s yaml文件。前者etcd导入的数据过多,控制精度不高。后者控制精度更高,更倾向于后者。

备份脚本

k8s-migrate.sh

#!/bin/bash
# crontab定期执行备份脚本, 每半小时备份一次.
set -eu

# k8s yaml文件备份路径
BACKUP_DIR='/data/simple-cloud-backup/k8s'

# 创建k8s yaml备份总目录
[ ! -d $BACKUP_DIR ] && mkdir -p $BACKUP_DIR
mkdir -p $BACKUP_DIR/secret

# 当前日期
DATE=`date +%Y%m%d-%H%M%S`

# k8s备份分目录, except rc,rs,sc or in monitoring,kube-system
mkdir -p ${BACKUP_DIR}/${DATE}

# 获取yaml文件 except rc,rs,sc or in monitoring,kube-system
for ns in $(kubectl get ns --no-headers | cut -d " " -f1); do
  if { [[ "$ns" != "monitoring" && "$ns" != "kube-system" ]]; }; then
  kubectl --namespace="${ns}" get --export -o=json svc,pvc,deployments,cm,ing,ds,statefulset | \
jq '.items[] |
    select(.type!="kubernetes.io/service-account-token") |
    del(
        .spec.clusterIP,
        .metadata.uid,
        .metadata.selfLink,
        .metadata.resourceVersion,
        .metadata.creationTimestamp,
        .metadata.generation,
        .metadata.annotations,
        .spec.cephfs,
        .spec.volumeName,
        .status,
        .spec.template.spec.securityContext,
        .spec.template.spec.dnsPolicy,
        .spec.template.spec.terminationGracePeriodSeconds,
        .spec.template.spec.restartPolicy
    )' >> "${BACKUP_DIR}/${DATE}/${ns}.json"
  fi
done

echo "save k8s yaml files in ${BACKUP_DIR}/${DATE}"
# 保留最近7个本地备份
cd $BACKUP_DIR;ls -lt $BACKUP_DIR|awk '{if(NR>8){print "rm -rf "$9}}'|sh

# 保存harbor-admin-sercet
kubectl get secrets  --all-namespaces |grep harbor-admin |awk '{print $1,$2}' | xargs -l bash -c 'kubectl get secrets harbor-admin -n $0  -o yaml > /data/simple-cloud-backup/k8s/secret/$0.yaml'

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐