当前位置:网站首页>详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
2022-06-24 12:57:00 【InfoQ】
前言
Kubernetes备份恢复利器:velero
Velero工作流程
- Velero 客户端首先调用 Kubernetes API 服务器以创建 Backup 对象;
- BackupController 将收到通知有新的 Backup 对象被创建并执行验证;
- BackupController 开始备份过程,它通过查询 API 服务器以获取资源来收集数据以进行备份;
- BackupController 将调用对象存储服务,例如,AWS S3 -上传备份文件。默认情况下,velero backup create支持任何持久卷的磁盘快照,可以通过指定其他标志来调整快照,运行velero backup create --help可以查看可用的标志,也可以使用--snapshot-volumes=false选项禁用快照。
- BackupStorageLocation 主要支持的后端存储是 S3 兼容的存储,存储所有Velero数据的存储区中的前缀以及一组其他特定于提供程序的字段。比如:Minio 和阿里云 OSS 等 ;
- VolumeSnapshotLocation(pv 数据),主要用来给 PV 做快照,需要云提供商提供插件,完全由提供程序提供的特定的字段(例如AWS区域,Azure资源组,Portworx快照类型等)定义。以对数据一致性最为敏感的数据库和中间件为例,开源存储插件 Carina 也即将提供数据库感知的 velero 卷快照功能,可以实现中间件数据的快速备份及恢复。
Velero安装和使用
安装velero客户端
$ wget https://mirror.ghproxy.com/https://github.com/vmware-tanzu/velero/releases/download/v1.6.3/velero-v1.6.3-darwin-amd64.tar.gz
$ tar -zxvf velero-v1.6.3-darwin-amd64.tar.gz && cd velero-v1.6.3-darwin-amd64
$ mv velero /usr/local/bin && chmod +x /usr/local/bin/velero
$ velero version
安装minio作为数据备份后端
apiVersion: v1
kind: Namespace
metadata:
name: velero
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: velero
name: minio
labels:
component: minio
spec:
strategy:
type: Recreate
selector:
matchLabels:
component: minio
template:
metadata:
labels:
component: minio
spec:
volumes:
- name: storage
emptyDir: {}
- name: config
emptyDir: {}
containers:
- name: minio
image: minio/minio:latest
imagePullPolicy: IfNotPresent
args:
- server
- /storage
- --config-dir=/config
- --console-address=:9001
env:
- name: MINIO_ACCESS_KEY
value: "minio"
- name: MINIO_SECRET_KEY
value: "minio123"
ports:
- containerPort: 9000
- containerPort: 9001
volumeMounts:
- name: storage
mountPath: "/storage"
- name: config
mountPath: "/config"
---
apiVersion: v1
kind: Service
metadata:
namespace: velero
name: minio
labels:
component: minio
spec:
type: NodePort
ports:
- name: api
port: 9000
targetPort: 9000
- name: console
port: 9001
targetPort: 9001
selector:
component: minio
---
apiVersion: batch/v1
kind: Job
metadata:
namespace: velero
name: minio-setup
labels:
component: minio
spec:
template:
metadata:
name: minio-setup
spec:
restartPolicy: OnFailure
volumes:
- name: config
emptyDir: {}
containers:
- name: mc
image: minio/mc:latest
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- "mc --config-dir=/config config host add velero http://minio:9000 minio minio123 && mc --config-dir=/config mb -p velero/velero"
volumeMounts:
- name: config
mountPath: "/config"
$ kubectl apply -f ./00-minio-deployment.yaml
$ kubectl get pods -n velero
NAME READY STATUS RESTARTS AGE
minio-58dc5cf789-z2777 0/1 ContainerCreating 0 14s
minio-setup-dz4jb 0/1 ContainerCreating 0 6s
$ kubectl get svc -n velero
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
minio NodePort 10.96.13.35 <none> 9000:30693/TCP,9001:32351/TCP 17s
安装 velero 服务端 ,使用s3 作为存储
- 创建 minio 凭证
$ cat > credentials-velero <<EOF
[default]
aws_access_key_id = minio
aws_secret_access_key = minio123
EOF
# 安装velero
$ cp velero /usr/bin/
# 启用快速补全
$ velero completion bash
- 使用官方提供的 restic 组件备份 pv
$ velero install \
--image velero/velero:v1.6.3 \
--plugins velero/velero-plugin-for-aws:v1.0.0 \
--provider aws \
--bucket velero \
--namespace velero \
--secret-file ./credentials-velero \
--velero-pod-cpu-request 200m \
--velero-pod-mem-request 200Mi \
--velero-pod-cpu-limit 1000m \
--velero-pod-mem-limit 1000Mi \
--use-volume-snapshots=false \
--use-restic \
--restic-pod-cpu-request 200m \
--restic-pod-mem-request 200Mi \
--restic-pod-cpu-limit 1000m \
--restic-pod-mem-limit 1000Mi \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero.svc:9000
--provider:声明使用的 Velero 插件类型。
--plugins:使用 S3 API 兼容插件 “velero-plugin-for-aws ”。
--bucket:在腾讯云 COS 创建的存储桶名。
--secret-file:访问 COS 的访问凭证文件,见上面创建的 “credentials-velero”凭证文件。
--use-restic:使用开源免费备份工具 restic 备份和还原持久卷数据。
--default-volumes-to-restic:使用 restic 来备份所有Pod卷,前提是需要开启 --use-restic 参数。
--backup-location-config:备份存储桶访问相关配置。
--region:兼容 S3 API 的 COS 存储桶地区,例如创建地区是广州的话,region 参数值为“ap-guangzhou”。
--s3ForcePathStyle:使用 S3 文件路径格式。
--s3Url:COS 兼容的 S3 API 访问地址
--use-volume-snapshots=false 来关闭存储卷数据快照备份。
$ velero backup-location get
apiVersion: velero.io/v1
kind: BackupStorageLocation
metadata:
name: default
namespace: velero
spec:
# 只有 aws gcp azure
provider: aws
objectStorage:
bucket: myBucket
prefix: backup
config:
region: us-west-2
profile: "default"
s3ForcePathStyle: "false"
s3Url: http://minio:9000
velero功能介绍
创建备份
$ velero create backup $NAME [flags]
$ velero backup create pvc-backup-1 --snapshot-volumes --include-namespaces nginx-example --default-volumes-to-restic --volume-snapshot-locations default
$ velero backup create backupName --include-cluster-resources=true --ordered-resources 'pods=ns1/pod1,ns1/pod2;persistentvolumes=pv4,pv8' --include-namespaces=ns1
$ velero backup create backupName --ordered-resources 'statefulsets=ns1/sts1,ns1/sts0' --include-namespaces=n
定时备份:
$ velero schedule create <SCHEDULE NAME> --schedule "0 7 * * *"
$ velero create schedule NAME --schedule="@every 6h"
$ velero create schedule NAME --schedule="@every 24h" --include-namespaces web
$ velero create schedule NAME --schedule="@every 168h" --ttl 2160h0m0s
备份高级用法举例
- 在单个Velero备份中创建不止一种持久卷的快照
$ velero snapshot-location create ebs-us-east-1 \
--provider aws \
--config region=us-east-1
$ velero snapshot-location create portworx-cloud \
--provider portworx \
--config type=cloud
$ velero backup create full-cluster-backup \
--volume-snapshot-locations ebs-us-east-1,portworx-cloud
- 在不同的地区将备份存储到不同的对象存储桶中
$ velero backup-location create default \
--provider aws \
--bucket velero-backups \
--config region=us-east-1
$ velero backup-location create s3-alt-region \
--provider aws \
--bucket velero-backups-alt \
--config region=us-west-1
$ velero backup create full-cluster-alternate-location-backup \
--storage-location s3-alt-region
- 对于公有云提供的存储卷,将一部分快照存储在本地,一部分存储在公有云
$ velero snapshot-location create portworx-local \
--provider portworx \
--config type=local
$ velero snapshot-location create portworx-cloud \
--provider portworx \
--config type=cloud
$ velero backup create cloud-snapshot-backup \
--volume-snapshot-locations portworx-cloud
- 使用存储位置
$ velero backup-location create default \
--provider aws \
--bucket velero-backups \
--config region=us-west-1
$ velero snapshot-location create ebs-us-west-1 \
--provider aws \
--config region=us-west-1
$ velero backup create full-cluster-backup
查看备份任务。
$ velero backup get
$ kubectl patch backupstoragelocation default --namespace velero \
--type merge \
--patch '{"spec":{"accessMode":"ReadOnly"}}'
velero backup-location get
NAME PROVIDER BUCKET/PREFIX PHASE LAST VALIDATED ACCESS MODE DEFAULT
default aws velero Unknown Unknown ReadWrite true
恢复备份数据
$ velero restore create --from-backup <backup-name>
$ velero restore create --from-backup pvc-backup-1 --restore-volumes
查看恢复任务。
$ velero restore get
$ kubectl patch backupstoragelocation default --namespace velero \
--type merge \
--patch '{"spec":{"accessMode":"ReadWrite"}}'
备份 hooks 介绍
- Pre hooks
pre.hook.backup.velero.io/container:将要执行命令的容器,默认为pod中的第一个容器,可选的。
pre.hook.backup.velero.io/command:要执行的命令,如果需要多个参数,请将该命令指定为JSON数组。例如:["/usr/bin/uname", "-a"]
pre.hook.backup.velero.io/on-error:如果命令返回非零退出代码如何处理。默认为“Fail”,有效值为“Fail”和“Continue”,可选的。
pre.hook.backup.velero.io/timeout:等待命令执行的时间,如果命令超过超时,则认为该挂钩失败的。默认为30秒,可选的。
- Post hooks
post.hook.backup.velero.io/container:将要执行命令的容器,默认为pod中的第一个容器,可选的。
post.hook.backup.velero.io/command:要执行的命令,如果需要多个参数,请将该命令指定为JSON数组。例如:["/usr/bin/uname", "-a"]
post.hook.backup.velero.io/on-error:如果命令返回非零退出代码如何处理。默认为“Fail”,有效值为“Fail”和“Continue”,可选的。
post.hook.backup.velero.io/timeout:等待命令执行的时间,如果命令超过超时,则认为该挂钩失败的。默认为30秒,可选的
还原 hooks 介绍
- InitContainer Restore Hooks:这些将在待还原的 Pod 的应用程序容器启动之前将 init 容器添加到还原的 pod 中,以执行任何必要的设置。
init.hook.restore.velero.io/container-image:要添加的init容器的容器镜像
init.hook.restore.velero.io/container-name:要添加的init容器的名称
init.hook.restore.velero.io/command:将要在初始化容器中执行的任务或命令
kubectl annotate pod -n <POD_NAMESPACE> <POD_NAME> \
init.hook.restore.velero.io/container-name=restore-hook \
init.hook.restore.velero.io/container-image=alpine:latest \
init.hook.restore.velero.io/command='["/bin/ash", "-c", "date"]'
- Exec Restore Hooks:可用于在已还原的Kubernetes pod的容器中执行自定义命令或脚本。
post.hook.restore.velero.io/container:;执行hook的容器名称,默认为第一个容器,可选
post.hook.restore.velero.io/command:将在容器中执行的命令,必填
post.hook.restore.velero.io/on-error:如何处理执行失败,有效值为Fail和Continue,默认为Continue,使用Continue模式,仅记录执行失败;使用Fail模式时,将不会在自行其他的hook,还原的状态将为PartiallyFailed,可选
post.hook.restore.velero.io/exec-timeout:开始执行后要等待多长时间,默认为30秒,可选
post.hook.restore.velero.io/wait-timeout:等待容器准备就绪的时间,该时间应足够长,以使容器能够启动,并
kubectl annotate pod -n <POD_NAMESPACE> <POD_NAME> \
post.hook.restore.velero.io/container=postgres \
post.hook.restore.velero.io/command='["/bin/bash", "-c", "psql < /backup/backup.sql"]' \
post.hook.restore.velero.io/wait-timeout=5m \
post.hook.restore.velero.io/exec-timeout=45s \
post.hook.restore.velero.io/on-error=Continue
Velero部分关键问题解析
Velero可以将资源还原到与其备份来源不同的命名空间中吗?
velero restore create RESTORE_NAME \
--from-backup BACKUP_NAME \
--namespace-mappings old-ns-1:new-ns-1,old-ns-2:new-ns-2
执行还原操作后,已有的 NodePort 类型的 service 如何处理?
velero怎么实现不影响业务前提下得一致性备份策略,并将备份数据上传到对象存储中?
边栏推荐
- kotlin 异步流
- 华为 PC 逆势增长,产品力决定一切
- Eight major trends in the industrial Internet of things (iiot)
- 发扬连续作战优良作风 全力以赴确保北江大堤安全
- 《中国数据库安全能力市场洞察,2022》报告研究正式启动
- Comparator 排序函数式接口
- How to avoid serious network security accidents?
- Explain the difference between iaas/paas/saas by cooking rice
- Kotlin inheritance, class, overload
- 源码解析 Handler 面试宝典
猜你喜欢

