hello云胜

技术与生活

0%

Velero迁移

简介:

Velero 是一种云原生的 Kubernetes 容灾解决方案,支持标准的K8S集群,既可以是私有云平台也可以是公有云。除了灾备之外它还能做资源移转,支持把容器应用从一个集群迁移到另一个集群。它是一个开源的安全备份和恢复工具,执行灾难恢复,迁移 Kubernetes 集群资源和持久的卷。
该方案功能强大,但是操作稍微复杂些,需要安装相应的客户端和服务端。

Velero工作流程:

  1. 流程图

    img

    image.png

  2. 备份过程

    本地 Velero 客户端发送备份指令。

    Kubernetes 集群内就会创建一个 Backup 对象。

    BackupController 监测 Backup 对象并开始备份过程。

    BackupController 会向 API Server 查询相关数据。

    BackupController 将查询到的数据备份到远端的对象存储。

    从以上过程中能看出,使用velero备份k8s集群,需要一个store来存储备份数据,这就需要用到velero-plugin,

    velero-plug中有aws以及阿里云的插件,但是没有华为云直接使用的,经过与华为云沟通,说obs也是s3协议的,可以直接用aws插件。

安装

1. 客户端安装

我们现在用的是1.7.0

1
2
3
4
5
6
https://github.com/vmware-tanzu/velero/releases/download/v1.6.3/velero-v1.6.3-linux-amd64.tar.gz

wget https://github.com/vmware-tanzu/velero/releases/download/v1.7.0/velero-v1.7.0-linux-amd64.tar.gz

内网
http://transfer.paas.xxx.net/1ncSK6Z/velero-v1.7.0-linux-amd64.tar.gz

官方:Velero Docs - Basic Install

1
2
3
4
tar -zxvf velero-v1.7.0-linux-amd64.tar.gz
cd velero-v1.7.0-linux-amd64
chmod +x velero
mv velero /usr/bin

2. 服务端安装

直接安装

创建华为云credentials文件
1
2
3
4
5
# vim /root/velero/velero-credentials

[default]
aws_access_key_id = UQSIGADUGNF9MHUGYWVP
aws_secret_access_key = 26kysnDpVX4b02bhxbpOpgof1B31MtGY6YFE9c6n
1
2
3
4
5
cat>/root/velero/velero-credentials<<EOF
[default]
aws_access_key_id = UQSIGADUGNF9MHUGYWVP
aws_secret_access_key = 26kysnDpVX4b02bhxbpOpgof1B31MtGY6YFE9c6n
EOF
安装velero服务

1.6.3版本

1
2
3
4
5
6
7
8
9
velero install \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.2.1 \
--bucket obs-zjjpt \
--prefix middleware \
--secret-file /root/velero/velero-credentials \
--use-restic \
--use-volume-snapshots=false \
--backup-location-config region=cn-east-213,s3ForcePathStyle="true",s3Url=http://1x.xxx.0.3 --image=velero/velero:v1.6.3

1.7.0版本

1
2
3
4
5
6
7
8
9
velero install \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.3.0 \
--bucket obs-zjjpt \
--prefix prod-huawei \
--secret-file /root/velero/velero-credentials \
--use-restic \
--use-volume-snapshots=false \
--backup-location-config region=cn-east-213,s3ForcePathStyle="true",s3Url=http://1x.xxx.0.3 --image=velero/velero:v1.7.0
  • –use-volume-snapshots=true: 是否使用kubernetes的卷快照功能
  • –use-restic: 使用开源免费备份工具 restic 备份和还原持久卷数据

使用私服镜像,【使用这个】

obs-zjjpt这个bucket是华为云分给我们的,不能换

