42、k8s-数据存储-高级存储-pv和pvc、NFS服务器
PV(Persistent Volume)是持久化卷的意思、是对底层的共享存储的一种抽象、一般情况下PV有kubernetes管理员进行创建和配置、他与底层具体的共享存储技术有关、并通过插件完成于共享存储的对接
PVC(Persistent Volume Claim)是持久卷声明的意思、是用户对于存储需求的一种声明、换句话说、PVC其实就是用户向kubernetes系统发出的一种资源需求申请
------------------------------------------------------PV--------------------------------------
pv是存储资源的抽象、下面是自资源清单:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
nfs: #存储类型、与底层真正存储对应
capacity: #存储能力、目前只支持存储空间的设置
storage: 2Gi
accessModes: #访问模式
storageClassName: #存储类别
persistentVolumeReclaimPolicy: #回收策略
PV的关键配置参数说明:
·存储类型:底层实际存储的类型、kubernetes支持多种存储类型、每种存储类型的配置都有差异
··NFS
··CIFS
··GlusterFS
·存储能力(capacity):目前只支持存储空间的设置(storage=1Gi)、不过未来可能会加入IOPS、吞吐量等指标的配置
·访问模式(accessModes):用于描述用户应用对存储资源的访问权限、访问权限包括下面几种方式:
··ReadWriteOnce(RWO):读写权限、但是只能被单个节点(PVC)挂载
··ReadOnlyMany(ROM):只读权限、可以被多个节点(PVC)挂载
··ReadWriteMany(RWM):可读可写权限、可以被多个节点(PVC)挂载
··需要注意的是:底层的存储类型不同可能所支持的访问模式就不同
·回收策略(persistentVolumeReclaimPolicy):当pv不再被使用了之后、对其的处理方式、目前支持三种策略:
·Retain(保留):保留数据、需要管理员手动清理数据
·recycle(回收):清理PV中的数据、效果相当于执行rm -rf /thevolume/*
·Delete(删除):与PV相连的后端存储完成volume的删除操作、当然这常见于云服务商的存储服务
·需要注意的是:底层不同的存储类型 可能支持的回收策略不同
·存储类别:PV可以通过storageClassName参数指定一个存储类别
·具有特定类别的PV只能与请求了该类别的PVC进行绑定
·未设定类别的PV则只能与不请求任何类别的PVC进行绑定
·状态(status):一个PV的生命周期中,可能会处于4种不同的阶段
·Availabel(可用的):表示可用状态、还未被任何PVC绑定
·Bound(已绑定):表示PV已经被PVC绑定
·Release(已释放):表示PVC被删除、但是资源还未被集群重新声明
·Faild(失败):表示该PV的自动回收失败
实验:
使用NFS作为存储、来演示PV的使用、创建3个PV,对应NFS种的3个暴露的路径
#先准备NFS环境
1、在NFS服务器上先创建三个目录:
·mkdir /root/data/{pv1,pv2,pv3} -pv
2、修改配置文件使目录暴露出去:
·vim /etc/exports
------------------------------------------------------
/root/data/pv1 192.168.177.0/24(rw,no_root_squash)
/root/data/pv2 192.168.177.0/24(rw,no_root_squash)
/root/data/pv3 192.168.177.0/24(rw,no_root_squash)
#192.168.177.0/24(rw,no_root_squash) 表示暴露给192.168.177.* 这个网段的主机 可读可写
---------------------------------------------------------------------------------------------------
3、重启NFS服务:systemctl restart nfs
=======================================================================
#在master节点准备创建PV
4、vim pv-exports-nfs.yaml
-----------------------------------------------------------------------------------------
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity: #存储能力
storage: 1Gi #存储空间为1G
accessModes: #访问方式
- ReadWriteMany #可读可写、可多挂载
persistentVolumeReclaimPolicy: Retain #回收策略:保留
nfs:
path: /root/data/pv1 #nfs暴露的某个目录路径
server: 192.168.177.180 #nfs服务器
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /root/data/pv2
server: 192.168.177.180
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /root/data/pv3
server: 192.168.177.180
--------------------------------------------------------------------------------------
5、创建PV:kubectl create -f pv-exports-nfs.yaml
6、查看PV: kubectl get pv -owide
-----------------------------------------------------------------------------------------------------------------
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Available 74s Filesystem
pv2 2Gi RWX Retain Available 74s Filesystem
pv3 3Gi RWX Retain Available 43s Filesystem
-------------------------------------------------------------------------------------------------------------------
下面介绍接着PVC后让PVC绑定PV
-------------------------------------PVC-------------------------------------------
PVC是资源的申请、用来声明对存储空间、访问模式、存储类别需求信息
以下是PVC的资源清单:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
namespace: dev
spec:
accessModes: #访问模式
selector: #采用标签对PV进行选择
storageClassName: #存储类别
resources: #请求空间
requests:
storage: 5Gi
PVC的关键配置参数说明:
·访问模式(accessModes): 用于描述用户对存储资源的访问权限
·选择条件(selector):通过labelSelector的设置、可使PVC对于系统中已存在的PV进行筛选
·存储类型(storageClassName):PVC在定义时可以设定需要的后端存储的类别、只有设置了该class的pv才能被系统选出
·资源请求(Resources):描述对存储资源的请求
实验:创建PVC、绑定pv、再使用pv
1、vim pvc-pv.yaml
----------------------------------------------------------------------
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: 3Gi
#注意这里的storage 的大小如果 pvc > pv 那pvc会绑定不到pv上、如果pvc<=pv 那么pvc就会自动申请去绑定空间足够的pv
----------------------------------------------------------------------------------------------------------------------
2、创建pvc:kubectl create -f pvc-pv.yaml
3、查看pvc:kubectl get pvc -ndev
--------------------------------------------------------------------------------
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pv1 1Gi RWX 36s
pvc2 Bound pv2 2Gi RWX 36s
pvc3 Bound pv3 3Gi RWX 36s
#这里可以看到pvc1 绑定到了pv1 状态时Bound时绑定的意思
---------------------------------------------------------------------------------
4、查看pv:kubectl get pv -owide
------------------------------------------------------------------------------------------------------------
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Bound dev/pvc1 27m Filesystem
pv2 2Gi RWX Retain Bound dev/pvc2 27m Filesystem
pv3 3Gi RWX Retain Bound dev/pvc3 27m Filesystem
#此时pv显示Bound状态 也绑定到了对应的pvc
------------------------------------------------------------------------------------------------------------------
这时pvc已经绑定了pv、此时只要通过pod来使用pv即可
------------------------------创建pod来使用pv-------------------------------------
1、
--------------------------------------------------------------------------------------------
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/ #将容器里的root目录挂载到pvc1、也就是pv1
volumes:
- name: volume
persistentVolumeClaim:
claimName: pvc1
readOnly: false
---
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: dev
spec:
containers:
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","while true;do echo pod2 >> /root/out.txt; sleep 10; done;"]
volumeMounts:
- name: volume
mountPath: /root/ #将容器里的root目录挂载到pvc2、pvc2绑定的是pv2、而pv2=/root/data/pv2 所以pod容器中的/root/下的out.txt会生成在pv2目录里
volumes:
- name: volume
persistentVolumeClaim:
claimName: pvc2
readOnly: false
#这里没有使用pvc3
-----------------------------------------------------------------------------------------------------------------------
2、创建pod:kubectl create -f pod-pv.yaml
3、查看pod:kubectl get pods -ndev -owide
--------------------------------------------------------------------------------------
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod1 1/1 Running 0 49s 10.244.1.120 node-1 <none> <none>
pod2 1/1 Running 0 49s 10.244.1.119 node-1 <none> <none>
---------------------------------------------------------------------------------------------------------------------------------
4、查看pvc:kubectl get pvc -ndev -owide
----------------------------------------------------------------------------------------------
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
pvc1 Bound pv1 1Gi RWX 26m Filesystem
pvc2 Bound pv2 2Gi RWX 26m Filesystem
pvc3 Bound pv3 3Gi RWX 26m Filesystem
------------------------------------------------------------------------------------------------
5、查看pv:kubectl get pv -owide
------------------------------------------------------------------------------------------------
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Bound dev/pvc1 52m Filesystem
pv2 2Gi RWX Retain Bound dev/pvc2 52m Filesystem
pv3 3Gi RWX Retain Bound dev/pvc3 52m Filesystem
------------------------------------------------------------------------------------------------------------------
6、去到NFS服务器上、查看/root/data/pv1 pv2里的是否有out.txt文件生成:
·tail -f /root/data/pv1/out.txt
·tail -f /root/data/pv2/out.txt
·out.txt会一直打印pv1和pv2
7、最后删除pod、pvc、此过程中注意查看pv的状态:
·kubectl delete -f pod-pv.yaml
·kubectl delete -f pvc-pv.yaml
·kubectl get pv -owide
---------------------------------------------------------------------------------------------------
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Released dev/pvc1 60m Filesystem
pv2 2Gi RWX Retain Released dev/pvc2 60m Filesystem
pv3 3Gi RWX Retain Released dev/pvc3 59m Filesystem
#此时pv的状态是released 释放
-------------------------------------------------------------------------------------------------------------------