TL; DR: Below are steps to mount an existing Google Persistent Disk to a pod in a GKE cluster in the same region.
Most of the time I’d prefer to run stateless pods in Kubernetes clusters – they come, they go, as if nothing happened. When there are things to be persisted, a Persistent Volume will be handy. But what if I ran some app in a VM, with its data in a Persistent Disk, and now I want to migrate the app into GKE with its existing data? To my surprise, the solution is already there and up for grabs.
First, imagine the Persistent Disk is called my-pd-01
, I can create a Persistent Volume resource as following, referring to the existing PD.
apiVersion: v1 kind: PersistentVolume metadata: labels: name: my-pd-01 name: my-pd-01 spec: accessModes: - ReadWriteOnce capacity: storage: 50Gi claimRef: name: my-pd-01 namespace: my-namespace csi: driver: pd.csi.storage.gke.io # the format is projects/<project name>/zones/<zone of pd>/disks/<pd name> volumeHandle: projects/my-gcp-project-name/zones/australia-southeast1-c/disks/my-pd-01 persistentVolumeReclaimPolicy: Retain storageClassName: ssd volumeMode: Filesystem
Then this PV can be claimed by the PVC mentioned above
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pd-01 namespace: my-namespace spec: accessModes: - ReadWriteOnce resources: requests: storage: 50Gi storageClassName: ssd volumeMode: Filesystem volumeName: my-pd-01
Finally the PVC can be mounted as a disk in a pod
... spec: containers: name: my-test image: ubuntu volumeMounts: - mountPath: /var/lib/test name: data-mount ... volumes: - name: data-mount persistentVolumeClaim: claimName: my-pd-01
🙂