Kubernetes - velero备份

2024-01-24

k8s集群备份恢复方案可分为使用etcd集群备份,另一种是使用第三方工具比如velero(vmware开源)备份,这里使用velero。其大致架构:

image.png

备份流程:

  • velero客户端调用kubernetes API Server创建backup任务
  • Backup控制器基于watch机制通过Api Server获取到备份任务
  • Backup控制器开始执行备份动作,会通过请求Api Server获取到需要备份的数据
  • Backup 控制器将获取到的数据备份到指定的对象存储server端

velero存储后端:

  • BackupStorageLocation
  • 主要用来定义 Kubernetes 集群资源的数据存放位置,也就是集群对象数据,不是 PVC 的数据。主要支持的后端存储是 S3 兼容的存储,比如:Mino 和阿里云 OSS 等。
  • VolumeSnapshotLocation
  • 主要用来给 PV 做快照,需要云提供商提供插件。阿里云已经提供了插件,这个需要使用 CSI 等存储机制。也可以使用专门的备份工具 Restic(GO 语言开发的数据加密备份工具,顾名思义,可以将本地数据加密后传输到指定的仓库。支持的仓库有 Local、SFTP、Aws S3、Minio、OpenStack Swift、Backblaze B2、Azure BS、Google Cloud storage、Rest Server。),把 PV 数据备份到阿里云 OSS 中去(安装时需要自定义选项)。
  1. 安装

wget https://github.com/vmware-tanzu/velero/releases/download/v1.13.0-rc.1/velero-v1.13.0-rc.1-linux-amd64.tar.gz
tar -zxvf velero-v1.13.0-rc.1-linux-amd64.tar.gz
cp velero-v1.13.0-rc.1-linux-amd64/velero /usr/bin/

source <(velero completion bash)
velero completion bash > /etc/bash_completion.d/velero

cat >> credentials-velero <EOF 
[default]
aws_access_key_id = xxxxxx
aws_secret_access_key =xxxxxxx
EOF

#安装服务端到k8s集群
velero install  --provider aws \ 
--plugins velero/velero-plugin-for-aws:v1.9.0-rc.1  \
--prefix kubeasz/baichuan/backup \
--bucket <bucket>-<appid> \
--backup-location-config region=ap-shanghai,s3ForcePathStyle="false",s3Url=https://cos.ap-shanghai.myqcloud.com \
--secret-file ./credentials-velero \
--kubeconfig /root/.kube/config

#由于原生不支持cos,支持oss和s3,,,minio等,但是oss插件版本低,不支持高版本velero和k8s,而cos兼容s3接口,因此直接接入到cos中,aws_access_key_id和aws_secret_access_key分别是cos的id和key,bucket是cos的bucket,prefix是bucket内部的路径,要求该路径下没有多余的目录,否则启动会报错,比如腾讯云格式:[bucket]-[appid]/kubeasz/baichuan/backup

执行成功后,可看k8s集群中velero pod启动日志看是否正常,另外可能涉及到镜像拉取失败问题。

  1. 备份、还原操作

  2. 备份

  3. 常用命令参数

#备份命令参数选项
velero backup create [name] [option]

# 剔除 namespace
--exclude-namespaces stringArray                  namespaces to exclude from the backup

# 剔除资源类型
--exclude-resources stringArray                   resources to exclude from the backup, formatted as resource.group, such as storageclasses.storage.k8s.io

# 包含集群资源类型 
--include-cluster-resources optionalBool[=true]   include cluster-scoped resources in the backup

# 包含 namespace
--include-namespaces stringArray                  namespaces to include in the backup (use '*' for all namespaces) (default *)

# 包含 namespace 资源类型
--include-resources stringArray                   resources to include in the backup, formatted as resource.group, such as storageclasses.storage.k8s.io (use '*' for all resources)

# 给这个备份加上标签
--labels mapStringString                          labels to apply to the backup

#指定输出格式
-o, --output string                               Output display format. For create commands, display the object but do not send it to the server. Valid formats are 'table', 'json', and 'yaml'. 'table' is not valid for the install command.

# 对指定标签的资源进行备份
-l, --selector labelSelector                      only back up resources matching this label selector (default <none>)

# 对 PV 创建快照
--snapshot-volumes optionalBool[=true]            take snapshots of PersistentVolumes as part of the backup

# 指定备份的位置
--storage-location string                         location in which to store the backup

# 备份数据多久删掉

--ttl duration                                    how long before the backup can be garbage collected (default 720h0m0s)

# 指定快照的位置,也就是哪一个公有云驱动
--volume-snapshot-locations strings               list of locations (at most one per provider) where volume snapshots should be stored
  1. 手动备份(不带pv)

#备份不带pv的pod
DATE=`date +%F-%H-%M-%S`
k8s_ns=test-app-namespace

velero backup create ${k8s_ns}-backup-${DATE} \
--include-namespaces ${k8s_ns} \
--kubeconfig=/root/.kube/config \
--namespace velero

#也可以备份到与原来不同的命名空间中,使用--namespace-mappings标志
# 例如下面将 redis 命名空间资源恢复到 redis-bak 下面
DATE=`date +%F-%H-%M-%S`
k8s_ns=test-app-namespace

velero backup create ${k8s_ns}-backup-${DATE} \
--include-namespaces ${k8s_ns} \
--kubeconfig=/root/.kube/config \
--namespace velero

#备份所有命名空间
velero backup create <backup-name>

image.png

image.png

image.png

  1. 手动备份(带pv)

#备份带pv的pod

DATE=`date +%F-%H-%M-%S`
k8s_ns=test-app-namespace

velero backup create ${k8s_ns}-backup-${DATE} \
--include-namespaces ${k8s_ns} \
--default-volumes-to-fs-backup \
--kubeconfig=/root/.kube/config \
--namespace velero
  1. 定时备份

#创建备份计划,每天0点备份,备份数据保留7天
k8s_ns=test-app-namespace

velero schedule create ${k8s_ns}-backup \
--schedule="0 0 * * *" \
--ttl 168h0m0s \
--include-namespaces ${k8s_ns} \
--default-volumes-to-fs-backup \
--kubeconfig=/root/.kube/config \
--namespace velero

#每小时备份,备份保留7天
k8s_ns=kube-system

velero schedule create ${k8s_ns}-backup \
--schedule="0 * * * *" \
--ttl 168h0m0s \
--include-namespaces ${k8s_ns} \
--kubeconfig=/root/.kube/config \
--namespace velero

velero schedule get --kubeconfig=/root/.kube/config --namespace velero-system

#手动触发定时任务
velero backup create --from-schedule ${k8s_ns}-backup 
  1. 集群恢复

删除test-app-namespace下所有资源验证还原:

  • 删除前image.png

  • 删除后
    image.png

执行还原:

#恢复
velero restore create --from-backup "test-app-namespace-backup-2024-01-23-23-36-26" --wait --kubeconfig=/root/.kube/config --namespace velero

image.png

image.png

  1. 参考

https://github.com/vmware-tanzu/velero
https://velero.io/docs/v1.13/supported-providers/
https://cloud.tencent.com/developer/article/1534154
https://blog.csdn.net/princeteng/article/details/126333151


标题:Kubernetes - velero备份
地址:https://blog.njqhome.com:8443/articles/2024/01/24/1706079938310.html