您可以使用独立 Velero 和 Restic 备份和还原在 TKG 集群上运行的工作负载。此方法可以替代使用 适用于 vSphere 的 Velero 插件。使用独立 Velero 的主要原因是能够获得可移植性。对于有状态工作负载,需要使用 Restic。
必备条件
要使用独立 Velero 和 Restic 备份和还原 TKG 集群上的工作负载,必须在目标集群上安装独立版本的 Velero 和 Restic。如果要在单独的目标集群上执行还原,还必须在目标集群上安装 Velero 和 Restic。请参见在 TKG 集群上安装和配置独立 Velero 和 Restic。
备份在 TKG 集群上运行的无状态应用程序
备份在 TKG 集群上运行的无状态应用程序需要使用 Velero。
--include namespaces
标记备份和还原示例无状态应用程序,其中所有应用程序组件都位于该命名空间中。
velero backup create example-backup --include-namespaces example-backup
Backup request "example-backup" submitted successfully. Run `velero backup describe example-backup` or `velero backup logs example-backup` for more details.
velero backup get
velero backup describe example-backup
检查与 S3 兼容的对象存储(如 MinIO 服务器)上的 Velero 存储桶。
kubectl get crd
kubectl get backups.velero.io -n velero
kubectl describe backups.velero.io guestbook-backup -n velero
还原在 TKG 集群上运行的无状态应用程序
还原 TKG 集群上运行的无状态应用程序需要使用 Velero。
要测试示例应用程序的还原情况,请将其删除。
kubectl delete ns guestbook namespace "guestbook" deleted
velero restore create --from-backup example-backup
Restore request "example-backup-20200721145620" submitted successfully. Run `velero restore describe example-backup-20200721145620` or `velero restore logs example-backup-20200721145620` for more details.
velero restore describe example-backup-20200721145620
velero restore get
kubectl get ns
kubectl get pod -n example
kubectl get svc -n example
备份在 TKG 集群上运行的有状态应用程序
要备份在 TKG 集群上的有状态应用程序,需要备份存储在持久卷上的应用程序元数据和应用程序数据。为此,需要使用 Velero 和 Restic。
对于此示例,我们将使用 Guestbook 应用程序。假设您已将 Guestbook 应用程序部署到 TKG 集群。请参见在 TKG 集群上部署 Guestbook 应用程序。
为了能够演示有状态备份和还原,请使用前端网页向 Guestbook 应用程序提交一些消息,以便保留这些消息。例如:
--include namespace
标记以及 Pod 注释备份和还原 Guestbook 应用程序。
--default-volumes-to-restic
选项。这将使用 Restic 自动备份所有 PVS。有关详细信息,请参见
https://velero.io/docs/v1.5/restic/。
kubectl get pod -n guestbook
kubectl get pod -n guestbook NAME READY STATUS RESTARTS AGE guestbook-frontend-deployment-85595f5bf9-h8cff 1/1 Running 0 55m guestbook-frontend-deployment-85595f5bf9-lw6tg 1/1 Running 0 55m guestbook-frontend-deployment-85595f5bf9-wpqc8 1/1 Running 0 55m redis-leader-deployment-64fb8775bf-kbs6s 1/1 Running 0 55m redis-follower-deployment-84cd76b975-jrn8v 1/1 Running 0 55m redis-follower-deployment-69df9b5688-zml4f 1/1 Running 0 55m
持久卷将附加到 Redis Pod。由于我们将使用 Restic 备份这些有状态 Pod,因此需要借助 volumeMount
名称将注释添加到有状态 Pod。
volumeMount
,才能为有状态 Pod 添加注释。要获取
mountName
,请运行以下命令。
kubectl describe pod redis-leader-deployment-64fb8775bf-kbs6s -n guestbook
您将在结果中看到来自 redis-leader-data
的 Containers.leader.Mounts: /data
。这最后一个令牌是用于主节点 Pod 注释的 volumeMount
名称。对于从节点,将为 redis-follower-data
。还可以从源 YAML 获取 volumeMount
名称。
kubectl -n guestbook annotate pod redis-leader-64fb8775bf-kbs6s backup.velero.io/backup-volumes=redis-leader-data
pod/redis-leader-64fb8775bf-kbs6s annotated
kubectl -n guestbook describe pod redis-leader-64fb8775bf-kbs6s | grep Annotations Annotations: backup.velero.io/backup-volumes: redis-leader-data
kubectl -n guestbook describe pod redis-follower-779b6d8f79-5dphr | grep Annotations Annotations: backup.velero.io/backup-volumes: redis-follower-data
velero backup create guestbook-backup --include-namespaces guestbook
Backup request "guestbook-backup" submitted successfully. Run `velero backup describe guestbook-pv-backup` or `velero backup logs guestbook-pv-backup` for more details.
velero backup get NAME STATUS ERRORS WARNINGS CREATED EXPIRES STORAGE LOCATION SELECTOR guestbook-backup Completed 0 0 2020-07-23 16:13:46 -0700 PDT 29d default <none>
velero backup describe guestbook-backup --details
kubectl get backups.velero.io -n velero NAME AGE guestbook-backup 4m58s
kubectl describe backups.velero.io guestbook-backup -n velero
还原在 TKG 2.0 集群上运行的有状态应用程序
要还原在 TKG 集群上运行的有状态应用程序,需要还原存储在持久卷中的应用程序元数据和应用程序数据。为此,需要使用 Velero 和 Restic。
此示例假设您备份了有状态 Guestbook 应用程序,如上一部分中所述。
kubectl delete ns guestbook namespace "guestbook" deleted
kubectl get ns kubectl get pvc,pv --all-namespaces
velero restore create --from-backup <velero-backup-name>
velero restore create --from-backup guestbook-backup
Restore request "guestbook-backup-20200723161841" submitted successfully. Run `velero restore describe guestbook-backup-20200723161841` or `velero restore logs guestbook-backup-20200723161841` for more details.
velero restore describe guestbook-backup-20200723161841 Name: guestbook-backup-20200723161841 Namespace: velero Labels: <none> Annotations: <none> Phase: Completed Backup: guestbook-backup Namespaces: Included: all namespaces found in the backup Excluded: <none> Resources: Included: * Excluded: nodes, events, events.events.k8s.io, backups.velero.io, restores.velero.io, resticrepositories.velero.io Cluster-scoped: auto Namespace mappings: <none> Label selector: <none> Restore PVs: auto Restic Restores (specify --details for more information): Completed: 3
velero restore get NAME BACKUP STATUS ERRORS WARNINGS CREATED SELECTOR guestbook-backup-20200723161841 guestbook-backup Completed 0 0 2021-08-11 16:18:41 -0700 PDT <none>
kubectl get ns NAME STATUS AGE default Active 16d guestbook Active 76s ... velero Active 2d2h
vkubectl get all -n guestbook NAME READY STATUS RESTARTS AGE pod/frontend-6cb7f8bd65-h2pnb 1/1 Running 0 6m27s pod/frontend-6cb7f8bd65-kwlpr 1/1 Running 0 6m27s pod/frontend-6cb7f8bd65-snwl4 1/1 Running 0 6m27s pod/redis-leader-64fb8775bf-kbs6s 1/1 Running 0 6m28s pod/redis-follower-779b6d8f79-5dphr 1/1 Running 0 6m28s pod/redis-follower-899c7e2z65-8apnk 1/1 Running 0 6m28s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/guestbook-frontend LoadBalancer 10.10.89.59 10.19.15.99 80:31513/TCP 65s service/redis-follower ClusterIP 10.111.163.189 <none> 6379/TCP 65s service/redis-leader ClusterIP 10.111.70.189 <none> 6379/TCP 65s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/guestbook-frontend-deployment 3/3 3 3 65s deployment.apps/redis-follower-deployment 1/2 2 1 65s deployment.apps/redis-leader-deployment 1/1 1 1 65s NAME DESIRED CURRENT READY AGE replicaset.apps/guestbook-frontend-deployment-56fc5b6b47 3 3 3 65s replicaset.apps/redis-follower-deployment-6fc9cf5759 2 2 1 65s replicaset.apps/redis-leader-deployment-7d89bbdbcf 1 1 1 65s
kubectl get pvc,pv -n guestbook NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE persistentvolumeclaim/redis-leader-claim Bound pvc-a2f6e6d4-42db-4fb8-a198-5379a2552509 2Gi RWO thin-disk 2m40s persistentvolumeclaim/redis-follower-claim Bound pvc-55591938-921f-452a-b418-2cc680c0560b 2Gi RWO thin-disk 2m40s NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE persistentvolume/pvc-55591938-921f-452a-b418-2cc680c0560b 2Gi RWO Delete Bound guestbook/redis-follower-claim thin-disk 2m40s persistentvolume/pvc-a2f6e6d4-42db-4fb8-a198-5379a2552509 2Gi RWO Delete Bound guestbook/redis-leader-claim thin-disk 2m40s
最后,使用 guestbook-frontend 服务的外部 IP 访问 Guestbook 前端,并验证您在教程开头提交的消息是否已还原。例如: