PV和PVC的概述
PV 和 PVC是kubernetes集群中两个新的API资源,
PV和PVC是一对一的映射关系,PVC会根据storageClassName(存储类的名称)、accessModes (访问的模式),存储容量大小判断的时候,首要条件就是PV声明容量大于等于PVC声明的容量,然后选取最接近PVC容量的PV(存储容量越小,优先级越大)
PV 是集群中的存储资源(可以使用NFS、ISCSI等技术来提供)
PVC是用户对存储资源的请求,请求符合条件的PV与之进行绑定,所以PVC是PV 的请求方式
PV的创建模板
apiVersion: v1
kind: PersistentVolume //定义资源类型为PV or PVC
metadata:
name: 名称
spec:
capacity:
storage: 定义大小(如1Gi)
accessModes:
- ReadWriteOnce (设置访问模式)
persistentVolumeReclaimPolicy: Recycle (设置回收策略)
storageClassName: nfs (定义PV的类别)
nfs:
path: /nfs3 (挂载的目录)
server: 192.168.1.100 (NFS服务器的IP地址)
参数详解
capacity
定义PV卷的大小,结合storage来进行使用
accessModes
定义访问的类型
访问类型可以分为如下三类:
1.ReadWriteOnce
(RWO)
可以被一个节点以读写方式保存
2.ReadOnlyMany
(ROX)
可以被多个节点以只读方式保存
3.ReadWriteMany
(RWX)
可以被多个节点以读写方式保存
persistentVolumeReclaimPolicy
定义回收策略
回收策略可以分为如下几类:
1.Retain
(保留 手动回收)
PVC删除,PV仍然存在,需手动删除
2.Delete
(删除)
删除相关存储资源,例如AWS EBS,GCE PD
3.Recycle
(自动回收)
PVC删除,PV自动进行资源回收
注:仅NFS和HostPath支持回收,AWS EBS,GCE PD,Azure Disk和Cinder卷支持删除
storageClassName
定义PV的类别,一共有两种类别:nfs slow
PV卷的状态
Available
尚未绑定到声明的空闲资源上
Bound
已绑定
Released
pvc以被删除,集群尚未回收该资源
Failed
自动回收失败
PV和PVC的应用
测试环境
主机 | IP地址 |
---|---|
master | 192.168.1.100 |
node1 | 192.168.1.200 |
node2 | 192.168.1.250 |
部署NFS服务器(master节点)
所有节点安装如下软件包
[root@master ~]# yum install -y nfs-utils rpcbind
master部署NFS服务器
[root@master ~]# mkdir -p /nfs{1,2,3} //创建三个目录
[root@master ~]# chmod 777 /nfs* //赋予权限
[root@master ~]# chown nfsnobody /nfs* //修改所属组
[root@master ~]# vi /etc/exports
/nfs1 *(rw,no_root_squash,no_all_squash,sync)
/nfs2 *(rw,no_root_squash,no_all_squash,sync)
/nfs3 *(rw,no_root_squash,no_all_squash,sync)
[root@master ~]# exportfs -r
[root@master ~]# systemctl start rpcbind
[root@master ~]# systemctl enable rpcbind
[root@master ~]# systemctl start nfs
[root@master ~]# systemctl enable nfs
node1 和 node2 客户端验证
[root@node1 ~]# showmount -e 192.168.1.100
Export list for 192.168.1.100:
/nfs3 *
/nfs2 *
/nfs1 *
分别向三个目录分别写入一个文件
[root@master ~]# echo "this is nfs1" >> /nfs1/index.html
[root@master ~]# echo "this is nfs2" >> /nfs2/index.html
[root@master ~]# echo "this is nfs3" >> /nfs3/index.html
创建PV
以下操作全部都在master节点进行
[root@master ~]# vi pv.yaml //创建三个PV来匹配三个Pod
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfs1 //pv1对应nfs1目录
server: 192.168.1.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfs2 //pv2对应nfs2目录
server: 192.168.1.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfs3 //pv3对应nfs3目录
server: 192.168.1.100
[root@master ~]# kubectl apply -f pv.yaml
查看创建效果
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Recycle Available nfs 4m2s
pv2 1Gi RWO Recycle Available nfs 4m1s
pv3 3Gi RWO Recycle Available nfs 4m1s
StatefulSet创建并使用PVC
StatefulSet 需要 headless 服务
(svc无IP地址无端口号)
来负责 Pod 的网络标识,因此需要负责创建此服务。
StatefulSet Pod的运行机制,必须得等前面一个Pod运行成功后才会运行后面的Pod
[root@master ~]# vi nfs_pv.yaml
apiVersion: v1
kind: Service //定义资源service
metadata:
name: svc-1
spec:
selector:
app: nginx
clusterIP: None //设置None代表无头服务,没有IP地址以及端口号
ports:
- port: 80
---
apiVersion: apps/v1
kind: StatefulSet //设置资源类型为StatefulSet
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: nginx
serviceName: "nginx" //引用无头服务 必须存在
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: wangyanglinux/myapp:v1
ports:
- containerPort: 80
volumeMounts: //设置容器里面的挂载目录
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates: //设置引用PVC的条件
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "nfs"
resources:
requests:
storage: 2Gi //最低要求大于等于2G
[root@master ~]# kubectl apply -f nfs_pv.yaml
查看运行效果
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Recycle Bound default/www-web-0 nfs 16m
pv2 1Gi RWO Recycle Available nfs 16m
pv3 3Gi RWO Recycle Bound default/www-web-1 nfs 16m
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pv1 2Gi RWO nfs 3m10s
www-web-1 Bound pv3 3Gi RWO nfs 2m46s
访问测试
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-0 1/1 Running 0 3m38s 10.244.1.2 node1 <none> <none>
web-1 1/1 Running 0 3m14s 10.244.2.2 node2 <none> <none>
[root@master ~]# curl 10.244.1.2
this is nfs1
[root@master ~]# curl 10.244.2.2
this is nfs3
从上实验可以得出,运行的Pod分别绑定到PV1以及PV3上,因为PV2不符合要求
删除资源并回收PV
删除Pod
[root@master ~]# kubectl delete -f nfs_pv.yaml
service "svc-1" deleted
statefulset.apps "web" deleted
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pv1 2Gi RWO nfs 8m7s
www-web-1 Bound pv3 3Gi RWO nfs 7m43s
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Recycle Bound default/www-web-0 nfs 21m
pv2 1Gi RWO Recycle Available nfs 21m
pv3 3Gi RWO Recycle Bound default/www-web-1 nfs 21m
即是删除了Pod,PV和PVC依旧是会存在的
删除PVC和回收PV
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pv1 2Gi RWO nfs 9m41s
www-web-1 Bound pv3 3Gi RWO nfs 9m17s
[root@master ~]# kubectl delete pvc www-web-0 www-web-1
persistentvolumeclaim "www-web-0" deleted
persistentvolumeclaim "www-web-1" deleted
因为我们设置回收策略是Recycle (自动回收)
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Recycle Available nfs 23m
pv2 1Gi RWO Recycle Available nfs 23m
pv3 3Gi RWO Recycle Available nfs 23m
成功回收
手动回收操作方法如下
创建时PV设置参数值为这个:
persistentVolumeReclaimPolicy:Retain
查看PV的状态
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Retain Bound default/www-web-0 nfs 107s
回收状态属于Retain
删除PVC
先删除Pod
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pv1 2Gi RWO nfs 81s
[root@master ~]# kubectl delete pvc www-web-0
persistentvolumeclaim "www-web-0" deleted
查看PV回收状态
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Retain Released default/www-web-0 nfs 4m12s
状态为Released 资源未被回收
手动回收操作
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Retain Released default/www-web-0 nfs 5m31s
[root@master ~]# kubectl edit pv pv1 //编辑pv1
删除如下信息即可
67 claimRef:
68 apiVersion: v1
69 kind: PersistentVolumeClaim
70 name: www-web-0
71 namespace: default
72 resourceVersion: "14474"
73 uid: 4fad4550-bcc6-4184-9989-003a0c5fbd63
删除完成以后再次查看状态
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 2Gi RWO Retain Available nfs 7m48s
状态为Available 处于闲置状态,资源回收成功