Restoring a volume from a snapshot via CSI

We can create snapshots in an attempt to restore other snapshots. Let's perform the following steps to restore the snapshot we created in the previous recipe:

  1. Restore the volume from the snapshot with a PVC using the following command. As you can see, a new PVC named csi-ebs-pvc-restored will be created based on the ebs-volume-snapshot snapshot:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-ebs-pvc-restored
spec:
accessModes:
- ReadWriteOnce
storageClassName: aws-csi-ebs
resources:
requests:
storage: 4Gi
dataSource:
name: ebs-volume-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
EOF
  1. Create another pod that will continue to write to the /data/out.txt file inside the PV. This step will ensure that the volume is still accessible after it's been created:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: newapp
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-restored
EOF
  1. Confirm that the newapp pod contains the restored data and the timestamps from the Creating a volume snapshot recipe:
$ kubectl exec -it newapp cat /data/out.txt

With this, you've learned how to provision persistent volumes from an existing snapshot. This is a very useful step in a CI/CD pipeline so that you can save time troubleshooting failed pipelines.