Search code examples
kubernetespersistent-volumeskubernetes-pvcpersistent-volume-claims

How to move a PersistentVolume from one zone to another zone?


I have a k8s cluster with machines on two different zones. Originally, I have a StatefulSet with PersistentVolumeClaim, and the Pod & PersistentVolume are on zone #1. Now I want the pod to run one zone #2, so I have to move the PersistentVolume to the zone #2 as well.

How can I do so? I know in the different nodes of the same zone things are quite simple - the pod will auto bring the PersistentVolume with it. But I do not know what to do across zones.

Thanks very much for any suggestions!


Solution

  • This is example on GKE, however other cloud providers should have similar options but with different zone names.

    You can do it by Regional Persistent Disks with specified StorageClass. Depends on your needs you can use:

    • Dynamic Provisioning

    To enable dynamic provisioning of regional persistent disks, create a StorageClass with the replication-type parameter, and specify zone constraints in allowedTopologies.

    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: regionalpd-storageclass
    provisioner: pd.csi.storage.gke.io
    parameters:
      type: pd-standard
      replication-type: regional-pd
    volumeBindingMode: WaitForFirstConsumer
    allowedTopologies:
    - matchLabelExpressions:
      - key: topology.gke.io/zone
        values:
        - europe-west1-b
        - europe-west1-c
    

    Above example describes a StorageClass named regionalpd-storageclass that uses standard persistent disks and that replicates data to the europe-west1-b and europe-west1-c zones

    • Manual Provisioning
    gcloud compute disks create \
     gce-disk-1 \
       --size 500Gi \
       --region europe-west1 \
       --replica-zones europe-west1-b,europe-west1-c
    

    If you would like to create Persistent Volume in specific zone you can specify it in storageClass like in this example:

    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: PV-us-central-1a
    provisioner: kubernetes.io/gce-pd
    parameters:
      type: pd-standard
      zone: us-central1-a
    

    To use specific StorageClass in PersistentVolume or PersistentVolumeClaim you have to use storageClassName.

    Below example with volumeClaimTemplates and storageClassName

      `volumeClaimTemplates`:
      - metadata:
          name: www
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: "my-storage-class"
          resources:
            requests:
              storage: 1Gi
    

    Regarding moving PV to different zone, you can use Volume Snapshots or use 3rd party software like Velero. You can find many migrating PV tutorials online using Volume Snapshots like Migrating Kubernetes PersistentVolumes across Regions and AZs on AWS.

    Additional documentation