kubernetes中的定期数据库备份? [英] Periodic database backup in kubernetes?

查看:156
本文介绍了kubernetes中的定期数据库备份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在kubernetes中设置数据库定期备份?我已经在kubernetes中将Postgres数据库部署为StatefulSet,并安装了PersistantVolume来存储数据.现在,要获得已装载卷的定期备份,我发现了三个选项,

How to setup periodic database backup in kubernetes? I have deployed a postgres database as StatefulSet in kubernetes and mounted a PersistantVolume to store data. Now to get periodic backup for the mounted volume, I found three options,

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

在kubernetes中推荐哪种方式进行定期卷备份?

Which is the recommended way to take periodic volume backup in kubernetes?

我更喜欢不使用运算符的解决方案.

I prefer solutions without using operators.

推荐答案

在kubernetes中推荐哪种方式进行定期卷备份?

Which is the recommended way to take periodic volume backup in kubernetes?

我建议 VolumeSnapshots ,但是您需要牢记这不是正常备份,您将无法将数据还原到以前的状态.

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.

许多存储系统(例如Google Cloud永久磁盘,Amazon Elastic Block Storage和许多本地存储系统)都可以创建持久卷的快照".快照表示卷的时间点副本.快照可用于设置新卷(预填充了快照数据)或将现有卷恢复到先前状态(由快照表示).

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. 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).

易于使用,截至2019年12月,它已移至Beta版

It's easy to use, as of December 2019 it was moved to beta Kubernetes 1.17 Feature: Kubernetes Volume Snapshot Moves to Beta.

一旦指定了VolumeSnapshotClass

例如,以下VolumeSnapshotClass告诉Kubernetes集群CSI驱动程序testdriver.csi.k8s.io可以处理卷快照,并且在创建这些快照时,其删除策略应为删除.

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.

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

公用快照控制器保留参数键csi.storage.k8s.io/snapshotter-secret-namecsi.storage.k8s.io/snapshotter-secret-namespace.如果指定,它将获取引用的Kubernetes机密并将其设置为卷快照内容对象上的注释. CSI外部快照者sidecar从内容注释中检索它,并将其在快照创建过程中传递给CSI驱动程序.

The common snapshot controller reserves the parameter keys csi.storage.k8s.io/snapshotter-secret-name and csi.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. The CSI external-snapshotter sidecar retrieves it from the content annotation and passes it to the CSI driver during snapshot creation.

通过创建VolumeSnapshot API对象触发创建卷快照.

Creation of a volume snapshot is triggered by the creation of a VolumeSnapshot API object.

VolumeSnapshot对象必须指定以下源类型:persistentVolumeClaimName-要快照的PVC的名称.请注意,VolumeSnapshot对象的源PVC,PV和VolumeSnapshotClass必须指向相同的CSI驱动程序.

The VolumeSnapshot object must specify the following source type: persistentVolumeClaimName - The name of the PVC to snapshot. Please note that the source PVC, PV, and VolumeSnapshotClass for a VolumeSnapshot object must point to the same CSI driver.

您可以创建VolumeSnapshot,在本示例中,该文件将制作PVC的快照,称为test-pvc:

You can create VolumeSnapshot which in this example will make s snapshot of PVC called test-pvc:

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

调用卷快照创建时,公共快照控制器首先使用volumeSnapshotRef,源volumeHandlevolumeSnapshotClassName(如果已指定),driverdeletionPolicy创建一个VolumeSnapshotContent对象.

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.

您可以从卷快照中还原PersistentVolumeClaim:

You can restore PersistentVolumeClaim from a Volume Snapshot:

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

要支持从卷快照数据源还原卷,请启用apiserver和控制器管理器上的VolumeSnapshotDataSource功能门.

有什么限制?

  • 不支持将现有卷恢复为快照表示的较早状态(beta版仅支持从快照配置新卷).
  • 没有快照一致性保证会超出存储系统提供的任何保证(例如崩溃一致性).这些是高级API/控制器的责任
  • 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 (e.g. crash consistency). These are the responsibility of higher level APIs/controllers

要自动执行此过程,您需要设置CronJob或使用 Python客户端编写Python代码kubernetes的库,并更改例如 python/examples /custom_object.py 满足您的需求.

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.

您还可以使用已开发的应用程序,例如 stash.run .

You can also use already developed apps like stash.run.

这篇关于kubernetes中的定期数据库备份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