k8s备份工具之velero
veleo备份原理
https://blog.csdn.net/weixin_42914965/article/details/106035429(ds安装minio)

-
本地
Velero客户端发送备份指令。 -
Kubernetes集群内就会创建一个Backup对象。 -
BackupController监测Backup对象并开始备份过程。 -
BackupController会向API Server查询相关数据。 -
BackupController将查询到的数据备份到远端的对象存储。
Velero 在 Kubernetes 集群中创建了很多 CRD 以及相关的控制器,进行备份恢复等操作实质上是对相关 CRD 的操作。
Velero 支持的后端存储
Velero 支持两种关于后端存储的 CRD,分别是 BackupStorageLocation 和 VolumeSnapshotLocation。
-
BackupStorageLocation
BackupStorageLocation 主要用来定义 Kubernetes 集群资源的数据存放位置,也就是集群对象数据,不是 PVC 的数据。主要支持的后端存储是 S3 兼容的存储,比如:Mino 和阿里云 OSS 等。
使用 Minio
apiVersion: velero.io/v1
kind: BackupStorageLocation
metadata:
name: default
namespace: velero
spec:
# 只有 aws gcp azure
provider: aws
# 存储主要配置
objectStorage:
# bucket 的名称
bucket: myBucket
# bucket内的
prefix: backup
# 不同的 provider 不同的配置
config:
#bucket地区
region: us-west-2
# s3认证信息
profile: "default"
# 使用 Minio 的时候加上,默认为 false
# AWS 的 S3 可以支持两种 Url Bucket URL
# 1 Path style URL:http://s3endpoint/BUCKET
# 2 Virtual-hosted style URL:http://oss-cn-beijing.s3endpoint 将 Bucker Name 放到了 Host Header中
# 3 阿里云仅仅支持 Virtual hosted 如果下面写上 true, 阿里云 OSS 会报错 403
s3ForcePathStyle: "false"
# s3的地址,格式为 http://minio:9000
s3Url: http://minio:9000
使用阿里云的 OSS
apiVersion: velero.io/v1
kind: BackupStorageLocation
metadata:
labels:
component: velero
name: default
namespace: velero
spec:
config:
region: oss-cn-beijing
s3Url: http://oss-cn-beijing.aliyuncs.com
s3ForcePathStyle: "false"
objectStorage:
bucket: build-jenkins
prefix: ""
provider: aws
-
VolumeSnapshotLocation
VolumeSnapshotLocation 主要用来给 PV 做快照,需要云提供商提供插件。阿里云已经提供了插件,这个需要使用 CSI 等存储机制。你也可以使用专门的备份工具 Restic,把 PV 数据备份到阿里云 OSS 中去(安装时需要自定义选项)。
# 安装时需要自定义选项
--use-restic
# 这里我们存储 PV 使用的是 OSS 也就是 BackupStorageLocation,因此不用创建 VolumeSnapshotLocation 对象
--use-volume-snapshots=false
安装veleo
1、下载
https://github.com/vmware-tanzu/velero/releases/download/v1.4.0/velero-v1.4.0-linux-amd64.tar.gz tar zxvf velero-v1.4.0-linux-amd64.tar.gz cd velero-v1.4.0-linux-amd64
mv velero /usr/local/bin/
2、安装miniio
创建minio凭证
vi examples/minio/credentials-velero
[default] aws_access_key_id = minio aws_secret_access_key = minio123
3、创建minio对象存储
kubectl create -f examples/minio/00-minio-deployment.yaml
namespace/velero created deployment.apps/minio created service/minio created job.batch/minio-setup created
4、开放端口
kubectl expose deployment minio -n velero --type=NodePort --name=minio-nodeport --target-port=9000
[root@master velero-v1.4.0-linux-amd64]# kubectl get svc -n velero NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE minio ClusterIP 10.254.155.172 <none> 9000/TCP 5m24s minio-nodeport NodePort 10.254.203.212 <none> 9000:32027/TCP 2s [root@master velero-v1.4.0-linux-amd64]#
5、登录minio,地址:宿主机IP:32152,查看buckets(已创建名为velero的bucket)

6. 安装velero (使用本地集群minio作为备份存储)
velero install
--provider aws
--bucket velero
--secret-file examples/minio/credentials-velero
--use-volume-snapshots=false
--plugins velero/velero-plugin-for-aws:v1.0.0
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.178.7.5:32027
provider 只有 aws gcp azure
bucket 备份位置,之前minio页面只有velero这一个,所以使用它做备份位置
[root@master velero-v1.4.0-linux-amd64]# kubectl get pod -n velero NAME READY STATUS RESTARTS AGE minio-557f9875fd-8g65x 1/1 Running 0 107m minio-setup-wbgtn 0/1 Completed 3 107m velero-6fb968d5cc-v7t6s 1/1 Running 0 34s [root@master velero-v1.4.0-linux-amd64]#
7、对某个namespace备份
[root@master velero-v1.4.0-linux-amd64]# kubectl get po -n test NAME READY STATUS RESTARTS AGE centos-5f9b47d65-nrjvc 1/1 Running 4 9d [root@master velero-v1.4.0-linux-amd64]#
8、备份test namespace
velero backup create test-backup --include-namespaces test --wait #执行备份
velero backup describe test-backup #查看备份
备份完毕后,登录minio页面可以看到数据已经在了

9、备份验证恢复
kubectl delete ns test
velero restore create --from-backup test-backup #指定对应的备份名称
再次查看,可以看到已经恢复了
[root@master velero-v1.4.0-linux-amd64]# kubectl get po -n test NAME READY STATUS RESTARTS AGE centos-5f9b47d65-nrjvc 1/1 Running 4 9d [root@master velero-v1.4.0-linux-amd64]#
此时登录minio页面可以看到执行恢复后,minio中创建了对应恢复目录及文件

