用 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

验证

回滚

常见坑

相关命令