Kubernetes备份恢复方案Velero快速实践

Kubernetes备份恢复方案Velero 快速实践

1. 什么是 Velero?

Velero是go语言开发的,用于提供备份和恢复 Kubernetes 集群资源和持久卷的开源工具,可在公有云平台或本地环境中运行 Velero。

Velero 的功能注意包括:

  • 对集群进行备份,并在丢失时进行恢复。
  • 将集群资源迁移到其他集群。
  • 复制生产集群到开发和测试集群。

Velero 由以下组件组成:

  • 运行在集群上的服务器
  • 本地运行的命令行客户端

2. Velero兼容性矩阵

Velero versionExpected Kubernetes version compatibilityTested on Kubernetes version
1.151.18-latest1.28.8, 1.29.8, 1.30.4 and 1.31.1
1.141.18-latest1.27.9, 1.28.9, and 1.29.4
1.131.18-latest1.26.5, 1.27.3, 1.27.8, and 1.28.3
1.121.18-latest1.25.7, 1.26.5, 1.26.7, and 1.27.3
1.111.18-latest1.23.10, 1.24.9, 1.25.5, and 1.26.1
1.101.18-latest1.22.5, 1.23.8, 1.24.6 and 1.25.1

本文环境k8s版本为1.27.6,采用verelero 1.15.0进行测试。

注意

如果用于生产环境建议严格遵守上述配套原则。

3. Velero 安装

安装 velero 客户端

这里将velero客户端安装到k8s master节点上:

# 下载
wget https://github.com/vmware-tanzu/velero/releases/download/v1.15.0/velero-v1.15.0-linux-amd64.tar.gz

# 解压安装
tar xf velero-v1.15.0-linux-amd64.tar.gz
cd velero-v1.15.0-linux-amd64
sudo mv velero /usr/local/bin

下载地址:https://github.com/vmware-tanzu/velero/releases/latest

安装 velero 服务端

本文使用minio作为velero备份的存储方案,搭建minio参考相关的文档,在minio创建备用的bucket,桶名为k8s1-velero-backup

在本地创建桶访问凭证文件velero-minio-credentials,内容如下,ak和sk对应minio的管理员和密码。

[default]
aws_access_key_id = minio
aws_secret_access_key = minio123

执行如下命令安装velero服务端:

velero install \
    --provider aws \
    --plugins harbor.jdzx.local/velero/velero-plugin-for-aws:v1.11.0 \
    --image harbor.jdzx.local/velero/velero:v1.15.0 \
    --namespace velero \
    --bucket k8s1-velero-backup \
    --default-volumes-to-fs-backup \
    --use-node-agent \
    --node-agent-pod-cpu-limit 1 \
    --node-agent-pod-mem-limit 1024Mi \
    --secret-file ./velero-minio-credentials \
    --use-volume-snapshots=false \
    --backup-location-config region=beijing,s3ForcePathStyle="true",s3Url=http://10.210.10.209:9000

参数说明:

  • --provider:指定备份存储供应商。
  • --plugins:指定 velero 插件。
  • --image:指定 velero 服务端镜像。
  • --namespace:指定 velero 服务端运行命名空间。
  • --bucket:指定 velero 服务端使用的存储桶。
  • --default-volumes-to-fs-backup:所有 Pod 卷备份使用 FSB(文件系统备份)。
  • --use-node-agent:启用文件系统备份持久卷。
  • --node-agent-pod-cpu-limit:指定 velero node-agent pod cpu限制。
  • --node-agent-pod-mem-limit:指定 velero node-agent pod 内存限制。
  • --secret-file:指定 velero 服务端使用的存储桶访问凭证文件。
  • --use-volume-snapshots:指定 velero 是否使用卷快照。
  • --backup-location-config:指定 velero 使用的备份存储桶信息,这里对接minio,根据自己的环境信息进行修改。

示例输出:

