

使用velero+minio+restic实现kubernetes业务数据备份与恢复
source link: https://blog.51cto.com/u_14458428/6114603
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用velero+minio+restic实现kubernetes业务数据备份与恢复
精选 原创十年过客 2023-03-11 14:42:46 ©著作权
文章标签 velero pv备份 Kubernetes数据备份 文章分类 运维 阅读数346
Velero(以前称为 Heptio Ark)为您提供了备份和恢复 Kubernetes 集群资源和持久卷的工具,可以安全的备份、恢复和迁移Kubernetes集群资源和持久卷。
Velero主要提供以下能力
- 备份群集并在丢失时进行还原。
- 将群集资源迁移到其他群集。
- 将生产群集复制到开发和测试群集。
Velero 支持备份存储
- Azure BloB 存储
- Google Cloud 存储
- AWS S3 及兼容S3 的存储(比如:MinIO)
- Aliyun OSS 存储
velero和etcd快照备份的区别:
- etcd快照备份是全局备份,即使一个资源对象需要恢复也需要做全局恢复到备份状态及会影响其他的pod和其他namespace的资源
- velero可以有针对性的备份,比如按照namespace单独备份、只备份单独资源对象等,在恢复的时候只恢复单独的namespace或资源对象,不影响其他namespace中pod的运行
- velero支持ceph,oss等对象存储,etcd快照是一个本地文件
- velero支持任务计划实现周期备份,但etcd快照备份也可以基于cronjob实现
velero存储方式:
Restic方式备份
Restic 是一款 GO 语言开发的开源免费且快速、高效和安全的跨平台备份工具。它是文件系统级别备份持久卷数据并将其发送到 Velero 的对象存储。执行速度取决于本地IO能力,网络带宽和对象存储性能,相对快照方式备份慢。 但如果当前集群或者存储出现问题,由于所有资源和数据都存储在远端的对象存储上, 用Restic方式备份可以很容易的将应用恢复。 Tips: 使用 Restic 来对 PV 进行备份会有一些限制:
- 不支持备份 hostPath,支持EFS、AzureFile、NFS、emptyDir、local 或其他没有本地快照概念的卷类型
- 备份数据标志只能通过 Pod 来识别
- 单线程操作大量文件比较慢
快照方式备份
Velero使用一组 BackupItemAction 插件针对 PersistentVolumeClaims 进行备份,执行速度快。它创建一个以 PersistentVolumeClaim 作为源的 VolumeSnapshot 对象, 此 VolumeSnapshot 对象与用作源的 PersistentVolumeClaim 位于同一命名空间中,与VolumeSnapshot对应的 VolumeSnapshotContent 对象是一个集群范围的资源,将指向存储系统中基于磁盘的实际快照。Velero 备份时将所有 VolumeSnapshots 和 VolumeSnapshotContents 对象上传到对象存储系统, 但是Velero 备份后的数据资源仍然保存在集群的存储上。数据可用性依赖于本地存储的高可用性,因为如果是由于存储故障导致的应用问题,Velero的快照备份机制并不能恢复应用数据。
搭建步骤:
安装minio存储
1、拉取minio镜像
2、启动minio
--net=host \
--name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=admin" \
-e "MINIO_SECRET_KEY=minio123" \
-v /home/minio/data:/data \
-v /home/minio/config:/root/.minio \
minio/minio server \
/data --console-address ":9090" -address ":9000"
3、创建存储桶
登陆地址: http://192.168.124.65:9090
账号:admin 密码:minio123

创建buckets,桶名:velerodata


安装velero
1、下载二进制客户端
tar -xvzf velero-v1.8.1-linux-amd64.tar.gz
mv velero /usr/local/bin
2、配置velero认证环境
cd /data/velero && cat velero-auth.txt
[default]
aws_access_key_id = admin
aws_secret_access_key = minio123
3、拉取velero服务镜像
docker pull velero/velero-plugin-for-aws:v1.4.1
4、安装velero+restic
install \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.4.1 \
--bucket velerodata \
--secret-file ./velero-auth.txt \
--use-volume-snapshots=false \
--namespace velero-system \
--use-restic \
--backup-location-config reginotallow=minio,s3ForcePathStyle="true",s3Url=http://192.168.124.65:9000
#此处会启动velero和restic pod 并创建velero使用的crd资源,等待输出Velero is installed! 表示创建成功!
如果安装出现以下报错,是因为kubernetes版本过低无法创建对应资源导致,可以升级k8s版本或者使用更低版本的velero即可。

