简体   繁体   English

kubernetes 中的定期数据库备份?

[英]Periodic database backup in kubernetes?

How to setup periodic database backup in kubernetes?如何在 kubernetes 中设置定期数据库备份? I have deployed a postgres database as StatefulSet in kubernetes and mounted a PersistantVolume to store data.我在 kubernetes 中部署了一个 postgres 数据库作为StatefulSet并安装了一个PersistantVolume来存储数据。 Now to get periodic backup for the mounted volume, I found three options,现在要定期备份已安装的卷,我找到了三个选项,

  1. Setup a CronJob in kubernetes and execute pg_dump and upload to the storage location.CronJob中设置一个CronJob并执行 pg_dump 并上传到存储位置。
  2. I have already using celery in my project.我已经在我的项目中使用了 celery。 So now add a new task to backup postgres data and upload to the storage location.所以现在添加一个新任务来备份postgres数据并上传到存储位置。
  3. VolumeSnapshots.卷快照。 This looks more kubernetes way.这看起来更像 kubernetes 方式。 But I couldn't find a way to automate this periodically.但我找不到一种方法来定期自动执行此操作。

Which is the recommended way to take periodic volume backup in kubernetes?在 kubernetes 中进行定期卷备份的推荐方法是什么?

[EDIT] I prefer solutions without using operators. [编辑] 我更喜欢不使用运算符的解决方案。

Which is the recommended way to take periodic volume backup in kubernetes?在 kubernetes 中进行定期卷备份的推荐方法是什么?

I would recommend VolumeSnapshots , but you need to keep in mind that this is not normal backup and you won't be able to revert the data to previous state.我会推荐VolumeSnapshots ,但您需要记住,这不是正常的备份,您将无法将数据恢复到以前的状态。

Many storage systems (like Google Cloud Persistent Disks, Amazon Elastic Block Storage, and many on-premise storage systems) provide the ability to create a “snapshot” of a persistent volume.许多存储系统(如 Google Cloud Persistent Disks、Amazon Elastic Block Storage 和许多本地存储系统)都提供了创建持久卷“快照”的能力。 A snapshot represents a point-in-time copy of a volume.快照代表卷的时间点副本。 A snapshot can be used either to provision a new volume (pre-populated with the snapshot data) or to restore an existing volume to a previous state (represented by the snapshot).快照可用于配置新卷(预先填充快照数据)或将现有卷恢复到以前的状态(由快照表示)。

It's easy to use, as of December 2019 it was moved to beta Kubernetes 1.17 Feature: Kubernetes Volume Snapshot Moves to Beta .它易于使用,截至 2019 年 12 月,它已移至测试版Kubernetes 1.17 功能:Kubernetes 卷快照移至 Beta 版

Once you specify the VolumeSnapshotClass一旦你指定了VolumeSnapshotClass

The following VolumeSnapshotClass, for example, tells the Kubernetes cluster that a CSI driver, testdriver.csi.k8s.io , can handle volume snapshots, and that when these snapshots are created, their deletion policy should be to delete.例如,以下 VolumeSnapshotClass 告诉 Kubernetes 集群 CSI 驱动程序testdriver.csi.k8s.io可以处理卷快照,并且当创建这些快照时,它们的删除策略应该是删除。

 apiVersion: snapshot.storage.k8s.io/v1beta1 kind: VolumeSnapshotClass metadata: name: test-snapclass driver: testdriver.csi.k8s.io deletionPolicy: Delete parameters: csi.storage.k8s.io/snapshotter-secret-name: mysecret csi.storage.k8s.io/snapshotter-secret-namespace: mysecretnamespace

The common snapshot controller reserves the parameter keys csi.storage.k8s.io/snapshotter-secret-name and csi.storage.k8s.io/snapshotter-secret-namespace .通用快照控制器保留参数键csi.storage.k8s.io/snapshotter-secret-namecsi.storage.k8s.io/snapshotter-secret-namespace If specified, it fetches the referenced Kubernetes secret and sets it as an annotation on the volume snapshot content object.如果指定,它会获取引用的 Kubernetes 机密并将其设置为卷快照内容对象上的注释。 The CSI external-snapshotter sidecar retrieves it from the content annotation and passes it to the CSI driver during snapshot creation. CSI 外部快照程序 sidecar 从内容注释中检索它,并在快照创建期间将其传递给 CSI 驱动程序。

Creation of a volume snapshot is triggered by the creation of a VolumeSnapshot API object.卷快照的创建由 VolumeSnapshot API 对象的创建触发。

The VolumeSnapshot object must specify the following source type: persistentVolumeClaimName - The name of the PVC to snapshot. VolumeSnapshot 对象必须指定以下源类型: persistentVolumeClaimName - 要进行快照的 PVC 的名称。 Please note that the source PVC, PV, and VolumeSnapshotClass for a VolumeSnapshot object must point to the same CSI driver.请注意,VolumeSnapshot 对象的源 PVC、PV 和 VolumeSnapshotClass 必须指向相同的 CSI 驱动程序。

You can create VolumeSnapshot which in this example will make s snapshot of PVC called test-pvc :您可以创建VolumeSnapshot ,在本例中,它会生成名为test-pvc的 PVC 快照:

apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
  name: test-snapshot
spec:
  volumeSnapshotClassName: test-snapclass
  source:
    persistentVolumeClaimName: test-pvc

When volume snapshot creation is invoked, the common snapshot controller first creates a VolumeSnapshotContent object with the volumeSnapshotRef , source volumeHandle , volumeSnapshotClassName if specified, driver , and deletionPolicy .调用卷快照创建时,公共快照控制器首先创建一个 VolumeSnapshotContent 对象,其中volumeSnapshotRef 、源volumeHandlevolumeSnapshotClassName如果指定)、 driverdeletionPolicy

You can restore PersistentVolumeClaim from a Volume Snapshot:您可以从卷快照恢复 PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: restore-pvc
spec:
  storageClassName: csi-hostpath-sc
  dataSource:
    name: test-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

To enable support for restoring a volume from a volume snapshot data source, enable the VolumeSnapshotDataSource feature gate on the apiserver and controller-manager.要支持从卷快照数据源恢复卷,请在 apiserver 和控制器管理器上启用VolumeSnapshotDataSource功能门。

What are the limitations?有哪些限制?

  • Does not support reverting an existing volume to an earlier state represented by a snapshot (beta only supports provisioning a new volume from a snapshot).不支持将现有卷恢复到由快照表示的早期状态(测试版仅支持从快照配置新卷)。
  • No snapshot consistency guarantees beyond any guarantees provided by storage system (eg crash consistency).除了存储系统提供的任何保证(例如崩溃一致性)之外,没有快照一致性保证。 These are the responsibility of higher level APIs/controllers这些是更高级别的 API/控制器的责任

EDIT:编辑:

To automate this process you would need to setup a CronJob or write a Python code using Python client library for kubernetes and change for example python/examples/custom_object.py to your needs.要自动执行此过程,您需要设置CronJob或使用Python 客户端库为 kubernetes编写 Python 代码并根据需要更改例如python/examples/custom_object.py

You can also use already developed apps like stash.run .您还可以使用已经开发的应用程序,例如stash.run

Velero does offer periodic volume snapshots. Velero 确实提供定期卷快照。 I would probably start there.我可能会从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM