Create clones of persistent volumes

This document shows you how to use Kubernetes volume cloning to clone persistent volumes in your Google Kubernetes Engine (GKE) clusters.

Overview

A clone is a new independent volume that is a duplicate of an existing Kubernetes volume. A clone is similar to a volume snapshot in that it's a copy of a volume at a specific point in time. However, rather than creating a snapshot object from the source volume, volume cloning provisions the clone with all the data from the source volume.

Requirements

To use volume cloning on GKE, you must meet the following requirements:

Limitations

  • Both volumes must use the same StorageClass.
  • Both volumes must use the same volume mode. By default, GKE sets the VolumeMode to ext4.

Using volume cloning

To provision a volume clone, you add a reference to an existing PersistentVolumeClaim in the same namespace to the dataSource field of a new PersistentVolumeClaim. The following exercise shows you how to provision a source volume with data, create a volume clone, and consume the clone.

Create a source volume

To create a source volume, follow the instructions in Using the Compute Engine persistent disk CSI Driver for Linux clusters to create a StorageClass, a PersistentVolumeClaim, and a Pod to consume the new volume. You'll use the PersistentVolumeClaim that you create as the source for the volume clone.

Add a test file to the source volume

Add a test file to the source volume. You can look for this test file in the volume clone to verify that cloning was successful.

  1. Create a test file in a Pod:

    kubectl exec POD_NAME \
        -- sh -c 'echo "Hello World!" > /var/lib/www/html/hello.txt'
    

    Replace POD_NAME with the name of a Pod that consumes the source volume. For example, if you followed the instructions in Using the Compute Engine persistent disk CSI Driver for Linux clusters, replace POD_NAME with web-server.

  2. Verify that the file exists:

    kubectl exec POD_NAME \
        -- sh -c 'cat /var/lib/www/html/hello.txt'
    

    The output is similar to the following:

    Hello World!
    

Clone the source volume

  1. Save the following manifest as podpvc-clone.yaml:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: podpvc-clone
    spec:
      dataSource:
        name: PVC_NAME
        kind: PersistentVolumeClaim
      accessModes:
      - ReadWriteOnce
      storageClassName: STORAGE_CLASS_NAME
      resources:
        requests:
          storage: STORAGE
    

    Replace the following:

    • PVC_NAME: the name of the source PersistentVolumeClaim that you created in Create a source volume.
    • STORAGE_CLASS_NAME: the name of the StorageClass to use, which must be the same as the StorageClass of the source PersistentVolumeClaim.
    • STORAGE: the amount of storage to request, which must be at least the size of the source PersistentVolumeClaim.
  2. Apply the manifest:

    kubectl apply -f podpvc-clone.yaml
    

Create a Pod that consumes the cloned volume

The following example creates a Pod that consumes the volume clone that you created.

  1. Save the following manifest as web-server-clone.yaml:

    apiVersion: v1
    kind: Pod
    metadata:
      name: web-server-clone
    spec:
      containers:
       - name: web-server-clone
         image: nginx
         volumeMounts:
           - mountPath: /var/lib/www/html
             name: mypvc
      volumes:
       - name: mypvc
         persistentVolumeClaim:
           claimName: podpvc-clone
           readOnly: false
    
  2. Apply the manifest:

    kubectl apply -f web-server-clone.yaml
    
  3. Verify that the test file exists:

    kubectl exec web-server-clone \
        -- sh -c 'cat /var/lib/www/html/hello.txt'
    

    The output is similar to the following:

    Hello World!
    

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. Delete the Pod objects:

    kubectl delete pod POD_NAME web-server-clone
    
  2. Delete the PersistentVolumeClaim objects:

    kubectl delete pvc podpvc podpvc-clone