Velero Version和Plugin Version对应关系如下:

5、检查服务运行状态:
NAME READY STATUS RESTARTS AGE
restic-mmwdh 1/1 Running 0 3d22h
velero-f7c9588d7-bwmct 1/1 Running 0 4d21h
数据备份和还原:
1、搭建演示环境
kubectl create ns test
#创建yaml文件
cat test.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: velero-pvc
namespace: test
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 1Gi
storageClassName: nfs
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: test
creationTimestamp: null
labels:
app: debugger
name: debugger
spec:
replicas: 1
selector:
matchLabels:
app: debugger
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: debugger
spec:
containers:
- image: dustise/sleep
name: sleep
volumeMounts:
- name: velero-pvc
mountPath: /data
volumes:
- name: velero-pvc
persistentVolumeClaim:
claimName: velero-pvc
#启动服务
kubectl apply -f test.yaml
#查看资源状况
kubectl get pod,pv,pvc -n test
NAME READY STATUS RESTARTS AGE
pod/debugger-dbbc7ccdd-7fgkm 1/1 Running 0 98s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-c4564d67-636c-4f6c-9e34-4eda858b4b11 1Gi RWX Delete Bound test/velero-pvc nfs 98s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/velero-pvc Bound pvc-c4564d67-636c-4f6c-9e34-4eda858b4b11 1Gi RWX nfs 98s
2、插入演示数据
bash-5.0# cd /data/
bash-5.0# touch test0311
3、执行备份
Restore request "test0311-20230311020408" submitted successfully.
Run `velero restore describe test0311-20230311020408` or `velero restore logs test0311-20230311020408` for more details.
#检查备份
[root@velero ~]# velero restore describe test0311-20230311020408 -n velero-system
Phase: Completed
1、删除namespaces
2、还原namespaces
Backup request "test0311" submitted successfully.
Run `velero backup describe test0311` or `velero backup logs test0311` for more details.
#整个namespaces已经还原
[root@aps03 ~]# kubectl get pod,pv,pvc -n test
NAME READY STATUS RESTARTS AGE
pod/debugger-dbbc7ccdd-7fgkm 1/1 Running 0 18s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-04018895-4958-426c-a6ab-951deaf0ef12 1Gi RWX Delete Bound test/velero-pvc nfs 19s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/velero-pvc Bound pvc-04018895-4958-426c-a6ab-951deaf0ef12 1Gi RWX nfs 19s
3、数据验证
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-5.0# ls /data/
#到此处已经完成演示namespaces的备份和还原操作,但是还原后可以发现pv中的数据并没有备份成功,备份的只是源数据信息,接下来演示针对pv的备份和还原操作
针对pv的备份和还原操作:
1、查询pod名称和pvc名称
NAME READY STATUS RESTARTS AGE
pod/debugger-dbbc7ccdd-7fgkm 1/1 Running 0 18m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/velero-pvc Bound pvc-04018895-4958-426c-a6ab-951deaf0ef12 1Gi RWX nfs 18m
2、进行pod和pvc的绑定
3、备份演示
[root@velero ~]# kubectl exec -it debugger-dbbc7ccdd-7fgkm -n test bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-5.0# touch /data/110
bash-5.0# ls /data/
110
#备份
[root@velero ~]# DATE=`date +%Y%m%d%H%M%S`
[root@velero ~]# velero backup create test-backup-${DATE} --include-namespaces test -n velero-system
Backup request "test-backup-20230311022930" submitted successfully.
#插入数据111
[root@velero ~]# kubectl exec -it debugger-dbbc7ccdd-7fgkm -n test bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] inst ead.
bash-5.0# touch /data/111
bash-5.0# ls /data/
110 111
#备份
[root@velero ~]# DATE=`date +%Y%m%d%H%M%S`
[root@velero ~]# velero backup create test-backup-${DATE} --include-namespaces test -n velero-system
Backup request "test-backup-20230311023127" submitted successfully.
4、还原演示
kubectl delete ns test
#还原到数据110备份
velero restore create --from-backup test-backup-20230311022930 -n velero-system
[root@velero ~]# kubectl exec -it debugger-dbbc7ccdd-7fgkm -n test bash
bash-5.0# ls /data/
110
#删除namespace
kubectl delete ns test
#还原到数据111备份
velero restore create --from-backup test-backup-20230311023127 -n velero-system
[root@velero ~]# kubectl exec -it debugger-dbbc7ccdd-7fgkm -n test bash
bash-5.0# ls /data/
110 111
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK