Search code examples
google-kubernetes-enginegke-networking

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


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?


Solution

  • 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:
        ...