1. 程式人生 > 其它 >k8s_使用k8s部署部落格系統-PV PVC(二)

k8s_使用k8s部署部落格系統-PV PVC(二)

k8s PV PVC

PV和PVC

PV(PersistentVolume)在宣告的時候需要指定大小和續寫模式:["ReadWriteMany","ReadWriteOnce","ReadOnlyMany"],pv是叢集宣告的儲存資源

實際資源部署請求的儲存空間,稱為PVC(PersistentVolumeClaim)。 pvc宣告時也需要指定讀寫模式和大小。pvc關聯某個pv後這個pv就不能再和別的pvc關聯了。k8s會根據pvc的大小和讀寫模式在可用的PV中匹配一個最佳的pv與pvc關聯。

PV的資源回收策略

與pv關聯的pvc被刪除時候如果pv的persistentVolumeRecliamPolicy:Retain 保留卷,需要手工清理。配置為Recycle 則刪除資料。這些在pv的建立檔案中進行定義

PV、PVC定義檔案以及建立

wordpress和mysql PV的定義檔案,此處只是進行PV定義,對應的路徑可以不進行nfs配置,使用PVC部署pod之前需要將對應的路徑配置完成nfs否則pod部署會報錯。
#PV的定義檔案
[root@k8s-01 ~]# cat pv_mysql_wordpress.yml apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv spec: capacity: storage: 3Gi accessModes: ["ReadWriteOnce"] persistentVolumeReclaimPolicy: Recycle nfs: path:
/kube/mysql-db server: k8s-01 --- apiVersion: v1 kind: PersistentVolume metadata: name: wordpress-pv spec: capacity: storage: 1Gi accessModes: ["ReadWriteOnce","ReadWriteMany"] persistentVolumeReclaimPolicy: Recycle nfs: path: /kube/wordpress server: k8s
-01

#建立PV和查詢
[root@k8s-01 ~]# kubectl create -f pv_mysql_wordpress.yml

#查詢PV

[root@k8s-01 ~]# kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
mysql-pv 3Gi RWO Recycle Bound default/mysql-pv-claim 50d Filesystem
wordpress-pv 1Gi RWO,RWX Recycle Bound default/wordpress-pv-claim 50d Filesystem

PVC定義檔案以及建立操作

#mysql和wordpress pvc定義檔案
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: mysql-pv-claim
spec:
    accessModes: ["ReadWriteOnce"]
    resources:
        requests:
            storage: 2Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: wordpress-pv-claim
spec:
    accessModes: ["ReadWriteOnce","ReadWriteMany"]
    resources:
        requests:
            storage: 1Gi

#建立PVC 
kubectl create -f pvc_mysql_wd.yml

#查詢建立的PVC,這裡能看到PVC根據大小和讀寫策略自動匹配到合適的PV
[root@k8s-01 ~]# kubectl get pvc -o wide
NAME                 STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
mysql-pv-claim       Bound    mysql-pv       3Gi        RWO                           50d   Filesystem
wordpress-pv-claim   Bound    wordpress-pv   1Gi        RWO,RWX                       50d   Filesystem