How to Import a DigitalOcean Snapshot into a Kubernetes Cluster

DigitalOcean Kubernetes (DOKS) is a managed Kubernetes service. Deploy Kubernetes clusters with a fully managed control plane, high availability, autoscaling, and native integration with DigitalOcean Load Balancers and volumes. DOKS clusters are compatible with standard Kubernetes toolchains and the DigitalOcean API and CLI.


You can import an existing DigitalOcean volume snapshot into a Kubernetes cluster by applying three new YAML files that link your existing snapshot, create a local snapshot, and request for space on your Kubernetes cluster.

Find the Snapshot ID

First, find the existing snapshot’s ID by making a request to our API or running the following command:

doctl compute snapshot list

Create a Volume Snapshot Content

Create a VolumeSnapshotContent spec called snapshotcontent.yaml. This file specifies your existing snapshot as the source of data to be imported. Write the following content, replacing <your-snapshot-ID> with your snapshot ID:

    
        
            
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotContent
metadata:
  name: manually-created-snapshot-content
spec:
  deletionPolicy: Retain
  driver: dobs.csi.digitalocean.com
  source:
    snapshotHandle: your-snapshot-ID
  volumeSnapshotRef:
    name: test-snapshot
    namespace: default

        
    

The name field for VolumeSnapshotContent is the name of the volume snapshot content and the name field for volumeSnapshotRef is the reference of the corresponding volume snapshot. You can pick any name you want for them, but the names must match in the VolumeSnapshot spec you are going to create later.

The deletionPolicy is set to Retain. This setting preserves the VolumeSnapshotContent and the DigitalOcean snapshot resource when you delete the VolumeSnapshot, instead of automatically deleting them. To delete all three files together when deleting VolumeSnapshot, change this value to Delete.

Save and exit the file, then use kubectl apply to create the volume snapshot content:

kubectl apply -f snapshotcontent.yaml

Create a Volume Snapshot

Create a VolumeSnapshot spec called snapshot.yml. This file creates a request local snapshot using the name of the VolumeSnapshotContent as the source for the snapshot. Write the following content, ensuring that the name used matches with the previously-created VolumeSnapshotContent:

    
        
            
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: test-snapshot
spec:
  source:
        volumeSnapshotContentName: manually-created-snapshot-content

        
    

Save and exit the file, then use kubectl apply to create the volume snapshot:

kubectl apply -f snapshot.yaml

Wait for the snapshot to be imported successfully.

Import the Volume

Lastly, once the snapshot is ready, source the snapshot from a PersistentVolumeClaim (PVC). The file requests space for the new data on the Kubernetes cluster. Create a file called pvc.yaml and write the following content, ensuring that the name of the VolumeSnapshot matches with the previous file:

    
        
            
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-restore
spec:
  dataSource:
    name: test-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

        
    

Save and exit the file, then kubectl apply to create the persistent volume claim:

kubectl apply -f pvc.yaml

This command causes the PersistentVolumeClaim to dynamically create a PersistentVolume.

You have now imported a DigitalOcean snapshot into a Kubernetes cluster and can use it as a volume in your workload.