Activity lifecycle

开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低

首席信息安全官仍然会犯的漏洞管理错误

Opengauss kernel: simple query execution

Without home assistant, zhiting can also open source access homekit and green rice devices?

Preparation and operation & Maintenance Guide for 'high concurrency & high performance & high availability service program'

黄金年代入场券之《Web3.0安全手册》

These default routes and static routes can not be configured and deployed. What kind of network workers are they!

每日一题day8-515. 在每个树行中找最大值

‘高并发&高性能&高可用服务程序’编写及运维指南
随机推荐
Goldfish rhca memoirs: do447 manage lists and credentials -- create machine credentials for the access list host
华为 PC 逆势增长,产品力决定一切
【sdx62】WCN685X IPA不生效问题分析及解决方案
数据科学家面临的七大挑战及解决方法
ERR AUTH&lt; password&gt; called without anypassword configured for the default user. Ar
服务可见可观测性
kotlin 异步流
How to avoid serious network security accidents?
Hands on data analysis unit 3 model building and evaluation
CVPR 2022 | 美团技术团队精选论文解读
Yyds dry goods counting solution sword finger offer: adjust the array order so that odd numbers precede even numbers (2)
2022年煤炭生产经营单位(安全生产管理人员)考试题模拟考试平台操作
#云原生征文#Ingress案例实战
**Unity中莫名其妙得小问题-灯光和天空盒
Kotlin composite suspend function
一个团队可以既做项目又做产品吗?
Detailed explanation of kotlin collaboration lanch
Sinomeni vine was selected as the "typical solution for digital technology integration and innovative application in 2021" of the network security center of the Ministry of industry and information te
黄楚平主持召开定点联系珠海工作视频会议 坚决落实省委部署要求 确保防疫情、稳经济、保安全取得积极成效
真正的项目经理强者,都是闭环高手!