系统学习 演练学习路径:35 个 lab 的难度排序、4 周基础计划与复盘方法

用 Velero 搭建 Kubernetes 备份与恢复

适用场景

需要备份 Kubernetes 资源和持久卷,支持误删恢复、集群迁移和灾备演练。Velero 把对象存到外部对象存储,并通过节点代理备份 PV 数据。

前置条件

步骤

  1. 准备凭据文件:
cat > credentials-velero <<'EOF'
[default]
aws_access_key_id=MINIO_ACCESS_KEY
aws_secret_access_key=MINIO_SECRET_KEY
EOF
  1. 安装 Velero:
velero install \
  --provider aws \
  --plugins velero/velero-plugin-for-aws:v1.10.0 \
  --bucket k8s-backup \
  --secret-file ./credentials-velero \
  --backup-location-config region=minio,s3ForcePathStyle=true,s3Url=http://minio.storage:9000 \
  --use-node-agent
  1. 检查 BackupStorageLocation 和 VolumeSnapshotLocation:
velero backup-location get
velero snapshot-location get
kubectl -n velero get pods
  1. 创建一次性备份:
velero backup create demo-$(date +%Y%m%d%H%M) \
  --include-namespaces demo \
  --default-volumes-to-fs-backup \
  --wait
  1. 创建定时备份和保留策略:
velero schedule create daily-demo \
  --schedule="0 2 * * *" \
  --include-namespaces demo \
  --ttl 720h
  1. 恢复前先 dry-run 或恢复到新命名空间验证:
velero restore create --from-backup demo-20261018020000 \
  --namespace-mappings demo:demo-restore \
  --wait

验证

回滚

常见坑

相关命令

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. 只备份 Kubernetes 对象、没有备份 PV 数据,会有什么问题?
  2. 跨集群恢复时 StorageClass 名称不同会怎样?
  3. 恢复本身失败时,如何回滚?
答案
  1. 卷数据不完整,必须启用 node-agent 或快照。
  2. PVC 会 Pending,需要提前做 StorageClass 映射。
  3. 恢复是新增资源,删除恢复出来的命名空间或资源即可。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令