k8s备份迁移工具velero

参考链接:
https://zhuanlan.zhihu.com/p/678750298
https://yunxue521.top/archives/velero
https://www.cnblogs.com/wubolive/p/17345716.html
https://velero.io/docs/v1.8/

一、简介

Velero(以前称为 Heptio Ark)提供了备份和还原 Kubernetes 集群资源和持久卷的工具。您可以使用云提供商或本地运行 Velero。

Velero 可让您:

  • 对集群进行备份,并在丢失时进行还原。
  • 将集群资源迁移到其他集群。
  • 将生产集群复制到开发和测试集群。

Velero包括:

  • 在群集上运行的服务器
  • 本地运行的命令行客户端

备份工作流程

  1. Velero 客户端调用 Kubernetes API 服务器来创建对象Backup。
  2. 注意到BackupController新Backup对象并执行验证。
  3. BackupController备份过程开始。它通过向 API 服务器查询资源来收集要备份的数据。
  4. 调用BackupController对象存储服务(例如 AWS S3)来上传备份文件
    在这里插入图片描述

二、安装

注意:需要提前准备好一个对象存储,这里使用的是minio

1、minio创建存储桶

输入存储桶名称velerodata
在这里插入图片描述
创建完成
在这里插入图片描述

2、部署velero

通过二进制文件安装

  • 下载二进制文件,并解压到/usr/local/bin下
## 本次选择了1.11.1版本https://github.com/vmware-tanzu/velero/releases
 ## 下载
 wget https://github.com/vmware-tanzu/velero/releases/download/v1.11.1/velero-v1.11.1-linux-amd64.tar.gz 
 ## 解压
 tar -zxvf velero-v1.11.1-linux-amd64.tar.gz -C /usr/local/bin && mv /usr/local/bin/velero-v1.11.1-linux-amd64/velero /usr/local/bin
  • 生成minio认证文件
cat >velero-auth.txt << EOF
[default]
aws_access_key_id = admin
aws_secret_access_key = admin123
EOF
  • 创建namespace
kubectl create ns velero
安装 (需要提前创建bucket)
velero install  \
--use-node-agent  --provider aws   \
--plugins velero/velero-plugin-for-aws:v1.7.0  \
--bucket velerodata \
--secret-file ./velero-auth.txt \
--use-volume-snapshots=false  \
--namespace velero \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.64.11:9000

http://192.168.64.11:9000为对象存储的地址

  • 查看安装状态
kubectl get pod -n velero 

NAME                     READY   STATUS    RESTARTS   AGE
node-agent-9lxqm         1/1     Running   0          4m46s
node-agent-9qc2x         1/1     Running   0          4m46s
node-agent-d2w6c         1/1     Running   0          4m46s
velero-bd8c98fd6-h59jh   1/1     Running   0          4m46s


kubectl get crd | grep velero

backuprepositories.velero.io                          2024-05-21T06:09:21Z
backups.velero.io                                     2024-05-21T06:09:21Z
backupstoragelocations.velero.io                      2024-05-21T06:09:21Z
deletebackuprequests.velero.io                        2024-05-21T06:09:21Z
downloadrequests.velero.io                            2024-05-21T06:09:21Z
podvolumebackups.velero.io                            2024-05-21T06:09:21Z
podvolumerestores.velero.io                           2024-05-21T06:09:21Z
restores.velero.io                                    2024-05-21T06:09:21Z
schedules.velero.io                                   2024-05-21T06:09:21Z
serverstatusrequests.velero.io                        2024-05-21T06:09:21Z
volumesnapshotlocations.velero.io                     2024-05-21T06:09:21Z
  • 检查备份数据存储状态
kubectl get backupstoragelocations -A 
NAMESPACE   NAME      PHASE       LAST VALIDATED   AGE     DEFAULT
velero      default   Available   5s               6m42s   true

三、使用velero进行资源备份

1 、部署测试应用

部署测试应用mysql,用来测试备份与恢复结果,使用minio进行备份数据的存储

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: managed-nfs-storage 
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1                 #版本
kind: Deployment                    #创建资源的类型
metadata:                           #资源的元数据
  name: mysql-dep                   #资源的名称,是元数据必填项
spec:                               #期望状态
  replicas: 1                       #创建的副本数量(pod数量),不填默认为1
  selector:                         #
    matchLabels:
      app: mysql-pod
  template:                         #定义pod的模板
    metadata:                       #pod的元数据
      labels:                       #labels标签,必填一个
        app: mysql-pod             
    spec:                           #pod的期望状态
      containers:                   #容器
      - name: mysql                 #容器名称
        image: mysql:5.7            #镜像
        imagePullPolicy: IfNotPresent
        ports:                      #容器的端口
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
---
apiVersion: v1               #版本
kind: Service                #创建资源的类型
metadata:                    #资源的元数据
  name: mysql-svc            #资源的名称,是元数据必填项
  labels:                    #labels标签  
    app: mysql-svc
spec:                        #期望状态
  type: NodePort             #服务类型
  ports:                     #端口
    - port: 3306
      targetPort: 3306       #与containerPort一样
      protocol: TCP
      nodePort: 31336
  selector:
    app: mysql-pod
# 创建命名空间
kubectl create ns mysql-velero-test  
# 部署mysql
kubectl apply -f mysqlvelero.yaml -n mysql-velero-test
# 查看mysql状态
kubectl get all -n mysql-velero-test

2、向测试应用写一点数据

kubectl exec -it -n mysql-velero-test mysql-dep-5459dfbd48-mghgm /bin/bash
mysql -uroot -proot

-- 创建测试数据库
CREATE DATABASE testdb;

-- 使用测试数据库
USE testdb;

-- 创建测试表
CREATE TABLE test_table (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  age INT,
  email VARCHAR(100)
);

