本文详细介绍如何使用 Velero 开源工具实现从 Amazon EKS 到 Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE) 的安全迁移,确保应用和数据的完整性。
简介
Velero 是一种开源工具,可用于安全地备份和恢复、执行灾难恢复以及迁移 Kubernetes 集群资源和持久性卷。由于 Oracle Cloud Infrastructure (OCI) 对象存储为 S3-Compatible,因此我们可以使用 Velero 的 AWS 插件。
在本教程中,我们将使用 Azure Kubernetes 服务 (AKS) 作为示例,但这些步骤也应在 Amazon Elastic Kubernetes 服务 (Amazon EKS) 中无缝运行。
架构概览
| 组件 | 源环境 | 目标环境 |
|---|---|---|
| Kubernetes 服务 | Amazon EKS / Azure AKS | Oracle Cloud OKE |
| 备份存储 | OCI 对象存储 (S3-Compatible) | 相同存储桶 |
| 迁移工具 | Velero + AWS Plugin | 相同配置 |
| 持久卷 | EBS / Azure Disk | OCI 块存储 |
工作流程图
先决条件检查
在开始迁移之前,请确保满足以下条件:
环境要求
| 要求项 | 描述 | 检查方式 |
|---|---|---|
| Kubernetes 知识 | 中级以上 K8s 操作经验 | - |
| OCI 对象存储桶 | 用作备份存储的桶 | oci os bucket list |
| 访问权限 | 对象存储读/写权限 | IAM 策略验证 |
| Velero 二进制 | 客户端工具 | velero version |
| 网络连接 | 集群间网络互通 | ping 测试 |
示例环境状态
在本教程中,我们准备了一个连接了持久卷声明的 NGINX Pod 以及一个定制 index.html 文件和一个虚拟密钥:
# 检查 Pod 状态
> kubectl get pods -n nginx
NAME READY STATUS RESTARTS AGE
task-pv-pod 1/1 Running 0 47h
# 验证应用内容
> kubectl exec -it task-pv-pod -n nginx -- curl localhost
AKS to OKE Migration with PVC attached
# 检查密钥
> kubectl get secrets --namespace nginx
NAME TYPE DATA AGE
okemigration Opaque 1 2s
任务 1:创建 Velero 访问凭据
1.1 生成客户密钥
如果您还没有客户密钥,可以按照以下步骤操作:
- 登录 OCI 控制台
- 导航至 身份 > 用户 > 客户密钥
- 点击 生成密钥 并保存生成的密钥对
1.2 创建凭据文件
在用于访问 AKS/EKS 和 OKE 群集的计算机上,创建文件 velero-credentials:
# 创建凭据文件
cat > velero-credentials << 'EOF'
[default]
aws_access_key_id=<<Access Key ID>>
aws_secret_access_key=<<Generated Key>>
EOF
# 设置适当的文件权限
chmod 600 velero-credentials
1.3 验证凭据
# 测试对象存储访问
aws --profile default \
--endpoint-url https://<<tenancy>>.compat.objectstorage.<<region>>.oraclecloud.com \
s3 ls s3://<<bucket-name>>/
任务 2:安装和配置 Velero
2.1 下载 Velero
选择以下安装方式之一:
方式 A:二进制安装
# 下载最新版本
curl -fsSL -o velero-v1.11.1-linux-amd64.tar.gz \
https://github.com/vmware-tanzu/velero/releases/download/v1.11.1/velero-v1.11.1-linux-amd64.tar.gz
# 解压并安装
tar -xvf velero-v1.11.1-linux-amd64.tar.gz
sudo mv velero-v1.11.1-linux-amd64/velero /usr/local/bin/
方式 B:包管理器安装
# macOS
brew install velero
# Linux (snap)
snap install velero --classic
2.2 在源集群安装 Velero
在 EKS/AKS 集群中运行以下命令:
velero install \
--provider aws \
--bucket <<Bucket name>> \
--prefix <<Tenancy name>> \
--use-volume-snapshots=false \
--plugins velero/velero-plugin-for-aws:v1.6.0 \
--secret-file ./velero-credentials \
--backup-location-config region=<<Region>>,s3ForcePathStyle="true",s3Url=https://<<tenancy name>>.compat.objectstorage.<<region>>.oraclecloud.com \
--use-node-agent
2.3 在目标集群安装 Velero
在 OKE 集群中运行相同的安装命令。
2.4 验证安装
# 检查 Velero 部署状态
> kubectl get deployment -n velero
NAME READY UP-TO-DATE AVAILABLE AGE
velero 1/1 1 1 47h
# 检查 Velero Pod
> kubectl get pods -n velero
NAME READY STATUS RESTARTS AGE
velero-7d4c4b5b5b-xyz12 1/1 Running 0 47h
node-agent-abcde 1/1 Running 0 47h
任务 3:执行集群备份
3.1 创建备份
在 源集群(EKS/AKS)中执行备份:
# 创建命名空间备份
velero backup create nginxoke \
--include-namespaces nginx \
--default-volumes-to-fs-backup
# 输出确认
Backup request "nginxoke" submitted successfully.
Run `velero backup describe nginxoke` or `velero backup logs nginxoke` for more details.
3.2 监控备份进度
# 检查备份状态
velero backup get
# 查看详细信息
velero backup describe nginxoke
# 检查备份日志
velero backup logs nginxoke
3.3 验证备份完成
成功的备份应显示以下状态:
> velero backup describe nginxoke
Name: nginxoke
Namespace: velero
Labels: velero.io/storage-location=default
Annotations: velero.io/source-cluster-k8s-gitversion=v1.24.10
velero.io/source-cluster-k8s-major-version=1
velero.io/source-cluster-k8s-minor-version=24
Phase: Completed # 关键状态
Namespaces:
Included: nginx
Excluded: <none>
Started: 2023-07-22 14:40:33 +0300 EEST
Completed: 2023-07-22 14:42:15 +0300 EEST
Backup Format Version: 1.1.0
Total items to be backed up: 12
Items backed up: 12
3.4 验证对象存储
登录 OCI 控制台 > 对象存储 > 您的存储桶,确认备份文件已上传。
任务 4:配置存储类映射
由于 EKS 和 OKE 对持久性卷声明使用不同的 storageClassName,我们需要创建一个 ConfigMap 进行存储类转换。
4.1 创建存储类映射配置
创建文件 configMap.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
# ConfigMap 名称(可自定义)
name: change-storage-class-config
# 必须在 velero 命名空间
namespace: velero
# 必需的标签用于 Velero 识别
labels:
# 标识这是插件配置 ConfigMap
velero.io/plugin-config: ""
# 指定插件名称和类型
velero.io/change-storage-class: RestoreItemAction
data:
# 存储类映射:旧存储类 -> 新存储类
# EKS 默认存储类
gp2: oci-bv
# AKS 默认存储类
default: oci-bv
managed-premium: oci-bv
# 根据实际情况调整
<your-old-storage-class>: oci-bv
4.2 应用配置到目标集群
在 OKE 集群中应用 ConfigMap:
# 应用存储类映射
kubectl apply -f configMap.yaml
# 验证 ConfigMap 创建
kubectl get configmap -n velero change-storage-class-config -o yaml
4.3 验证 OKE 存储类
确认 OKE 集群中可用的存储类:
# 列出可用存储类
kubectl get storageclass
# 输出示例
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE AGE
oci-bv (default) blockvolume.csi.oraclecloud.com Delete WaitForFirstConsumer 30d
oci-bv-encrypted blockvolume.csi.oraclecloud.com Delete WaitForFirstConsumer 30d
任务 5:执行恢复和迁移
5.1 验证备份可访问性
在 OKE 集群中确认可以访问之前创建的备份:
# 列出可用备份
$ velero backup get
NAME STATUS ERRORS WARNINGS CREATED EXPIRES STORAGE LOCATION SELECTOR
nginxoke Completed 0 0 2023-07-22 14:40:33 +0300 EEST 29d default <none>
5.2 创建恢复
从备份创建恢复任务:
# 创建恢复
$ velero restore create --from-backup nginxoke
# 输出确认
Restore request "nginxoke-20230722145553" submitted successfully.
Run `velero restore describe nginxoke-20230722145553` or `velero restore logs nginxoke-20230722145553` for more details.
5.3 监控恢复进度
# 检查恢复状态
velero restore get
# 查看详细恢复信息
$ velero restore describe nginxoke-20230722145553
Name: nginxoke-20230722145553
Namespace: velero
Labels: <none>
Annotations: <none>
Phase: Completed # 成功状态
Total items to be restored: 7
Items restored: 7
Started: 2023-07-22 14:55:54 +0300 EEST
Completed: 2023-07-22 14:56:45 +0300 EEST
Backup: nginxoke
Namespaces:
Included: nginx
Excluded: <none>
Resources:
Included: *
Excluded: <none>
Cluster-scoped: auto
任务 6:验证迁移结果
6.1 检查 Pod 状态
验证应用 Pod 是否成功恢复:
# 检查 Pod 运行状态
$ kubectl get pod -n nginx
NAME READY STATUS RESTARTS AGE
task-pv-pod 1/1 Running 0 2m15s
6.2 验证密钥迁移
确认 Kubernetes Secrets 已正确迁移:
# 检查密钥
$ kubectl get secrets -n nginx
NAME TYPE DATA AGE
okemigration Opaque 1 2m21s
6.3 验证持久卷声明
检查 PVC 是否使用正确的存储类:
# 检查 PVC 状态
$ kubectl get pvc -n nginx
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
task-pv-claim Bound csi-d03267f7-32cf-4068-8809-d29aa649dcba 50Gi RWO oci-bv 2m31s
6.4 验证应用功能
测试应用是否正常工作:
# 测试应用响应
$ kubectl exec task-pv-pod -n nginx -- curl http://localhost
AKS to OKE Migration with PVC attached
# 验证持久卷数据完整性
$ kubectl exec task-pv-pod -n nginx -- ls -la /usr/share/nginx/html/
total 12
drwxr-xr-x 2 root root 4096 Jul 20 10:30 .
drwxr-xr-x 3 root root 4096 Jul 18 12:15 ..
-rw-r--r-- 1 root root 34 Jul 20 10:30 index.html
故障排查指南
常见问题及解决方案
问题 1:备份卡在 InProgress 状态
现象:
velero backup get
NAME STATUS ERRORS WARNINGS CREATED EXPIRES STORAGE LOCATION
backup1 InProgress 0 0 5m 29d default
解决方案:
# 检查 velero pod 日志
kubectl logs -n velero deployment/velero
# 检查 node-agent 状态
kubectl get pods -n velero -l name=node-agent
# 重启 velero 组件
kubectl rollout restart deployment/velero -n velero
问题 2:恢复时 PVC 保持 Pending 状态
现象:
kubectl get pvc -n nginx
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Pending 0 gp2 5m
解决方案:
# 1. 确认存储类映射 ConfigMap 已正确创建
kubectl get configmap -n velero change-storage-class-config
# 2. 检查目标集群存储类
kubectl get storageclass
# 3. 手动编辑 PVC 修改存储类
kubectl edit pvc pvc1 -n nginx
问题 3:网络连接问题
解决方案:
# 测试对象存储连接
aws --endpoint-url https://tenancy.compat.objectstorage.region.oraclecloud.com s3 ls
# 检查防火墙设置
curl -I https://tenancy.compat.objectstorage.region.oraclecloud.com
# 验证 DNS 解析
nslookup tenancy.compat.objectstorage.region.oraclecloud.com
最佳实践建议
备份策略
| 备份类型 | 频率 | 保留期 | 适用场景 |
|---|---|---|---|
| 全量备份 | 每周 | 3个月 | 重要生产环境 |
| 增量备份 | 每日 | 1个月 | 常规应用 |
| 迁移备份 | 一次性 | 直到迁移完成 | 迁移项目 |
性能优化
# 使用并发备份加速
velero backup create backup-name \
--include-namespaces namespace1,namespace2 \
--default-volumes-to-fs-backup \
--parallel-files-upload=10
# 排除不需要的资源
velero backup create backup-name \
--exclude-resources pods,replicasets \
--include-namespaces production
安全考虑
- 加密存储:启用 OCI 对象存储加密
- 访问控制:使用 IAM 策略限制访问权限
- 网络安全:配置 VCN 安全列表
- 审计日志:启用 OCI 审计服务
扩展应用场景
1. 多集群灾难恢复
2. 开发环境复制
# 从生产环境创建开发环境
velero backup create prod-snapshot --include-namespaces production
# 在开发集群恢复并修改命名空间
velero restore create dev-env --from-backup prod-snapshot \
--namespace-mappings production:development
3. 版本升级备份
# 升级前备份
velero backup create pre-upgrade-$(date +%Y%m%d) \
--include-cluster-resources=true \
--snapshot-volumes=true
# 升级后验证,必要时回滚
velero restore create rollback --from-backup pre-upgrade-20230722
成本优化建议
存储成本
| 存储类型 | 成本 | 适用场景 |
|---|---|---|
| 标准存储 | 较高 | 频繁访问的备份 |
| 冷存储 | 中等 | 长期保留备份 |
| 归档存储 | 最低 | 合规性备份 |
网络成本
- 跨区域传输:选择就近的存储区域
- 数据压缩:启用 Velero 压缩功能
- 增量备份:减少重复数据传输
总结
通过本指南,您已经掌握了使用 Velero 进行 EKS 到 OKE 迁移的完整流程:
✅ 完成的任务
- 环境准备:配置 OCI 访问凭据和存储桶
- 工具安装:在源和目标集群部署 Velero
- 备份创建:安全备份 Kubernetes 资源和数据
- 存储映射:处理不同云平台存储类差异
- 恢复验证:确保应用和数据完整迁移
🎯 关键优势
- 零停机迁移:应用可在备份过程中继续运行
- 数据完整性:包含持久卷和应用状态
- 跨云兼容:利用 S3-Compatible 存储标准
- 自动化程度:减少手动操作和错误风险
📈 后续行动
- 监控:设置备份监控和告警
- 自动化:创建定期备份计划
- 测试:定期进行恢复演练
- 优化:根据实际使用调整备份策略
需要专业的 Kubernetes 迁移咨询服务?联系我们的云架构专家获取个性化迁移方案。