velero备份k8s

1.简介

Velero 是vmware开源的一个云原生的灾难恢复和迁移工具,意思是帆船,非常符合Kubernetes社区的命名风格,它本身也是开源的,采用Go语言编写,可以安全的备份、恢复和迁移Kubernetes集群资源数据;官网https://velero.io/。

它支持标准的K8S集群,既可以是私有云平台也可以是公有云,除了灾备之外它还能做资源移转,支持把容器应用从一个集群迁移到另一个集群。

它的工作方式就是把kubernetes中的数据备份到对象存储以实现高可用和持久化,默认的备份保存时间为720小时,并在需要的时候进行下载和恢复。

它由一个客户端和一个服务端组成

客户端:运行在本地的命令行工具,只要配置好kubectl和kubeconfig认证文件就可使用,非常简单

服务端:运行在Kubernetes集群之上,负责执行具体的备份和恢复操作

2.安装搭建velero

备注:采用minio作为velero的后端存储

master节点部署velero

# 下载客户端
wget https://github.com/vmware-tanzu/velero/releases/download/v1.13.1/velero-v1.13.1-linux-amd64.tar.gz
 
# 解压
tar -zxvf velero-v1.13.1-linux-amd64.tar.gz
 
# 拷贝到/usr/bin目录下
chmod +x velero-v1.13.1-linux-amd64/velero
 
cp velero-v1.13.1-linux-amd64/velero /usr/bin/
 
# 检查验证
velero --version

配置velero连接minio的配置

备注:

# 创建工作目录 mkdir -p /opt/velero
# 创建访问minio的认证文件,这个velero-auth.txt文件中记录了访问对象存储minio的用户名和密码
# aws_access_key_id这个变量用来指定对象存储用户名
# aws_secret_access_key变量用来指定密码

cat >/opt/velero/velero-auth.txt << EOF
[default]
aws_access_key_id = admin
aws_secret_access_key = 123456
EOF

创建bucket:

在minio中创建一个bucket,命名为:k8s-backup

服务端安装部署到k8s集群

velero install \
--kubeconfig  /root/.kube/config \
--provider aws \
--plugins velero/velero-plugin-for-aws:latest \
--use-volume-snapshots=false  \
--uploader-type=restic \
--use-node-agent \
--bucket k8s-backup \
--secret-file /opt/velero/velero-auth.txt \
--log_file_max_size 512 \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.1.1:9000

验证是否安装完毕

kubectl get pod -n velero 
NAME                     READY   STATUS    RESTARTS   AGE
velero-xxxxxxxx   1/1     Running   0          47s

kubectl  api-versions  | grep velero
velero.io/v1

常用命令:

velero create backup NAME [flags]
# 剔除 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

数据备份

单次备份执行以下命令,该命令会默认备份集群所有资源(持久卷不会备份)

velero backup create <backupName>

备份指定namespace下的资源,则可以加--include-namespaces参数

velero backup create <backupName> --include-namespaces ns1,ns2

可通过下面命令查看任务状态

velero backup get

备份有效期默认是30天,到期自动删除。如果需要手动删除,则执行命令

velero backup delete <backupName>

定时备份

velero schedule create <backupName> --schedule="0 */12 * * *"

恢复数据

kubectl delete pod test
velero restore create --from-backup k8s-backup-20250101 --wait
# 等待y一分钟
kubectl get pod

常用的恢复命令

velero restore get:查看已经restore的资源
 
velero restore create restore-1 --from-backup backup-1:从backup-1恢复

# 仅恢复指定空间资源
velero restore create --from-backup backup-2 --include-namespace=default