容灾恢复是绝大多数企业级应用的基本要求,对于kubernetes的容灾,其中几个关键的因素是需要考虑的。
- 备份对象粒度
- 能够备份集群resource以及持续存储中的数据
- 针对Kubernetes namespace级别的备份恢复
- 原生和共有云的支持
使用velero可以对kubernetes集群进行备份和恢复,降低集群DR造成的影响。velero的基本原理就是将集群的source,以及persistent volumes中的数据备份到对象存储中,在恢复的时候将数据从对象存储中拉取下来。
Velero (formerly Heptio Ark) gives you tools to back up and restore your Kubernetes cluster resources and persistent volumes. You can run Velero with a cloud provider or on-premises. Velero lets you:
- Take backups of your cluster and restore in case of loss.
- Migrate cluster resources to other clusters.
- Replicate your production cluster to development and testing clusters.
Velero支持原生的,以及公有云环境的备份和恢复,支持的provider如下:
下面以azure为实例说明,如何使用velero对kubernetes进行备份和恢复。
前置条件
- An Azure Subscription
- resource group
- Storageaccount
- sp
其中集群备份的数据放在azure storageaccount Blob中,PV的snapshot放到指定的resourcegroup中,其中注意的是PV绑定的disk是不可以跨区打快照,比如东二的managed disk的snapshot只能放在东二,不能放到北二。
安装velero
# download velero cli
Wget https://github.com/vmware-tanzu/velero/releases/download/v1.3.0/velero-v1.3.0-linux-amd64.tar.gz
# config credentials
cat << EOF > ./credentials-velero
AZURE_SUBSCRIPTION_ID=
AZURE_TENANT_ID=
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_RESOURCE_GROUP=
AZURE_CLOUD_NAME=AzureChinaCloud
EOF
#install
velero install \
--provider azure \
--plugins velero/velero-plugin-for-microsoft-azure:v1.0.1 \
--bucket velero \
--secret-file ./credentials-velero \
--backup-location-config resourceGroup=,storageAccount=,subscriptionId= \
--snapshot-location-config apiTimeout=5m,resourceGroup=,subscriptionId=
backup
支持两个方式的备份,按需手动备份方式,就是手动触发一次备份,备份的时候可以全集群备份,或者选择指定的资源备份,选择的颗粒度包括指定的namespace,指定label的resource。还支持定时备份,指定备份时间间隔进行自动的备份。
#On-demand backups
velero backup create backup-On-demand backups --exclude-namespaces kube-node-lease,kube-public,kube-system,velero
#Scheduled backups
velero create schedule backup-every-24h --schedule="@every 24h" --snapshot-volumes=true --ttl 720h0m0s
restore
恢复的时候也是可以选择整个集群,或者指定的资源进行恢复。
#restore all
velero restore create restore-every-24h --from-backup backup-every-24h-20200831082549 --wait