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

时间:2017-11-19 15:33:43

标签: kubernetes google-kubernetes-engine persistent-volumes persistent-volume-claims gce-persistent-disk

我想将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 Container Engine上创建的节点磁盘上的一个卷。 那么,我错过了什么吗?

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

2 个答案:

答案 0 :(得分:12)

我找到了解决方案。

低于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

我希望它会有所帮助。

答案 1 :(得分:0)

使用 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和存储类型(类型:pd-ssd ),则可以指定它保留存储( reclaimPolicy:保留)。

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