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