'GKE migrate pv to a new cluster whithin the same GCloud project

I would like to move existing PVCs/PVs to a new GKE cluster in the same Google project. Can someone give me a hint how to do this?

Thanks in advance!



Solution 1:[1]

In case of same zone(s) of old and new GKEs one possible way is to create PVCs & PVs in a new GKE as a copy from old GKE with claimRef removal. Here are my unoptimized bash snippets I use to copy

SRC=src_k8s_context
DST=dst_k8s_context

# copy NS
kubectl --context $SRC get ns -o name|cut -d '/' -f 2|xargs -n1 kubectl --context $DST create ns

# copy PV
for PV in $(kubectl --context $SRC get pv -o name|cut -d '/' -f 2); do kubectl --context $SRC get pv $PV -o yaml --export|kubectl --context $DST create -f -;done

# copy PVC
tmp=$IFS;IFS=$'\n';for a in $(kubectl --context $SRC get pvc -A|tail -n +2|awk '{print "NS="$1" PVC="$2}');do eval $a; kubectl --context $SRC --namespace $NS get pvc $PVC -o yaml --export | kubectl --context $DST --namespace $NS create -f -;done; IFS=$tmp

# stop all required pods in old GKE, my case is StatefulSet's in all namespaces
for NS in $(kubectl --context $SRC get ns -o name|cut -d '/' -f 2); do kubectl --context $SRC -n $NS scale statefulset --all --replicas=0;done

# cleanup PV's in a new GKE from links to old GKE PVC
for PV in $(kubectl --context $DST get pv -o name|cut -d '/' -f 2); do kubectl --context $DST patch pv $PV  --type json   -p='[{"op": "remove", "path": "/spec/claimRef"}]';done

When Your old and new clusters are located in different regions - use Velero, as already mentioned.

Solution 2:[2]

Valero gives you tools to back up and restore your Kubernetes cluster resources and persistent volumes. You can run Velero with a cloud provider or on-premises.

Solution 3:[3]

If you are trying to use preexisting disks on compute engine on new gke cluster, have a look at this doc. It contains an example with yaml file that you might find helpful in order to achieve your migration.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Arghya Sadhu
Solution 3 dany L