6

我想将 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.ymlnfs-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"}
4

2 回答 2

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

我希望它会有所帮助。

于 2017-11-20T04:40:40.727 回答
2

使用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: 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
于 2018-11-30T13:30:53.450 回答