Skip to main content
Version: 3.2

Snapshot single PVCs

Summary and Key concepts

Summary:

This article demonstrates how to create and manage snapshots of Persistent Volume Claims (PVCs) backed by Portworx volumes in Kubernetes. It provides steps to create snapshots within a single namespace and across multiple namespaces. The guide shows how to manage snapshots using Kubernetes manifests, annotations, and the storkctl command-line tool. Additionally, it includes an example of creating a PVC from a snapshot in another namespace and offers methods to verify snapshots using both kubectl and storkctl commands. The article also explains how to specify cross-namespace snapshot restoration through annotations.

Kubernetes Concepts:

  • PersistentVolumeClaim (PVC): A request for storage in Kubernetes, can be snapshotted and restored.
  • Namespace: Used for isolating resources such as PVCs and snapshots within Kubernetes clusters.
  • StorageClass: Defines how storage is dynamically provisioned for PVCs.
  • Annotations: Attach metadata to objects, such as defining snapshot sources and cross-namespace restoration.

Portworx Concepts:

  • Stork: A storage orchestration tool that manages snapshots, backups, and cross-namespace restoration in Kubernetes.

  • VolumeSnapshot: A snapshot of a Portworx volume, allowing for point-in-time data restoration.

  • storkctl: A CLI tool for managing Portworx storage operations like snapshots and backups.

This document will show you how to create a snapshot of a PVC backed by a Portworx volume.

note

This method is not supported for FlashArray Direct Access volumes. Use the CSI-based snapshots method for snapshotting FlashArray Direct Access PVCs.

Creating snapshot within a single namespace

  1. If you have a PVC called jenkins-home-jenkins-master-0, in the jenkins namespace, you can create a snapshot for that PVC by using the following spec:

    apiVersion: volumesnapshot.external-storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
    name: jenkins-home-jenkins-master-0
    namespace: jenkins
    spec:
    persistentVolumeClaimName: jenkins-home-jenkins-master-0
  2. Once you apply the above object you can check the status of the snapshots using kubectl:

    kubectl get volumesnapshot.volumesnapshot.external-storage.k8s.io -n jenkins
    NAME                                                      AGE
    jenkins-jobs-jenkins-master-0-snapshot-2019-03-20-snap1 6m
    kubectl get volumesnapshot.volumesnapshotdatas.external-storage.k8s.io -n jenkins 
    NAME                                                       AGE
    k8s-volume-snapshot-xxxxxxxx-xxxx-xxxx-xxxx-0242ac110002 8m
  3. The creation of the volumesnapshotdatas object indicates that the snapshot has been created. If you describe the volumesnapshotdatas object you can see the Portworx Volume Snapshot ID and the PVC for which the snapshot was created:

kubectl describe volumesnapshotdatas
Name:         k8s-volume-snapshot-xxxxxxxx-xxxx-xxxx-xxxx-0242ac110002
Namespace:
Labels: <none>
Annotations: <none>
API Version: volumesnapshot.external-storage.k8s.io/v1
Kind: VolumeSnapshotData
Metadata:
Creation Timestamp: 2019-03-20T22:22:37Z
Generation: 1
Resource Version: 56596513
Self Link: /apis/volumesnapshot.external-storage.k8s.io/v1/volumesnapshotdatas/k8s-volume-snapshot-xxxxxxxx-xxxx-xxxx-xxxx-0242ac110002
UID: xxxxxxxx-xxxx-xxxx-xxxx-0cc47ab5f9a2
Spec:
Persistent Volume Ref:
Kind: PersistentVolume
Name: pvc-xxxxxxxx-xxxx-xxxx-xxxx-0cc47ab5f9a2
Portworx Volume:
Snapshot Id: 411710013297550893
Snapshot Type: local
Volume Snapshot Ref:
Kind: VolumeSnapshot
Name: jenkins/jenkins-jobs-jenkins-master-0-snapshot-2019-03-20-snap1-xxxxxxxx-xxxx-xxxx-xxxx-0cc47ab5f9a2
Status:
Conditions:
Last Transition Time: 2019-03-20T22:22:37Z
Message: Snapshot created successfully and it is ready
Reason:
Status: True
Type: Ready
Creation Timestamp: <nil>
Events: <none>
  1. You can use the storkctl command to verify that the snapshot was created successfully:

    storkctl -n jenkins get snap
    NAME                                                      PVC                             STATUS    CREATED               COMPLETED             TYPE
    jenkins-jobs-jenkins-master-0-snapshot-2019-03-20-snap1 jenkins-jobs-jenkins-master-0 Ready 20 Mar 19 15:22 PDT 20 Mar 19 15:22 PDT local

For details about how you can restore a snapshot to a new PVC or the original PVC, see the Restore snapshots section.

Creating snapshots across namespaces

When creating snapshots, you can provide comma separated regexes with stork.libopenstorage.org/snapshot-restore-namespaces annotation to specify which namespaces the snapshot can be restored to. When creating PVC from snapshots, if a snapshot exists in another namespace, the snapshot namespace should be specified with stork.libopenstorage.org/snapshot-source-namespace annotation.

Let's take an example where we have 2 namespaces dev and prod. We will create a PVC and snapshot in the dev namespace and then create a PVC in the prod namespace from the snapshot.

  1. Create the namespaces

    apiVersion: v1
    kind: Namespace
    metadata:
    name: dev
    labels:
    name: dev
    ---
    apiVersion: v1
    kind: Namespace
    metadata:
    name: prod
    labels:
    name: prod
  2. Create the PVC

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
    name: mysql-data
    namespace: dev
    annotations:
    volume.beta.kubernetes.io/storage-class: px-mysql-sc
    spec:
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 2Gi
    ---
    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
    name: px-mysql-sc
    provisioner: pxd.portworx.com
    parameters:
    repl: "2"
  3. Create the snapshot

    apiVersion: volumesnapshot.external-storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
    name: mysql-snapshot
    namespace: dev
    annotations:
    stork.libopenstorage.org/snapshot-restore-namespaces: "prod"
    spec:
    persistentVolumeClaimName: mysql-data

  4. Create a PVC in a different namespace from the snapshot

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: mysql-clone
    namespace: prod
    annotations:
    snapshot.alpha.kubernetes.io/snapshot: mysql-snapshot
    stork.libopenstorage.org/snapshot-source-namespace: dev
    spec:
    accessModes:
    - ReadWriteOnce
    storageClassName: stork-snapshot-sc
    resources:
    requests:
    storage: 2Gi