如何使用 gcePersistentDisk 绑定持久卷声明?

how to bound a Persistent volume claim with a gcePersistentDisk?

我想将 PersistentVolumeClaim 与 gcePersistentDisk PersistentVolume 绑定。在我为获得它所做的步骤下面:

1。 gcePersistentDisk 的创建:

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

2。定义 PersistentVolume 和 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

运行kubectl apply -f pv-pvc.yml后,nfs-pvc不绑定nfs-pv。事实上,下面是我拥有的 PersistentVolume 和 PersistentVolumeClaim 的列表:

$ 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

得到的PersistentVolume是我在Google容器引擎上创建的节点的磁盘上的一个volume。 那么,我是不是漏掉了什么?

PS: 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"}

我找到了解决方案。

下面PV和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

经过这些修改后,这是有效的边界:

$ 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

希望对您有所帮助。

使用 PersistentVolumeClaim,您不需要创建 PersistentVolume 对象或 gcePersistentDisk。相反,只创建一个 PVC,Kubernetes 会自动创建一个引用后备存储的 PV 对象。

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ssd-sc  # specify the storage class created below
  resources:
    requests:
      storage: 10Gi

创建一个 StorageClass 以便它知道要使用哪个后备存储。如果删除PVC和存储类型(type:pd-ssd),您可以指定它保留存储(reclaimPolicy:Retain)。

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd-sc 
provisioner: kubernetes.io/gce-pd
reclaimPolicy: Retain # Retain storage even if we delete PVC
parameters:
  type: pd-ssd # ssd