1
2
3
4
5
6
7
8
9
10
11
12
13
velero install \
--provider aws \
--plugins harbor-test.xxx.net/velero/velero-plugin-for-aws:v1.3.0 \
--bucket obs-zjjpt \
--prefix dubbo-test \
--secret-file /root/velero/velero-credentials \
--use-restic \
--restic-pod-mem-limit=2048Mi \
--restic-pod-mem-request=2048Mi \
--velero-pod-mem-limit=2048Mi \
--velero-pod-mem-request=2048Mi \
--use-volume-snapshots=false \
--backup-location-config region=cn-east-213,s3ForcePathStyle="true",s3Url=http://1x.xxx.0.3 --image=harbor-test.xxx.net/velero/velero:v1.7.0

在kubesphere平台上安装

【有报错,可能和华为obs的配置有关系,没有解决,不用这种方式】

我目前使用的是kubesphere平台。直接使用helm安装也很简单。

添加velero仓库

https://vmware-tanzu.github.io/helm-charts/

20210913135757381

创建velero应用

使用应用模板部署velero

image-20210913164721573

参数配置

values.yaml

1
2
3
4
5
6
7
initContainers:
- name: velero-plugin-for-aws
image: velero/velero-plugin-for-aws:v1.3.0 //国外的镜像可能下载不下来
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /target
name: plugins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
configuration:
# 华为云沟通,说obs也是s3协议的,可以直接用aws插件。
# 阿里云的话写:alibabacloud
provider: aws
backupStorageLocation:
name:
provider:
# bucket名字,必填
bucket: obs-zjjpt
caCert:
# 我们创建的目录名
prefix: pcloud-test
default:
config:
region: cn-east-213
1
2
3
4
5
6
7
8
9
credentials:
useSecret: true
name:
existingSecret:
secretContents:
cloud: |
# 如果是阿里云写 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET
aws_access_key_id=UQSIGADUGNF9MHUGYWVP
aws_secret_access_key=26kysnDpVX4b02bhxbpOpgof1B31MtGY6YFE9c6n

备份

默认保存30天

一次性备份

1
velero backup create test-backup --include-namespaces test

备份名:nginx-backup

指定备份的namespace是test

1
2
# 备份全部
velero backup create backup-all

定时任务

1
2
# 创建一个定时任务
velero create schedule backup-all --schedule="0 17 * * *"

定时任务就是全量备份的

写17是因为时区问题。17 + 8 = 在1点备份

备份持久化卷

部署一个wordpress

1
2
3
4
5
6
7
# helm install my-wordpress bitnami/wordpress -n wordpress
...
3. Login with the following credentials below to see your blog:

echo Username: user
echo Password: $(kubectl get secret --namespace wordpress my-wordpress -o jsonpath="{.data.wordpress-password}" | base64 --decode)

备份

1
velero backup create wordpress-backup --include-namespaces wordpress --default-volumes-to-restic -n velero

恢复后数据还在

image-20211216162811468

查看

查看备份

1
velero get backups

查看定时任务

1
velero get schedules

查看恢复

1
velero get restore

查看备份地址

1
velero get backup-locations

恢复

从backup恢复

最基本的操作

1
velero restore create RESTORE_NAME --from-backup BACKUP_NAME 

从schedule恢复

1
velero restore create restore-sch --from-schedule backup-all --preserve-nodeports  --include-namespaces testvelero -n velero2

参数

​ –preserve-nodeports optionalBool[=true] 固定端口

​ –include-namespaces stringArray 选择性的从全部backup里恢复哪些ns

也可以恢复到其他ns,使用–namespace-mappings进行映射

1
2
3
velero restore create RESTORE_NAME \
--from-backup BACKUP_NAME \
--namespace-mappings old-ns-1:new-ns-1,old-ns-2:new-ns-2

要在另一个集群B恢复集群A的backup,那就再建一个集群A相同的velero,只是指定一个新的namespace。(默认的ns是velero)

1
2
3
4
5
6
7
8
9
10
velero install \
--namespace velero2 \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.3.0 \
--bucket obs-zjjpt \
--prefix dubbo-test \
--secret-file /root/velero/velero-credentials \
--use-restic \
--use-volume-snapshots=false \
--backup-location-config region=cn-east-213,s3ForcePathStyle="true",s3Url=http://1x.xxx.0.3 --image=velero/velero:v1.7.0

