一、PVC简介
PVC是资源的申请,用来声明对存储空间、访问模式、存储类别需求信息,如下为PVC的资源文件
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
namespace: dev
spec:
accessModes: # 访问模式
selector: # 采用标签对PV选择
storageClassName: # 存储类别
resources: # 请求空间
requests:
storage: 2Gi
PVC的关键配置参数说明
- 访问模式(accessModes)
用于描述用户应用对存储资源的访问权限
- 选择条件
通过Label Selecot的设置,可使PVC对于系统中已存在的PV进行筛选
- 存储类别
PVC在定义时可以设定需要的后端存储的类别,只有设置了该class的PV才能被系统选出
- 资源请求
对存储资源的请求
二、PVC实例
编写pvc.yaml文件,申请PV,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
namespace: dev
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc2
namespace: dev
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc3
namespace: dev
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
使用如下命令创建pvc
[root@master volume]# kubectl apply -f pvc.yaml
namespace/dev created
persistentvolumeclaim/pvc1 created
persistentvolumeclaim/pvc2 created
persistentvolumeclaim/pvc3 created
[root@master volume]#
查看创建好的pvc资源,如下:
[root@master volume]# kubectl get pvc -n dev
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pv2 2Gi RWX 2m15s
pvc2 Bound pv1 2Gi RWX 2m15s
pvc3 Bound pv3 2Gi RWX 2m15s
[root@master volume]#
此时再查看一下PV,如下,已经存在绑定关系了
[root@master volume]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
auth-mysql-pv 10Gi RWO Retain Bound lbsheng/auth-mysql-pv-claim manual 8d
pv1 2Gi RWX Retain Bound dev/pvc2 31h
pv2 2Gi RWX Retain Bound dev/pvc1 31h
pv3 2Gi RWX Retain Bound dev/pvc3 31h
[root@master volume]#
在创建pod的yaml中申请pvc,编辑pod_pvc.yaml文件,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: dev
spec:
containers:
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","while true;do echo pod1 >> /root/out.txt;sleep 10;done;"]
volumeMounts:
- name: volume
mountPath: /root/
volumes:
- name: volume
persistentVolumeClaim:
claimName: pvc1
readOnly: false
使用如下命令创建资源:
[root@master volume]# kubectl apply -f pod_pvc.yaml
pod/pod1 created
[root@master volume]#
根据上面的创建的资源可以发现,pvc1使用的是pv2,而pv2挂载的是nfs中的/root/data/pv2的目录,此时查看目录如下,已经存在out.txt文件了
[root@master volume]# ls /root/data/pv2
out.txt
[root@master volume]#
使用tail命令可以发现out.txt文件中的内容
[root@master volume]# tail -f /root/data/pv2/out.txt
pod1
pod1
pod1
pod1
pod1
pod1
pod1
pod1
pod1
pod1
pod1
如下删除pod以及pvc
[root@master volume]# kubectl delete -f pod_pvc.yaml
pod "pod1" deleted
[root@master volume]# kubectl delete -f pvc.yaml
namespace "dev" deleted
persistentvolumeclaim "pvc1" deleted
persistentvolumeclaim "pvc2" deleted
persistentvolumeclaim "pvc3" deleted
[root@master volume]#
此时再查看pv可以发现此时pv的绑定关系已经释放了
[root@master volume]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
auth-mysql-pv 10Gi RWO Retain Bound lbsheng/auth-mysql-pv-claim manual 8d
pv1 2Gi RWX Retain Released dev/pvc2 31h
pv2 2Gi RWX Retain Released dev/pvc1 31h
pv3 2Gi RWX Retain Released dev/pvc3 31h
[root@master volume]#