10、定期备份
# 每日1点进行备份 velero create schedule <SCHEDULE NAME> --schedule="0 1 * * *" # 每日1点进行备份,备份保留72小时 velero create schedule <SCHEDULE NAME> --schedule="0 1 * * *" --ttl 72h # 每5小时进行一次备份 velero create schedule <SCHEDULE NAME> --schedule="@every 5h" # 每日对 指定 namespace 进行一次备份 (如panshi-qtc-dev) velero create schedule <SCHEDULE NAME> --schedule="@every 24h" --include-namespaces panshi-qtc-dev
11、资源查看
velero get backup #备份查看 velero get schedule #查看定时备份 velero get restore #查看已有的恢复
velero get plugins #查看插件
velero restore create --from-backup all-ns-backup #恢复集群所有备份,(对已经存在的服务不会覆盖) velero restore create --from-backup all-ns-backup --include-namespaces default,nginx-example #仅恢复 default nginx-example namespace Velero可以将资源还原到与其备份来源不同的命名空间中。为此,请使用--namespace-mappings标志 velero restore create RESTORE_NAME --from-backup BACKUP_NAME --namespace-mappings old-ns-1:new-ns-1,old-ns-2:new-ns-2 例如下面将test-velero 命名空间资源恢复到test-velero-1下面 velero restore create restore-for-test --from-backup everyday-1-20210203131802 --namespace-mappings test-velero:test-velero-1
12、备份带pv的pod
velero backup create pvc-backup --snapshot-volumes --include-namespaces test-velero
恢复
velero restore create --from-backup pvc-backup --restore-volumes
备份pv数据需要云厂商支持,参考:
https://blog.csdn.net/easylife206/article/details/102927512
https://blog.51cto.com/kaliarch/2531077?source=drh
可以先创建一个configmap自定义一些内容,可有可无的,不想自定义就不创建,参考:https://velero.io/docs/v1.5/restic/#troubleshooting
安装时velero需加上--use-restic参数表示使用restic备份pv数据
velero install --provider aws --bucket velero --secret-file examples/minio/credentials-velero --use-volume-snapshots=true --plugins velero/velero-plugin-for-aws:v1.0.0 --use-restic --snapshot-location-config region=minio
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.178.7.5:32027
restic相关知识:https://github.com/restic/restic
使用 Restic 给带有 PVC 的 Pod 进行备份,必须先给 Pod 加上注解
格式:
$ kubectl -n YOUR_POD_NAMESPACE annotate pod/YOUR_POD_NAME backup.velero.io/backup-volumes=YOUR_VOLUME_NAME_1,YOUR_VOLUME_NAME_2,...
YOUR_VOLUME_NAME_1指的是pod spec.template.spec.volumes.name的值
比如我的volumes是www,则 annotate 就要是backup-volumes=www

例如:
kubectl -n test-velero annotate pod nfs-pvc-7d75fbbcdf-dn7xw backup.velero.io/backup-volumes=www
#查看结果
kubectl get po -n test-velero nfs-pvc-7d75fbbcdf-dn7xw -o jsonpath='{.metadata.annotations}'
再次备份
velero backup create pvc-backup-2 --snapshot-volumes --include-namespaces test-velero
恢复
velero restore create --from-backup pvc-backup-2 --restore-volumes
可以看已经能把pv的数据恢复了
备份原理:https://velero.io/docs/v1.5/restic/#troubleshooting
minio 高可用
apiVersion: v1
kind: Service
metadata:
name: minio
labels:
app: minio
spec:
clusterIP: None
ports:
- port: 9000
name: minio
selector:
app: minio
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: minio
spec:
selector:
matchLabels:
app: minio
serviceName: minio
replicas: 4
template:
metadata:
labels:
app: minio
spec:
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: minio
env:
- name: MINIO_ACCESS_KEY
value: "RGifLiRPS6KFmuEq"
- name: MINIO_SECRET_KEY
value: "quTAJguUiio4fJV8"
image: minio/minio:RELEASE.2020-06-01T17-28-03Z
args:
- server
- http://minio-{0...3}.minio.default.svc.cluster.local/data
ports:
- containerPort: 9000
# These volume mounts are persistent. Each pod in the PetSet
# gets a volume mounted based on this field.
volumeMounts:
- name: data
mountPath: /data
# These are converted to volume claims by the controller
# and mounted at the paths mentioned above.
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi
# Uncomment and add storageClass specific to your requirements below. Read more https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
storageClassName: nfs-client
helm安装
客户端安装
wget https://get.helm.sh/helm-v3.2.0-linux-amd64.tar.gz tar -xf helm-v3.2.0-linux-amd64.tar.gz cd linux-amd64/ cp helm /usr/local/bin/
添加源
helm repo add aliyuncs https://apphub.aliyuncs.com
helm repo add stable https://kubernetes-charts.storage.googleapis.com
helm repo update
安装velero
添加源
[root@master velero]# helm repo add vmware-tanzu https://vmware-tanzu.github.io/helm-charts "vmware-tanzu" has been added to your repositories [root@master velero]# helm search repo vmware-tanzu NAME CHART VERSION APP VERSION DESCRIPTION vmware-tanzu/velero 2.12.0 1.4.0 A Helm chart for velero [root@master velero]#
下载安装
helm pull vmware-tanzu/velero
https://blog.csdn.net/weixin_41476014/article/details/106800677?%3E
https://zhuanlan.zhihu.com/p/92853124
https://blog.csdn.net/easylife206/article/details/102927512
https://docs.signalfx.com/en/latest/integrations/agent/monitors/prometheus-velero.html


浙公网安备 33010602011771号