以后再restor的时候,加上-n velero2

1
velero restore create RESTORE_NAME --from-backup BACKUP_NAME -n velero2

考虑过是不是增加一个backup-location就可以。

1
2
3
4
5
6
7
8
9
10
# 新增一个backup-location
velero create backup-location dubbo-test --provider aws --bucket obs-zjjpt --prefix dubbo-test --config region=cn-east-213,s3ForcePathStyle="true",s3Url=http://1x.xxx.0.3 --default
# 将其设置为default
# velero backup-location set dubbo-test --default
# 查看一下
# velero get backup-location
NAME PROVIDER BUCKET/PREFIX PHASE LAST VALIDATED ACCESS MODE DEFAULT
default aws obs-zjjpt/middleware Available 2021-12-15 15:47:24 +0800 CST ReadWrite
dubbo-test aws obs-zjjpt/dubbo-test Unknown Unknown ReadWrite true

尝试了下,不行。问题是找不到密钥。没看找出 传递密码进去的写法。

官方说以后或再resotre命令中增加指定backup-location的参数。

报错

这个报错是用helm安装报错

kubectl -n velero logs deployment/velero

1
2
time="2021-12-14T06:32:45Z" level=error msg="No backup storage locations found, at least one is required" controller=backup-storage-location error="no backup storage locations found" error.file="/go/src/github.com/vmware-tanzu/velero/internal/storage/storagelocation.go:93" error.function=github.com/vmware-tanzu/velero/internal/storage.ListBackupStorageLocations logSource="pkg/controller/backup_storage_location_controller.go:62"
time="2021-12-14T06:32:49Z" level=error msg="Error getting backup store for this location" backupLocation=default controller=backup-sync error="rpc error: code = Unknown desc = NoCredentialProviders: no valid providers in chain. Deprecated.\n\tFor verbose messaging see aws.Config.CredentialsChainVerboseErrors" error.file="/go/src/velero-plugin-for-aws/velero-plugin-for-aws/volume_snapshotter.go:59" error.function=main.getSession logSource="pkg/controller/backup_sync_controller.go:175"

换用Credential文件的形式安装

2, 存储的文件夹

time=”2021-12-14T09:04:30Z” level=info msg=”Backup storage location is invalid, marking as unavailable” backup-storage-location=default controller=backup-storage-location logSource=”pkg/controller/backup_storage_location_controller.go:117”
time=”2021-12-14T09:04:30Z” level=error msg=”Current backup storage locations available/unavailable/unknown: 0/1/0, Backup storage location "default" is unavailable: Backup store contains invalid top-level directories: [middleware pcloud-test])” controller=backup-storage-location logSource=”pkg/controller/backup_storage_location_controller.go:164”

image-20211214171046011

说我提前建好的文件夹不合法

1
velero backup create test-backup --include-namespaces test

image-20211214171249223

执行备份是可以成功的,会自动生成一个叫backups的文件夹

然后我用1.7.0的版本测试,发现prefix参数是可以的

image-20211214174817515

会在middleware下产生备份的文件夹

dmz-sit

time=”2021-12-17T06:52:06Z” level=error msg=”Error listing backups in backup store” backupLocation=default controller=backup-sync error=”rpc error: code = Unknown desc = RequestError: send request failed\ncaused by: Get http://1x.xxx.0.3/obs-zjjpt?delimiter=%2F&list-type=2&prefix=dmz-sit%2Fbackups%2F: dial tcp 1x.xxx.0.3:80: i/o timeout” error.file=”/go/src/velero-plugin-for-aws/velero-plugin-for-aws/object_store.go:361” error.function=”main.(*ObjectStore).ListCommonPrefixes” logSource=”pkg/controller/backup_sync_controller.go:182”

备份一直卡住InProgress

describe的信息

1
2
3
4
5
6
7
State:          Running
Started: Thu, 18 Aug 2022 09:00:29 +0800
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Started: Wed, 17 Aug 2022 09:01:01 +0800
Finished: Thu, 18 Aug 2022 09:00:28 +0800