CustomResourceDefinition/backuprepositories.velero.io: attempting to create resource
CustomResourceDefinition/backuprepositories.velero.io: attempting to create resource client
CustomResourceDefinition/backuprepositories.velero.io: created
CustomResourceDefinition/backups.velero.io: attempting to create resource
CustomResourceDefinition/backups.velero.io: attempting to create resource client
CustomResourceDefinition/backups.velero.io: created
CustomResourceDefinition/backupstoragelocations.velero.io: attempting to create resource
CustomResourceDefinition/backupstoragelocations.velero.io: attempting to create resource client
CustomResourceDefinition/backupstoragelocations.velero.io: created
CustomResourceDefinition/deletebackuprequests.velero.io: attempting to create resource
CustomResourceDefinition/deletebackuprequests.velero.io: attempting to create resource client
CustomResourceDefinition/deletebackuprequests.velero.io: created
CustomResourceDefinition/downloadrequests.velero.io: attempting to create resource
CustomResourceDefinition/downloadrequests.velero.io: attempting to create resource client
CustomResourceDefinition/downloadrequests.velero.io: created
CustomResourceDefinition/podvolumebackups.velero.io: attempting to create resource
CustomResourceDefinition/podvolumebackups.velero.io: attempting to create resource client
CustomResourceDefinition/podvolumebackups.velero.io: created
CustomResourceDefinition/podvolumerestores.velero.io: attempting to create resource
CustomResourceDefinition/podvolumerestores.velero.io: attempting to create resource client
CustomResourceDefinition/podvolumerestores.velero.io: created
CustomResourceDefinition/restores.velero.io: attempting to create resource
CustomResourceDefinition/restores.velero.io: attempting to create resource client
CustomResourceDefinition/restores.velero.io: created
CustomResourceDefinition/schedules.velero.io: attempting to create resource
CustomResourceDefinition/schedules.velero.io: attempting to create resource client
CustomResourceDefinition/schedules.velero.io: created
CustomResourceDefinition/serverstatusrequests.velero.io: attempting to create resource
CustomResourceDefinition/serverstatusrequests.velero.io: attempting to create resource client
CustomResourceDefinition/serverstatusrequests.velero.io: created
CustomResourceDefinition/volumesnapshotlocations.velero.io: attempting to create resource
CustomResourceDefinition/volumesnapshotlocations.velero.io: attempting to create resource client
CustomResourceDefinition/volumesnapshotlocations.velero.io: created
CustomResourceDefinition/datadownloads.velero.io: attempting to create resource
CustomResourceDefinition/datadownloads.velero.io: attempting to create resource client
CustomResourceDefinition/datadownloads.velero.io: created
CustomResourceDefinition/datauploads.velero.io: attempting to create resource
CustomResourceDefinition/datauploads.velero.io: attempting to create resource client
CustomResourceDefinition/datauploads.velero.io: created
Waiting for resources to be ready in cluster...
Namespace/velero: attempting to create resource
Namespace/velero: attempting to create resource client
Namespace/velero: created
ClusterRoleBinding/velero: attempting to create resource
ClusterRoleBinding/velero: attempting to create resource client
ClusterRoleBinding/velero: created
ServiceAccount/velero: attempting to create resource
ServiceAccount/velero: attempting to create resource client
ServiceAccount/velero: created
Secret/cloud-credentials: attempting to create resource
Secret/cloud-credentials: attempting to create resource client
Secret/cloud-credentials: created
BackupStorageLocation/default: attempting to create resource
BackupStorageLocation/default: attempting to create resource client
BackupStorageLocation/default: created
Deployment/velero: attempting to create resource
Deployment/velero: attempting to create resource client
Deployment/velero: created
DaemonSet/node-agent: attempting to create resource
DaemonSet/node-agent: attempting to create resource client
DaemonSet/node-agent: created
Velero is installed! ⛵ Use 'kubectl logs deployment/velero -n velero' to view the status.

确认velero组件运行状态正常,verelro使用deployment部署,单副本;node-agent使用daemonset部署,每个节点一个pod。

[root@k8s-master1 velero]# kubectl get pod -n velero
NAME                      READY   STATUS    RESTARTS   AGE
node-agent-5tpd7          1/1     Running   0          4s
node-agent-scb72          1/1     Running   0          4s
node-agent-v8w69          1/1     Running   0          4s
node-agent-vjtlr          1/1     Running   0          4s
velero-5bdddf5655-nvwsk   1/1     Running   0          4s

4. 备份恢复测试

部署测试的应用

velero安装包自带了一个应用用于测试验证:

