Cart

    Sorry, we could not find any results for your search querry.

    Using VolumeSnapshots in Kubernetes

    With VolumeSnapshots, you create a point-in-time snapshot of a PersistentVolumeClaim (PVC) in your Kubernetes cluster. This lets you capture the contents of a volume at a specific moment, for example before performing an application upgrade or before changing data.

    In this guide, we explain when to use VolumeSnapshots, what the costs are, how to create a snapshot, and how to restore a snapshot to a new PersistentVolumeClaim.

    • VolumeSnapshots are available for volumes created through the TransIP CSI driver, such as volumes with the StorageClass transip-fast-storage or transip-big-storage.
    • A restore does not overwrite your existing volume. Kubernetes creates a new PersistentVolumeClaim with the data from the snapshot.
    • VolumeSnapshots are useful as a restore point within your cluster, but they do not replace an external backup strategy for disaster recovery.
    • The price is €0.06 per 10 GB per month. A snapshot of 100 GB therefore costs €0.60 per month.
     

     

    Before you begin

     

    For this guide, you need the following:

    • A Kubernetes cluster with an existing PersistentVolumeClaim.
    • kubectl with access to your cluster.
    • A volume created with the TransIP CSI driver. For example, use the StorageClass transip-fast-storage or transip-big-storage.

    First, check whether VolumeSnapshots are available in your cluster:

    kubectl api-resources | grep -i volumesnapshot

    Then check which VolumeSnapshotClass is available:

    kubectl get volumesnapshotclass

    In a TransIP Kubernetes cluster, you use the VolumeSnapshotClass team-blue-k8s. This class uses the driver bs.csi.transip.nl.

    Finally, check the PersistentVolumeClaim you want to create a snapshot of:

    kubectl get pvc -n <namespace>

    Replace <namespace> with the namespace where your PVC is located.


     

    Creating a VolumeSnapshot

     

    In this example, we create a snapshot of a PVC named webdata in the namespace productie. Replace these names with the names from your own cluster.

     

    Step 1

    Create a file named volume-snapshot.yaml:

    nano volume-snapshot.yaml

    Add the following contents to the file:

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: webdata-before-upgrade
      namespace: productie
    spec:
      volumeSnapshotClassName: team-blue-k8s
      source:
        persistentVolumeClaimName: webdata

    Replace the following values:

    • webdata-before-upgrade: the name of the snapshot.
    • productie: the namespace where your PVC is located.
    • webdata: the name of the PVC you are creating a snapshot of.

    Save the changes and close the file (ctrl + x > y > enter).


     

    Step 2

    Create the VolumeSnapshot:

    kubectl apply -f volume-snapshot.yaml

     

    Step 3

    Check whether the snapshot is ready to use:

    kubectl get volumesnapshot -n productie

    Wait until READYTOUSE is set to true. You can use this command if needed:

    kubectl wait --for=jsonpath='{.status.readyToUse}'=true volumesnapshot/webdata-before-upgrade -n productie --timeout=10m

    View additional details with:

    kubectl describe volumesnapshot webdata-before-upgrade -n productie

     

    Restoring a VolumeSnapshot

     

    A restore from a VolumeSnapshot creates a new PersistentVolumeClaim. Kubernetes does not overwrite the original PVC. Only connect your application to the restored PVC after you have verified that the data is correct.

    Step 1

    Check which StorageClass the original PVC uses:

    kubectl get pvc webdata -n productie -o jsonpath='{.spec.storageClassName}{"\n"}'

    Use the same StorageClass for the restore, for example transip-fast-storage or transip-big-storage.


     

    Step 2

    Create a file named volume-snapshot-restore.yaml:

    nano volume-snapshot-restore.yaml

    Add the following contents to the file:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: webdata-restore
      namespace: productie
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: transip-fast-storage
      resources:
        requests:
          storage: 20Gi
      dataSource:
        name: webdata-before-upgrade
        kind: VolumeSnapshot
        apiGroup: snapshot.storage.k8s.io

    Replace the following values:

    • webdata-restore: the name of the new PVC.
    • productie: the namespace where the snapshot is located.
    • transip-fast-storage: the StorageClass of the new PVC.
    • 20Gi: the size of the new PVC. Use at least the restoreSize of the snapshot.
    • webdata-before-upgrade: the name of the VolumeSnapshot.

    Save the changes and close the file (ctrl + x > y > enter).


     

    Step 3

    Create the restored PVC:

    kubectl apply -f volume-snapshot-restore.yaml

    Check whether the PVC has been created:

    kubectl get pvc webdata-restore -n productie

    Wait until STATUS is set to Bound.


     

    Step 4

    Check the data on the restored PVC before changing your production workload. You can do this by creating a temporary pod, for example:

    apiVersion: v1
    kind: Pod
    metadata:
      name: check-restored-volume
      namespace: productie
    spec:
      restartPolicy: Never
      containers:
        - name: shell
          image: busybox:1.36
          command: ["sh", "-c", "ls -la /data && sleep 3600"]
          volumeMounts:
            - name: data
              mountPath: /data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: webdata-restore

    For example, save this file as check-restored-volume.yaml and create the pod:

    kubectl apply -f check-restored-volume.yaml

    Then view the contents of the volume:

    kubectl exec -n productie check-restored-volume -- ls -la /data

    Delete the temporary pod after you have checked the data:

    kubectl delete pod check-restored-volume -n productie

     

    Connecting your workload to the restored PVC

     

    After you have checked the data, connect your application to the restored PVC. The exact steps depend on your workload and deployment method.

    If you use a Deployment with a fixed claimName, scale the Deployment down first:

    kubectl scale deployment <deploymentnaam> -n productie --replicas=0

    Then update your manifest, Helm chart, or Kustomize configuration so that the workload uses webdata-restore instead of webdata:

    volumes:
      - name: data
        persistentVolumeClaim:
          claimName: webdata-restore

    Apply the updated configuration and then scale your Deployment back up:

    kubectl apply -f deployment.yaml
    kubectl scale deployment <deploymentnaam> -n productie --replicas=1

    If you use a StatefulSet, first check how your application uses the PVC. A StatefulSet often creates PVCs through volumeClaimTemplates. These PVC names are linked to the StatefulSet and pod name. In that case, first create a restore plan that fits your application, for example by copying data from the restored PVC back to a new volume with the expected name.


     

    Cleaning up VolumeSnapshots

     

    Delete snapshots you no longer need. The VolumeSnapshotClass team-blue-k8s uses deletionPolicy Delete. This means that the underlying snapshot in the storage environment is also deleted when you delete the VolumeSnapshot object.

    kubectl delete volumesnapshot webdata-before-upgrade -n productie

    Then check whether the snapshot is gone:

    kubectl get volumesnapshot -n productie

    Note: deleting a VolumeSnapshot does not automatically delete PVCs that you previously restored from that snapshot. Delete restored PVCs separately when you no longer use them.


     

    Common issues

     

    • kubectl does not recognize VolumeSnapshot: check whether you are connected to the correct cluster and whether the VolumeSnapshot CRDs are available.
    • The snapshot remains on READYTOUSE false: first wait a few minutes, then check the events with kubectl describe volumesnapshot <snapshotnaam> -n <namespace>.
    • The restore PVC remains Pending: check whether the StorageClass exists, whether the requested storage size is at least equal to the restoreSize, and whether your dataSource points to the correct VolumeSnapshot.
    • Your workload does not start with the restored PVC: check whether the accessMode matches your workload. A ReadWriteOnce volume is attached to one node at a time.
    • You want to restore to a different namespace: use the same namespace for the standard workflow. Cross-namespace restores require additional Kubernetes functionality and are not the basic workflow in this guide.

     

    You have now created, checked, and restored a VolumeSnapshot to a new PersistentVolumeClaim. Use snapshots as a quick restore point for maintenance and changes, and clean up old snapshots as soon as you no longer need them.

    Need help?

    Receive personal support from our supporters

    Contact us