-- 插入测试数据
INSERT INTO test_table (name, age, email) VALUES
  ('John Doe', 25, 'john.doe@example.com'),
  ('Jane Smith', 30, 'jane.smith@example.com'),
  ('Mike Johnson', 35, 'mike.johnson@example.com');
  

select * from test_table;
+----+--------------+------+--------------------------+
| id | name         | age  | email                    |
+----+--------------+------+--------------------------+
|  1 | John Doe     |   25 | john.doe@example.com     |
|  2 | Jane Smith   |   30 | jane.smith@example.com   |
|  3 | Mike Johnson |   35 | mike.johnson@example.com |
+----+--------------+------+--------------------------+ 

3、备份数据库

velero backup create test-mysql-backup1 --include-namespaces=mysql-velero-test
 --namespace velero --default-volumes-to-fs-backup
velero backup create mysql-backup-demo1 --include-namespaces=mysql-velero-test
 --default-volumes-to-fs-backup


备份命令:velero create backup NAME [flags]
backup选项:

--default-volumes-to-restic 参数指示使用 restic 备份持久卷
--default-volumes-to-fs-backup:默认将所有PV卷进行备份,详情查看官方文档。
--include-namespaces:指定要备份的命名空间
--exclude-namespaces stringArray : 要从备份中排除的名称空间
--exclude-resources stringArray: 要从备份中排除的资源,如storageclasses.storage.k8s.io
--include-cluster-resources optionalBool[=true]: 包含集群资源类型
--include-namespaces stringArray: 要包含在备份中的名称空间(默认'*')
--include-resources stringArray: 备份中要包括的资源
--labels mapStringString: 给这个备份加上标签
-o, --output string: 指定输出格式,支持'table''json''yaml'-l, --selector labelSelector: 对指定标签的资源进行备份
--snapshot-volumes optionalBool[=true]: 对 PV 创建快照
--storage-location string: 指定备份的位置
--ttl duration: 备份数据多久删掉
--volume-snapshot-locations strings: 指定快照的位置,也就是哪一个公有云驱动
  • 查看备份结果
velero backup get

打开minio可以看到备份数据
在这里插入图片描述

4、删除数据库并恢复

## 删除数据库 
kubectl delete -f mysqlvelero.yaml -n mysql-velero-test
kubectl delete namespace mysql-velero-test
kubectl get all -n mysql-velero-test
## 获取备份信息
kubectl get backup -n velero

NAME                AGE
test-mysql-backup   9m10s

## 恢复数据库
velero restore create --from-backup test-mysql-backup

5、velero操作命令

5.1 创建备份任务

  • 手动发起备份任务
velero backup create nginx-example --include-namespaces nginx-example
  • 指定备份排除某些命名空间下面的资源
velero backup create ${BACKUP_NAME} --exclude-namespaces ${NAMESPACE1},${NAMESPACE2}

–include-resources选项可以指定备份哪些资源类型;
–exclude-resources可以指定排除某些资源类型

velero backup create ${BACKUP_NAME} --include-resources pod,secret

定时备份

Usage:
  velero schedule create NAME --schedule [flags]

Examples:
  # 每日1点进行备份
  velero create schedule --schedule="0 1 * * *"

  # 每六小时备份一次.所有namespace
  velero create schedule NAME --schedule="@every 6h"

  # 每24小时备份一次 web namespace的数据
  velero create schedule NAME --schedule="@every 24h" --include-namespaces web

  # 7天备份一次所有名称空间数据,备份保存90天
  velero create schedule NAME --schedule="@every 168h" --ttl 2160h0m0s

5.2 查看备份资源

velero get backup #备份查看
velero get schedule #查看定时备份
velero get restore #查看已有的恢复
velero get plugins #查看插件

批量删除定时备份

velero backup get --selector velero.io/schedule-name=mysql-schedule-demo1 |
awk 'NR>1{print $1}' |
xargs -I % velero backup delete % --confirm

5.3恢复

velero restore create --from-backup $(BACKUP_NAME)
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

5.4 卸载
卸载velero

kubectl delete namespace/velero clusterrolebinding/velero
kubectl delete crds -l component=velero

四、注意事项

  • 在velero备份的时候,备份过程中创建的对象是不会被备份的。

  • 可以将velero作为一个cronjob来运行,定期备份数据。

  • velero restore 恢复不会覆盖已有的资源,只恢复当前集群中不存在的资源。已有的资源不会回滚到之前的版本,如需要回滚,需在restore之前提前删除现有的资源。

  • restore: 对历史备份的 Kubernetes 资源对象和持久卷进行还原,且允许按需选择指定部分资源对象还原到指定命名空间(Namespace)中。且可以选择在备份还原期间或还原后执行 restore hook 操作(比如:执行自定义数据库的还原操作之后,再执行数据库应用容器启动动作)。

  • Tips: 默认情况下,Velero 进行的是非破坏性还原操作(non-destructive restore),这意味着它不会删除目标集群上的任何数据——即如果备份中的资源对象已经存在于目标集群中,restore 操作将会跳过该资源的还原。当然,也可通配置更新策略 (–existing-resource-policy=update),尝试更新目标集群中已存在资源,以匹配备份中的资源数据。

  • 备份使用volumes 的Pod,需要给Pod加上注解

  • 备份时禁用快照,可指定参数–snapshot-volumes=false

  • 各云厂商Volumes快照插件: https://velero.io/plugins/

使用 Velero 跨集群迁移资源,确保如下检查工作

  • 确保镜像资源在迁移后可以正常拉取。
  • 确保两个集群的 K8S 版本的 API 兼容,最好是相同版本
  • 绑定集群外部资源的无法迁移, 例如 LoadBalancer 类型的service, 创建备份建议忽略
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