CSI Driver and Volume Snapshots

CSI Driver and Volume Snapshots

Overview

This tutorial explains how to set up the CSI Hostpath Driver in minikube and create volume snapshots.

Prerequisites

  • latest version of minikube
  • kubernetes v1.20 or later

What you’ll need

Support for volume snapshots in minikube is provided through the volumesnapshots addon. This addon provisions the required CRDs and deploys the Volume Snapshot Controller. It is disabled by default.

Furthermore, the default storage provider in minikube does not implement the CSI interface and thus is NOT capable of creating/handling volume snapshots. For that, you must first deploy a CSI driver. To make this step easy, minikube offers the csi-hostpath-driver addon, which deploys the CSI Hostpath Driver. This addon is disabled by default as well.

Thus, to utilize the volume snapshots functionality, you must:

1) enable the volumesnapshots addon AND
2a) either enable the csi-hostpath-driver addon OR
2b) deploy your own CSI driver

You can enable/disable either of the above-mentioned addons using

  1. minikube addons enable [ADDON_NAME]
  2. minikube addons disable [ADDON_NAME]

The csi-hostpath-driver addon deploys its required resources into the kube-system namespace and sets up a dedicated storage class called csi-hostpath-sc that you need to reference in your PVCs. The driver itself is created under the name hostpath.csi.k8s.io. Use this wherever necessary (e.g. snapshot class definitions).

Once both addons are enabled, you can create persistent volumes and snapshots using standard ways (for a quick test of volume snapshots, you can find some example yaml files along with a step-by-step here). The driver stores all persistent volumes in the /var/lib/csi-hostpath-data/ directory of minikube’s host.

Tutorial

In this tutorial, you use volumesnapshots addon(1) and csi-hostpath-driver addon(2a).

1Start your cluster

  1. minikube start

2Enable addons

Enable volumesnapshots and csi-hostpath-driver addons:

  1. minikube addons enable volumesnapshots
  2. minikube addons enable csi-hostpath-driver

3Check volume snapshot class

When you create the volume snapshot, you have to register Volume Snapshot Classes to your cluster. The default VolumeSnapshotClass called csi-hostpath-snapclass is already registered by csi-hostpath-driver addon. You can check the VolumeSnapshotClass by the following command:

  1. kubectl get volumesnapshotclasses
  2. NAME DRIVER DELETIONPOLICY AGE
  3. csi-hostpath-snapclass hostpath.csi.k8s.io Delete 10s

4Prepare persistent volume

Create persistent volume claim to create persistent volume dynamically:

  1. # example-pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: csi-pvc
  6. spec:
  7. accessModes:
  8. - ReadWriteOnce
  9. resources:
  10. requests:
  11. storage: 1Gi
  12. storageClassName: csi-hostpath-sc
  1. kubectl apply -f example-pvc.yaml

You can confirm that persistent volume is created by the following command:

  1. kubectl get pv
  2. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  3. pvc-388c33e2-de56-475c-8dfd-4990d5f7a640 1Gi RWO Delete Bound default/csi-pvc csi-hostpath-sc 60s

5Take a volume snapshot

You can take a volume snapshot for persistent volume claim:

  1. # example-csi-snapshot.yaml
  2. apiVersion: snapshot.storage.k8s.io/v1
  3. kind: VolumeSnapshot
  4. metadata:
  5. name: snapshot-demo
  6. spec:
  7. volumeSnapshotClassName: csi-hostpath-snapclass
  8. source:
  9. persistentVolumeClaimName: csi-pvc
  1. kubectl apply -f example-csi-snapshot.yaml

You could get volume snapshot. You can confirm your volume snapshot by the following command:

  1. kubectl get volumesnapshot
  2. NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE
  3. snapshot-demo true csi-pvc 1Gi csi-hostpath-snapclass snapcontent-19730fcb-c34a-4f1a-abf2-6c5a9808076b 5s 5s

6Restore from volume snapshot

You can restore persistent volume from your volume snapshot:

  1. # example-csi-restore.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: csi-pvc-restore
  6. spec:
  7. storageClassName: csi-hostpath-sc
  8. dataSource:
  9. name: snapshot-demo
  10. kind: VolumeSnapshot
  11. apiGroup: snapshot.storage.k8s.io
  12. accessModes:
  13. - ReadWriteOnce
  14. resources:
  15. requests:
  16. storage: 1Gi
  1. kubectl apply -f example-csi-restore.yaml

You can confirm that persistent volume claim is created from VolumeSnapshot:

  1. kubectl get pvc
  2. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  3. csi-pvc Bound pvc-388c33e2-de56-475c-8dfd-4990d5f7a640 1Gi RWO csi-hostpath-sc 23m
  4. csi-pvc-restore Bound pvc-496bab30-9bd6-4abb-94e9-d2e9e1c8f210 1Gi RWO csi-hostpath-sc 26s

Last modified March 12, 2021: Upgrade csi-hostpath addon to v1.6.0 (788e91007)