[Kubernetes] 如何关联PVC到特定的PV

如何关联pvc到特定的pv?

我们可以使用对 pv 打 label 的方式,具体如下:

创建 pv,指定 label

[root@server PV]# cat pv-test1.yaml   
kind: PersistentVolume
apiVersion: v1
metadata:
  name: test1-pv
  namespace: kubeflow
  labels:
    pv: test1
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/test1"

 

[root@server PV]# cat pv-test2.yaml 
kind: PersistentVolume
apiVersion: v1
metadata:
  name: test2-pv
  namespace: kubeflow
  labels:
    pv: test2
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/test2"

然后创建 pvc,使用 matchLabel 来关联刚创建的 pv: test2

[root@server PV]# cat pvc2.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test2-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
  selector:
    matchLabels:
      pv: test2

测试

[root@server PV]# kubectl create -f pv-test1.yaml 
persistentvolume "test1-pv" created
[root@server PV]# kubectl create -f pv-test2.yaml  
persistentvolume "test2-pv" created
You have mail in /var/spool/mail/root
[root@server PV]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                      STORAGECLASS   REASON    AGE
test1-pv   100Mi      RWO            Retain           Available                                                       10s
test2-pv   100Mi      RWO            Retain           Available                                                       6s
[root@server PV]# kubectl create -f pvc2.yaml 
persistentvolumeclaim "test2-pvc" created
[root@server PV]# kubectl get pvc
NAME        STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
test2-pvc   Bound     test2-pv   100Mi      RWO                           5s

[root@server PV]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                      STORAGECLASS   REASON    AGE
test1-pv   100Mi      RWO            Retain           Available                                                       2m
test2-pv   100Mi      RWO            Retain           Bound       default/test2-pvc                                   2m

绑定成功

 

参考:

https://blog.51cto.com/ygqygq2/2308576

曾先森~~ CSDN认证博客专家 AI学习者 努力奋斗者
AI 学习与生活
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页