Volume snapshots
Volume snapshots allows you to initialize persistent volume claims with the contents of a preexisting volume snapshot. Use volume snapshots when working with large datasets or if you want to create development environments with real data coming from your production or staging environments.
Requirements
- Self-Hosted
- SaaS
Follow this guide to enable Volume Snapshots in your cluster.
Use a storage class provisioned by a CSI driver that supports volume snapshots for the source persistent volume of your volume snapshots.
If your instance is hosted by Okteto, Volume Snapshots are enabled by default.
Important: you need to use the csi-okteto
storage classes for the source persistent volume of your volume snapshots.
Using Volume Snapshots in your Development Environment
Okteto enables developers to initialize persistent volume claims with the contents of a pre-existing volume snapshot. The volume snapshot is created from a persistent volume claim and it can contain database backups, big files, images, a copy of your staging data, etc.
In order to use volume snapshots with your development environment you need to:
- Create the source persistent volume
- Create a volume snapshot of the source persistent volume
- Consume the volume snapshot in a new persistent volume
Creating the Source Persistent Volume
First, create the source persistent volume. This is the data that you want to be able to clone into your development environment. In the example below we use a database, but this could be anything that uses a volume for storage: databases, ML models, images, etc...
If the default storage class of your cluster doesn't support volume snapshots, make sure you set the storage class to one that is compatible when creating your persistent volume. Check the requirements section to learn more about the available storage classes in Okteto.
- Kubernetes
- Compose
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
storageClassName: csi-okteto
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
services:
mysql:
image: mysql
volumes:
- mysql-pvc:/var/lib/mysql
volumes:
mysql-pvc:
driver_opts:
class: csi-okteto
Creating a Volume Snapshot
After creating the source persistent volume, the next step is to create the Volume Snapshot. Volume snapshots are created with the content of the persistent volume at the time of creating the volume snapshot. Further updates on the persistent volume aren't reflected in the volume snapshot content.
This step is independent of whether you created your persistent volume with Kubernetes manifests or a Docker Compose file:
The following manifest creates a volume snapshot from the mysql-pvc
persistent volume claim:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: mysql-snapshot
spec:
volumeSnapshotClassName: okteto-snapshot-class
source:
persistentVolumeClaimName: mysql-pvc
Consuming a Volume Snapshot
Finally, you can use the volume snapshot created in the previous step to populate a new persistent volume.
Use the dev.okteto.com/from-snapshot-name
and dev.okteto.com/from-snapshot-namespace
annotations on any persistent volume claim to tell Okteto to initialize your persistent volume claim from an existing volume snapshot, as shown below:
- Kubernetes
- Compose
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
dev.okteto.com/from-snapshot-name: mysql-snapshot
dev.okteto.com/from-snapshot-namespace: staging
name: pvc-from-snapshot
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
services:
mongodb:
image: mysql
volumes:
- data:/var/lib/mysql
volumes:
data:
labels:
dev.okteto.com/from-snapshot-name: mysql-snapshot
dev.okteto.com/from-snapshot-namespace: staging
If the annotation dev.okteto.com/from-snapshot-namespace
is not defined, Okteto will default to the namespace of the new persistent volume claim.
Use the annotation dev.okteto.com/skip-snapshot-if-same-namespace: "true"
to skip the data cloning operation if the source snapshot and the new persistent volume claim are in the same namespace.
For a full end to end sample, check the following resources: