Kubernetes备份与恢复完全指南:基于Velero与MinIO
前言
在生产环境中,Kubernetes 集群的配置和数据安全至关重要。本文将介绍如何使用 Velero 配合 MinIO 实现 Kubernetes 集群的备份与恢复功能。
一、环境概述
- MinIO:对象存储,作为 Velero 后端
- Velero:Kubernetes 备份恢复工具
- 场景:将 K8s 集群中的 Deployment、ConfigMap、Secret 等资源备份到 MinIO,并在需要时还原
二、MinIO 部署
2.1 使用 Docker 部署 MinIO
# 启动 MinIO 容器
docker run -d \
--name minio \
-p 9000:9000 \
-p 9090:9090 \
--restart=always \
-e "MINIO_ROOT_USER=minioadmin" \
-e "MINIO_ROOT_PASSWORD=minioadmin123" \
-v /data/minio:/data \
minio/minio server /data --console-address ":9090"
注意:请使用强密码替换
minioadmin123,并限制 9000 和 9090 端口的访问。
2.2 创建 Velero 存储桶
- 访问 MinIO Console(http://YOUR_MINIO_IP:9090)
- 使用刚才设置的账号密码登录
- 创建 Bucket,命名为
velero-backup
2.3 创建专用 Access Key(可选但推荐)
- 进入 MinIO Console → Access Keys
- 创建新 Access Key,记录
access_key和secret_key - 为 Velero 创建专用密钥,限制仅访问
velero-backupbucket
三、Velero 安装
3.1 下载 Velero 客户端
# 下载 Velero 二进制文件
wget https://github.com/vmware-tanzu/velero/releases/download/v1.17.2/velero-v1.17.2-linux-amd64.tar.gz
tar -xvf velero-v1.17.2-linux-amd64.tar.gz
# 移动到系统路径
sudo mv velero /usr/local/bin/velero
# 验证安装
velero version
3.2 创建 MinIO 凭证文件
# 创建 Velero 凭证目录
mkdir -p ~/.velero
# 创建凭证文件(替换为你的 MinIO Access Key)
cat > ~/.velero/credentials-velero <<EOF
[default]
aws_access_key_id=YOUR_MINIO_ACCESS_KEY
aws_secret_access_key=YOUR_MINIO_SECRET_KEY
EOF
注意:将
YOUR_MINIO_ACCESS_KEY和YOUR_MINIO_SECRET_KEY替换为实际值。
3.3 安装 Velero Server
velero install \
--namespace velero \
--provider aws \
--bucket velero-backup \
--secret-file ~/.velero/credentials-velero \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://YOUR_MINIO_IP:9000 \
--use-node-agent \
--plugins velero/velero-plugin-for-aws:v1.14.0
参数说明:
--namespace velero:Velero 安装到的命名空间--provider aws:使用 AWS S3 兼容插件(MinIO 兼容)--bucket velero-backup:存储备份的 bucket 名称--secret-file:MinIO 凭证文件路径--s3Url:MinIO 服务地址--use-node-agent:部署 node-agent DaemonSet,支持卷快照--plugins:Velero AWS 插件
3.4 验证安装
# 检查 Pod 状态
kubectl get pods -n velero
# 查看备份存储位置
velero backup-location get
正常情况应看到:
NAME PROVIDER BUCKET STATUS LAST VALIDATED
default aws velero-backup Ready 2026-01-01 00:00:00 +0000 UTC
四、备份管理
4.1 创建手动备份
# 备份所有命名空间
velero backup create backup-all-$(date +%F-%H%M) \
--include-namespaces "*" \
--snapshot-volumes=false \
--ttl 720h \
--wait
参数说明:
--include-namespaces "*":备份所有命名空间,可改为ns1,ns2备份特定命名空间--snapshot-volumes=false:不备份 PersistentVolume(设为 true 需要 CSI 快照支持)--ttl 720h:备份保留 30 天(720 小时)--wait:等待备份完成
4.2 创建定时备份
# 每天凌晨 2 点自动备份
velero schedule create backup-all \
--schedule "0 2 * * *" \
--include-namespaces "*" \
--snapshot-volumes=false \
--ttl 720h
4.3 查看备份状态
# 查看所有备份
velero backup get
# 查看备份详情
velero backup describe backup-all-2026-05-01-0200
# 查看备份日志
velero backup logs backup-all-2026-05-01-0200
4.4 查看定时任务
velero schedule get
五、还原操作
5.1 还原整个备份
# 还原所有内容
velero restore create restore-full \
--from-backup backup-all-2026-05-01-0200 \
--wait
5.2 按命名空间还原
# 只还原指定命名空间
velero restore create restore-ns-icc \
--from-backup backup-all-2026-05-01-0200 \
--include-namespaces icc-projects-online \
--wait
5.3 按标签选择还原(推荐生产使用)
# 只还原特定应用
velero restore create restore-face-net \
--from-backup backup-all-2026-05-01-0200 \
--selector app=face-net \
--include-namespaces icc-projects-online \
--wait
5.4 还原到原命名空间
# 完整还原示例
velero restore create restore-only-deployment-face-net \
--from-backup backup-all-2026-05-01-0200 \
--selector app=face-net \
--include-namespaces icc-projects-online \
--namespace-mappings icc-projects-online:icc-projects-online \
--include-resources deployments \
--wait
参数说明:
--from-backup:指定从哪个备份还原--selector:只还原匹配标签的资源--include-namespaces:指定命名空间--namespace-mappings:命名空间映射(原NS:目标NS)--include-resources:只还原指定资源类型(如 deployments、configmaps)
5.5 查看还原状态
# 查看所有还原记录
velero restore get
# 查看还原详情
velero restore describe restore-face-net
# 查看还原日志
velero restore logs restore-face-net
六、常见问题排查
6.1 Velero Pod 无法启动
# 查看 Pod 日志
kubectl logs -n velero deployment/velero
# 查看 node-agent 日志
kubectl logs -n velero daemonset/node-agent
6.2 备份失败
- 检查 MinIO 连接:
velero backup-location get - 检查 Velero 凭证是否正确
- 检查 MinIO bucket 是否存在且有写入权限
6.3 还原失败
- 确认备份是否存在:
velero backup get - 检查目标命名空间是否存在
- 查看还原日志获取详细错误
七、高级技巧
7.1 排除特定资源
velero backup create backup-exclude-kube-system \
--include-namespaces "*" \
--exclude-namespaces kube-system,kube-public \
--wait
7.2 备份钩子(Hook)
在备份前执行数据库一致性操作:
velero backup create backup-with-hook \
--include-namespaces mysql \
--hook-pre-backup "kubectl exec -n mysql primary-0 -- /usr/bin/mysqladmin flush-logs"
7.3 备份到其他存储后端
Velero 支持多种后端:
# 阿里云 OSS
velero install \
--provider aliyun \
--bucket velero-backup \
--secret-file ./credentials-aliyun \
--backup-location-config region=cn-beijing,accessKeyID=YOUR_KEY,accessKeySecret=YOUR_SECRET
# S3 兼容
velero install \
--provider aws \
--bucket velero-backup \
--secret-file ./credentials-aws \
--backup-location-config region=us-east-1
八、备份策略建议
| 场景 | 策略 |
|---|---|
| 开发/测试环境 | 每小时备份,保留 24 小时 |
| 生产环境 | 每 6 小时备份,保留 30 天 |
| 关键数据 | 每日备份,保留 90 天 |
| 长期归档 | 每周备份,保留 1 年 |
九、参考命令速查
# 安装
velero install --namespace velero --provider aws --bucket velero-backup \
--secret-file ~/.velero/credentials-velero \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://MINIO_IP:9000 \
--use-node-agent --plugins velero/velero-plugin-for-aws:v1.14.0
# 备份
velero backup create backup-$(date +%F) --include-namespaces "*" --snapshot-volumes=false --ttl 720h --wait
# 定时备份
velero schedule create backup-all --schedule "0 2 * * *" --include-namespaces "*" --snapshot-volumes=false --ttl 720h
# 还原
velero restore create restore-$(date +%F) --from-backup BACKUP_NAME --wait
# 查看
velero backup get
velero restore get
velero schedule get
velero backup-location get
总结
通过 Velero + MinIO 的组合,我们可以轻松实现 Kubernetes 集群的备份与恢复功能。建议定期测试备份还原流程,确保在真正的灾难恢复场景中能够正常工作。