In GKE, can we specify the zone or region of a persistent volume when we create it through a PVC object

3k views Asked by At

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?

3

There are 3 answers

0
Bryan Larsen On BEST ANSWER

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:
    ...
0
M4C4R On

The following works for me in GKE:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: standard-euw2c
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard
volumeBindingMode: Immediate
allowedTopologies:
- matchLabelExpressions:
  - key: topology.kubernetes.io/zone
    values:
    - europe-west2-c

Ref: https://kubernetes.io/docs/concepts/storage/storage-classes/#allowed-topologies

The key part above is allowedTopologies, where I permitted a single zone.

Example PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: hi-europe-west2-c
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard-euw2c
2
Mr.KoopaKiller On

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