Skip to content

Instantly share code, notes, and snippets.

@janeczku
Last active November 14, 2023 09:54
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save janeczku/2c644022f80187bd004293b7bdfad8bf to your computer and use it in GitHub Desktop.
How to use the K8s Volume Snapshot Subsystem with Rancher RKE

Here are the steps to enable and test the Kubernetes Volume Snaphot subsystem on Rancher RKE or RKE2.

Deploying the volume snapshot controller and CRDs is required for managing snapshots for storage providers such as Longhorn, Rook/Ceph via the native K8s API as well as for integrating Rancher-managed clusters with backup solutions like Kasten K10.

1. Clone the repository

export SNAPSHOT_CTRL_VER=v5.0.1
git clone -b $SNAPSHOT_CTRL_VER https://github.com/kubernetes-csi/external-snapshotter.git
cd external-snapshotter

2. Install the Snapshot CRDs

kubectl kustomize client/config/crd | kubectl create -f -

3. Deploy the Snapshot Controller

kubectl -n kube-system kustomize deploy/kubernetes/snapshot-controller | kubectl create -f -
kubectl -n kube-system rollout status deploy/snapshot-controller

4. Install the CSI Driver for the storage solution, e.g. Longhorn

See: https://longhorn.io/docs/1.2.3/deploy/install/

5. Add Default Snapshot Class

Ensure the Snapshot CRDs are available:

kubectl -n kube-system wait --for condition=established --timeout=60s crd/volumesnapshotclasses.snapshot.storage.k8s.io

Create a default Snapshot Class, for example for Longhorn:

K8s 1.21+

cat <<K8S | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: longhorn
driver: driver.longhorn.io
deletionPolicy: Delete
K8S

K8s 1.20

cat <<K8S | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
  name: longhorn
driver: driver.longhorn.io
deletionPolicy: Delete
K8S

5. Create a Persistent Volume

For example:

cat <<K8S | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: longhorn
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-stateful-world
spec:
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: busybox
        command: ['sh', '-c', 'echo "Hello World!" >/srv/hello/hello.txt && sleep 3600']
        volumeMounts:
        - name: test-volume
          mountPath: /srv/hello/
      volumes:
      - name: test-volume
        persistentVolumeClaim:
          claimName: test-pvc
K8S

7. Create a Snapshot

Wait for the Pod:

kubectl rollout status deploy/hello-stateful-world

Create the snapshot CR:

v1.21+

cat <<K8S | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: test-snapshot
spec:
  volumeSnapshotClassName: longhorn
  source:
    persistentVolumeClaimName: test-pvc
K8S

v1.20

cat <<K8S | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
  name: test-snapshot
spec:
  volumeSnapshotClassName: longhorn
  source:
    persistentVolumeClaimName: test-pvc
K8S

8. Verify that the snapshot was created

kubectl get volumesnapshotcontents --watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment