Create a PVC from a snapshot in Tanzu
Summary and Key concepts
Summary:
This article explains how to create, manage, and restore snapshots of Portworx volumes in Kubernetes. It covers both automated periodic snapshots and on-demand snapshots using Kubernetes manifests and the Portworx command-line tool pxctl
. The document also details how to use snapshots to clone volumes and restore data, with specific instructions for MySQL applications. The recommended method for managing snapshots is through Stork, and the article includes examples for triggering snapshots using annotations and inline specifications. Additionally, it walks through the process of listing snapshots, restoring from snapshots, and using snapshots to provision new PVCs for pods.
Kubernetes Concepts:
- PersistentVolumeClaim (PVC): Used to request dynamic storage in Kubernetes, can be snapshotted and cloned.
- StorageClass: Defines storage parameters, such as snapshot schedules, for PVCs.
- Annotations: Used to trigger on-demand snapshots and manage snapshot-related configurations.
- ClusterRole: Manages permissions across Kubernetes clusters, often edited to manage snapshots.
Portworx Concepts:
-
Stork: Portworx’s scheduler extension for managing data, snapshots, and backups in Kubernetes.
-
pxctl: Command-line tool used to manage Portworx volumes, snapshots, and other storage-related tasks.
-
Portworx Volume Snapshot: A feature that captures point-in-time copies of volumes for data protection or cloning.
This document will show you how to take a snapshot of a volume using Portworx and use that snapshot as the volume for a new pod.
The suggested way to manage snapshots on Kuberenetes is now to use Stork. Instructions for using Stork to manage snapshots can be found here
Managing snapshots through kubectl
Taking periodic snapshots on a running POD
When you create the Storage Class, you can specify a snapshot schedule on the volume as specified below:
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: portworx-io-priority-high
provisioner: pxd.portworx.com
parameters:
repl: "1"
snap_interval: "24"
io_priority: "high"
Creating a snapshot on demand
You can trigger a new snapshot on a running POD by creating a PersistentVolumeClaim.
Using annotations
Portworx uses a special annotation px/snapshot-source-pvc which can be used to identify the name of the source PVC whose snapshot needs to be taken.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespace: prod
name: ns.prod-name.px-snap-1
annotations:
volume.beta.kubernetes.io/storage-class: px-sc
px/snapshot-source-pvc: px-vol-1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 6Gi
Note the format of the name field - ns.<namespace_of_source_pvc>-name.<name_of_the_snapshot>
. The above example takes a snapshot with the name px-snap-1 of the source PVC px-vol-1 in the prod namespace.
You can run the following command to edit your existing Portworx ClusterRole
kubectl edit clusterrole node-get-put-list-role
Clone from a snapshot
You can restore a clone from a snapshot using the following spec file. In 1.3 and higher releases, this is required to create a read-write clone as snapshots are read only.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespace: prod
name: ns.prod-name.px-snap-restore
annotations:
volume.beta.kubernetes.io/storage-class: px-sc
px/snapshot-source-pvc: ns.prod-name.px-snap-1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
The above example restores a volume from the source snapshot PVC with name ns.prod-name.px-snap-1.