Volume Snapshots

Info

FEATURE AVAILABILITY: Available in v5.7.0 and above

Volume snapshots are a convenient way to create a copy of a volume’s contents without having to create a new PersistentVolume. Taking a volume snapshot can be much faster than creating a traditional full backup. Restoring from a snapshot can also be much faster.

Despite the promise of volume snapshots, they also have notable limitations:

  • The accessibility of snapshots across zones and regions will vary with your platform. A snapshot created in zone B may require additional work to be made available in zone C.
  • Restoring from a naive snapshot can leave you with a corrupted database.

To keep your data safe, Crunchy Postgres for Kubernetes takes the additional steps to make sure that snapshot capture is properly handled in coordination with a traditional backup strategy. This strategy provides the dependability of traditional backups with the benefits of snapshot-based storage.

Prepare your environment

To use the volume snapshot feature, you will first need to know if your Kubernetes cluster has the necessary CRDs and controller to take snapshots. You can check your CRDs by running:

kubectl get crd volumesnapshotclasses.snapshot.storage.k8s.io             
kubectl get crd volumesnapshotcontents.snapshot.storage.k8s.io             
kubectl get crd volumesnapshots.snapshot.storage.k8s.io                    

If you don’t have the correct CRDs installed, install them from the external-snapshotter Github repo. You may also need to deploy the snapshot-controller.

Create a VolumeSnapshotClass

Now that you’ve ensured the VolumeSnapshot CRDs are installed, the next step is to check that you have a usable VolumeSnapshotClass in place. Some Kubernetes clusters will already have a VolumeSnapshotClass available and in some cases you will need to create one yourself. See your platform's documentation for details.

If you already have a VolumeSnapshotClass installed, you should be able to find it with:

kubectl get volumesnapshotclasses

Enable the feature gate

To enable Crunchy Postgres for Kubernetes' volume snapshot feature, activate the VolumeSnapshot feature gate. Feature gates are enabled by setting the PGO_FEATURE_GATES environment variable on the Crunchy Postgres for Kubernetes Deployment.

PGO_FEATURE_GATES="VolumeSnapshots=true"

To enable more than one feature at a time, use a comma delimited list. For example, to enable multiple features, you would set PGO_FEATURE_GATES like so:

PGO_FEATURE_GATES="FeatureName=true,FeatureName2=true,FeatureName3=true..."

Enable VolumeSnapshots for your postgrescluster

To enable the automatic capturing of volume snapshots for a given PostgresCluster, add the following to your spec:

spec:
  backups:
    snapshots:
      volumeSnapshotClassName: <name of the snapshot class>

Now, every time you take a manual backup or a scheduled backup runs, the backup will be used to build a consistent snapshot. With every new snapshot taken, the old snapshot will be deleted, ensuring that you do not need to manage snapshots on your own.

Cloning from a snapshot

Once you enable snapshots on the cluster you want to clone, your steps to create the clone are the same as they’ve always been (see Clone a Postgres Cluster), though you’ll notice some difference in what happens under the hood. Crunchy Postgres for Kubernetes will automatically look for the source cluster’s snapshot. If a snapshot is found, a new persistent volume will be populated with the data in the snapshot.

Early tests with data sets up to 100 GB show that turning on snapshots can decrease the time it takes to create a clone by 60%.