Search code examples
kubernetesgoogle-kubernetes-enginepersistent-volumespersistent-volume-claimsgce-persistent-disk

how to bound a Persistent volume claim with a gcePersistentDisk?


I would like to bound PersistentVolumeClaim with a gcePersistentDisk PersistentVolume. Below the steps I did for getting that:

1. Creation of the gcePersistentDisk:

gcloud compute disks create --size=2GB --zone=us-east1-b gce-nfs-disk

2. Definition the PersistentVolume and the PersistentVolumeClaim

# pv-pvc.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: gce-nfs-disk
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
  labels:
    app: test
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

After running kubectl apply -f pv-pvc.yml, the nfs-pvc is not bound with nfs-pv. In fact, below is the list of the PersistentVolume and PersistentVolumeClaim I have:

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM             STORAGECLASS   REASON    AGE
nfs-pv                                     2Gi        RWO            Retain           Available                                              30s
pvc-16e4cdf2-cd3d-11e7-83ae-42010a8e0243   2Gi        RWO            Delete           Bound       default/nfs-pvc   standard                 26s
$ kubectl get pvc
NAME      STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nfs-pvc   Bound     pvc-16e4cdf2-cd3d-11e7-83ae-42010a8e0243   2Gi        RWO            standard       59s

The obtained PersistentVolume is a volume on the disk of the node I created on Google Container Engine. So, have I missed something?

PS: the version of kubernetes

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.3", GitCommit:"f0efb3cb883751c5ffdbe6d515f3cb4fbe7b7acd", GitTreeState:"clean", BuildDate:"2017-11-08T18:39:33Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"7+", GitVersion:"v1.7.8-gke.0", GitCommit:"a7061d4b09b53ab4099e3b5ca3e80fb172e1b018", GitTreeState:"clean", BuildDate:"2017-10-10T18:48:45Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}

Solution

  • I found the solution.

    Below the new definitions of the PV and PVC:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs-pv
      labels:
        app: test  # the label has been added to make sure the bounding is working as expected
    spec:
      capacity:
        storage: 2Gi
      accessModes:
        - ReadWriteOnce
      gcePersistentDisk:
        pdName: gce-nfs-disk
        fsType: ext4
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nfs-pvc
      labels:
        app: test
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: "" # the storageClassName has to be specified
      resources:
        requests:
          storage: 2Gi
      selector:
        matchLabels:
          app: test
    

    After these modifications, this is the bounding worked:

    $ kubectl get pvc
    NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    nfs-pvc   Bound     nfs-pv    2Gi        RWO                           8s
    $ kubectl get pv
    NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM             STORAGECLASS   REASON    AGE
    nfs-pv    2Gi        RWO            Retain           Bound     default/nfs-pvc                            22m
    

    I hope it will help.