k8s存储卷与数据持久化(11)
时间:2020-09-23 15:52 来源:未知 作者:liangzh 点击:次
Bound :己经绑定至某 PVC
Released :绑定的 PVC 已经被删除,但资源尚未被集群回收
Failed :因自动回收资源失败而处于的故障状态
移动或删除nfs目录会导致pv状态失败
[root@zfb-jyhpt-bgcssj1 UCMSServer]# mv webapps webapps-bak
[root@zfb-jyhpt-bgcssj1 UCMSServer]# ls
repo templates uploadfile webapps-bak
[root@zfb-jyhpt-bgcssj1 UCMSServer]# mkdir webapps
[root@zfb-jyhpt-bgcssj1 UCMSServer]# cd webapps
[root@zfb-jyhpt-bgcssj1 webapps]# ls
[root@zfb-jyhpt-bgcssj1 webapps]# vim index.html
[root@zfb-jyhpt-bgcssj1 webapps]# ls
index.html
5.2>创建PVC
PersistentVolumeClaim 是存储卷类型的资源,它通过申请占用某个 PersistentVolume
创建,它与 PV 的关系,用户无须关心其底层实现细节。申请时,用户只需要指定
目标空间的大小、访问模式、 PV 标签选择器和 StorageClass 等相关信息即可
PVC Spec字段的可嵌套字段具体如下。
accessMode:当前 PVC 的访问模式 ,其可用模式与 PV 相同
resources:当前 PVC 存储卷需要占用的资源量最小值;目前, PVC 资源、限定仅指其空间大小
selector :绑定时对 PV 应用的标签选择器( matchLabels 或匹配条件表达式( matchEx-pressions )用于挑选要绑定 PV ;
如果同时指定了两种挑选机制 则必须同时满足两种选择机制的 PV 才能被选出
storageClassName :所依赖的存储类的名称
volumeMode 卷模型,用于指定此卷可被用作文件系统还是裸格式的块设备 默认为Filesystem
volumeName :用于直接指定要绑定的 PV 卷名
下面的配置清单(pvc-nfs-000l.yaml文件) 定义了一个 PVC 资源示例,其选择 PV 的挑
选机制是使用了标签选择器,适配的标签是 release: stable ,存储类为 slow ,这会关联到前
建的 PV 资源 pv-nfs-0001 :
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs-0001
labels:
release: "stable"
spec:
capacity:
volumeMode: Filesystem
accessModes:
- ReadWriteMany #可被多个节点同时读写挂载
resources:
requests:
storage: 5Gi #这里最好和pv大小一致,因为即使设置其它值,最后也是实际pv的大小
storageClassName: slow
selector:
matchLabels:
release: "stable"使用资源创建命令完成资源创建,而后即可查看其绑定 PV 资源的相关信息
[root@zfb-jyhpt-bgcsjyh1 k8s]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-nfs-0001 Bound pv-nfs-0001 10Gi RWX slow 93s
创建好 PVC 资源之后, 即可在 Pod 资源 中通过 persisten VolumeClain 存储卷引用它,
而后挂载于容器中进行数据持久化 需要注意是, PV 是集群级别的资源, PVC 隶属
于名称空间,因 PVC 在绑定目标 PV 时不受名称空间的限制,但pod 引用 PVC时,
只能是属于同一名称空间中的资源
删除pvc测试回收策略
[root@zfb-jyhpt-bgcsjyh1 k8s]# kubectl delete -f pvc-nfs-0001.yaml
persistentvolumeclaim "pvc-nfs-0001" deleted
[root@zfb-jyhpt-bgcsjyh1 k8s]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-nfs-0001 10Gi RWX Recycle Failed default/pvc-nfs-0001 slow 51s
5.3>Pod 中使用 PVC
Pod 资源中调用 PVC 资源,只需要在定义 volumes 时使用 persistentVolumeClaims (责任编辑:liangzh) |