kubectl apply -f examples/nginx-app/base.yaml

部署一个nginx应用:

]# kubectl get pod -n nginx-example
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5546f4477f-f768z   1/1     Running   0          44s
nginx-deployment-5546f4477f-h9c9x   1/1     Running   0          22s

备份应用

  1. 为匹配 app=nginx 标签选择器的任何对象创建备份:
velero backup create nginx-backup --selector app=nginx

测试应用数据量较小,备份应该很快即可完成。备份成功后,即可在minio的备份桶中看到备份的数据。

其他相关命令:

  • 查看备份列表:velero get backups
  • 查看备份详情:velero backup describe <BACKUP_NAME> --detail
  • 备份所有资源:velero backup create <BACKUP_NAME>
  1. 模拟灾难:

这里通过删除应用所在命名空间,模拟应用故障。

kubectl delete namespace nginx-example
  1. 检查应用此时已经被删除
kubectl get deployments --namespace=nginx-example
kubectl get services --namespace=nginx-example
kubectl get namespace/nginx-example

下面进行恢复。

恢复应用

  1. 创建恢复任务:
velero restore create --from-backup nginx-backup
  1. 查看恢复状态:
velero restore get
# 或者
velero get restores

等待恢复完成,输出参考如下:

NAME                          BACKUP         STATUS      STARTED                         COMPLETED                       ERRORS   WARNINGS   CREATED                         SELECTOR
nginx-backup-20241205182946   nginx-backup   Completed   2024-12-05 18:29:46 +0800 CST   2024-12-05 18:29:46 +0800 CST   0        1          2024-12-05 18:29:46 +0800 CST   <none>

注意: 恢复操作可能需要一些时间才能完成。在此期间,STATUS 列显示为 InProgress

恢复成功后,STATUS 列会显示为 Completed,并且 WARNINGSERRORS 均为 0。nginx-example 命名空间中的所有对象应该会恢复到删除前的状态。

如果存在错误或警告,您可以详细查看:

velero restore describe <RESTORE_NAME> --detail
  1. 查看恢复后的应用:
]# kubectl get pod -n nginx-example
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5546f4477f-f768z   1/1     Running   0          18s
nginx-deployment-5546f4477f-h9c9x   1/1     Running   0          18s

测试验证应用恢复正常。

5. Velero 工作流程说明

主要包括备份流程和恢复流程。

备份流程

当运行 velero backup create test-backup 时:

  1. Velero 客户端调用 Kubernetes API 服务器以创建一个 Backup 对象。
  2. BackupController 发现新的 Backup 对象并进行验证。
  3. BackupController 开始备份过程,通过查询 API 服务器获取要备份的资源数据。
  4. BackupController 调用对象存储服务(例如,AWS S3)上传备份文件。

默认情况下,velero backup create 会对任何持久卷进行磁盘快照。可以通过指定额外的标志来调整快照设置。运行 velero backup create --help 可以查看可用的标志。可以使用选项 --snapshot-volumes=false 来禁用快照。

bakcup-process

恢复流程

当运行 velero restore create 时:

  1. Velero 客户端会调用 Kubernetes API 服务器来创建一个 Restore 对象。
  2. RestoreController 发现新的 Restore 对象并进行验证。
  3. RestoreController 从对象存储服务中获取备份信息,并对备份的资源进行一些预处理,确保这些资源在新集群上能够正常运行。例如,使用备份的 API 版本验证恢复的资源是否能在目标集群上正常工作。
  4. RestoreController 开始恢复过程,逐一恢复每个符合条件的资源。

默认情况下,Velero 执行非破坏性恢复,即不会删除目标集群上的任何数据。如果备份中的资源已存在于目标集群中,Velero 将跳过该资源。可以通过使用 --existing-resource-policy 恢复标志来配置 Velero 使用更新策略。当此标志设置为 update 时,Velero 将尝试更新目标集群中的现有资源,使其与备份中的资源匹配。

有关 Velero 恢复过程的更多详细信息,请参阅恢复参考页面

6. velero 卸载

如果希望从集群中完全卸载 Velero,可以使用以下命令删除由 velero install 创建的所有资源:

velero uninstall

7. 相关资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lldhsds

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值