Creating a volume snapshot via CSI

A volume snapshot is a copy of the state taken from a PVC in the Kubernetes cluster. It is a useful resource for bringing up a stateful application using existing data. Let's perform the following steps to create a volume snapshot using CSI:

  1. Create a PVC or select an existing one. In our recipe, we'll use the AWS EBS CSI driver and the aws-csi-ebs storage class we created in Chapter 5, Preparing for Stateful Workloads, in the Installing an EBS CSI driver to manage EBS volumes recipe:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-ebs-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: aws-csi-ebs
resources:
requests:
storage: 4Gi
EOF
  1. Create a pod that will write to the /data/out.txt file inside the PersistentVolume (PV):
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: centos
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: csi-ebs-pvc
EOF
  1. Create a VolumeSnapshotClass. Make sure that the snapshot provider is set to your CSI driver name. In this recipe, this is ebs.csi.aws.com:
$ cat <<EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1alpha1
kind: VolumeSnapshotClass
metadata:
name: csi-ebs-vsc
snapshotter: ebs.csi.aws.com
EOF

  1. A PVC must be created using the CSI driver of a storage vendor. In our recipe, we will use the PVC we created in the Installing EBS CSI driver to manage EBS volumes recipe. Now, create a VolumeSnapshot using the PVC name (csi-ebs-pvc) we set in Step 1:
$ cat <<EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1alpha1
kind: VolumeSnapshot
metadata:
name: ebs-volume-snapshot
spec:
snapshotClassName: csi-ebs-vsc
source:
name: csi-ebs-pvc
kind: PersistentVolumeClaim
EOF
  1. List the Volume Snapshots:
$ kubectl get volumesnapshot
NAME AGE
ebs-volume-snapshot 18s
  1. Validate that the status is Ready To Use: true when checking the output of the following command:
$ kubectl describe volumesnapshot ebs-volume-snapshot
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.102