6

I want my persistent volume to reside in a specific zone, say us-central1-a, but I want to deploy it through a PVC, not by creating an object of PV directly. Is this possible in GKE?

  • 1
    Are your node in the same zone? By this [doc](https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes#pd-zones) k8s will choose a random zone to create the disk. What is the requirement to choose the zone? Maybe there are another way to achieve what you want. – Mr.KoopaKiller Oct 16 '20 at 08:20
  • @KoopaKiller I want to create a PV in a regional cluster, so there are many zones present, so when I create a PVC, random zones are assigned to their respective PVs. So I want to specify the zone while creating the PVC. – Ruchidnya Kadam Oct 16 '20 at 08:49

2 Answers2

5

It doesn't appear that you can specify a zone for a PV or PVC, but you can set volumeBindingMode: WaitForFirstConsumer on the StorageClass to ensure that the PV is in the same zone as the pod that uses it. nodeSelector on the pod can then be used to choose the zone.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd
parameters:
  type: pd-ssd
provisioner: kubernetes.io/gce-pd
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: db
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 200Gi
  storageClassName: ssd

pod:

kind: Pod
apiVersion: v1
metadata:
  name: foo
spec:
  nodeSelector:
    failure-domain.beta.kubernetes.io/zone: us-central1-a
  containers:
    ...
Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
3

You should use Regional persistent disks

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

For example, the following manifest 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:

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

Create a PersistentVolumeClaim object, and use the storageClassName field to refer to the StorageClass you created. For example, the following manifest creates a PersistentVolumeClaim named regional-pvc and references the regionalpd-storageclass

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: regional-pvc
  namespace: testns
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 200Gi
  storageClassName: regionalpd-storageclass

The following manifest is an example Pod using the previously created PersistentVolumeClaim:

kind: Pod
apiVersion: v1
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: regional-pvc
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

You can follow this guide that show you how you can do it

Mr.KoopaKiller
  • 3,665
  • 10
  • 21
  • @RuchidnyaKadam, if my answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Mr.KoopaKiller Nov 03 '20 at 08:26
  • 1
    Regional disks are more expensive than zonal disks. Also, zonal disks have affinity, so it pins the pod to the zone, which may be a good or bad thing depending on requirements. It'd be nice to have an answer to the original question. – Bryan Larsen Mar 30 '21 at 15:13