由于kubernetes支持的存储系统有很多,为了能够屏蔽底层存储实现的细节,方便普通用户使用,kubernetes引入了PV和PVC两种资源对象
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
nfs:
path: /root/data/nfs/pv1
server: 192.168.23.31
capacity:
storage: 2Gi # 存储空间的设置
accessModes: # 访问模式
- ReadWriteMany
storageClassName: fast # 自定义的存储类别
persistentVolumeReclaimPolicy: Retain # 回收策略
访问模式(accessModes):用来描述用户应用对存储资源的访问权限,底层不同的存储类型可能支持的访问模式不同
回收策略(persistentVolumeReclaimPolicy):当PV不再被使用之后,对其的处理方式,底层不同的存储类型可能支持的回收策略不同
存储类别(storageClassName):PV可以通过自定义storageClassName参数指定一个存储类别
创建共享目录
[root@nfs ~]# mkdir -p /root/data/nfs/{pv1,pv2,pv3}
修改/etc/exports文件
[root@nfs ~]# cat /etc/exports
/root/data/nfs/pv1 192.168.23.0/24(rw,no_root_squash)
/root/data/nfs/pv2 192.168.23.0/24(rw,no_root_squash)
/root/data/nfs/pv3 192.168.23.0/24(rw,no_root_squash)
[root@nfs ~]#
重启nfs
[root@nfs ~]# systemctl restart nfs
新建pv.yaml文件,内容如下。然后创建PV
[root@k8s-master ~]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
nfs:
path: /root/data/nfs/pv1
server: 192.168.23.31
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
nfs:
path: /root/data/nfs/pv2
server: 192.168.23.31
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
nfs:
path: /root/data/nfs/pv3
server: 192.168.23.31
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pv.yaml
persistentvolume/pv1 created
persistentvolume/pv2 created
persistentvolume/pv3 created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Available 41s Filesystem
pv2 2Gi RWX Retain Available 41s Filesystem
pv3 3Gi RWX Retain Available 41s Filesystem
[root@k8s-master ~]#
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
namespace: dev
spec:
accessModes: # 访客模式
- ReadWriteMany
selector: # 采用标签对PV进行选择
matchLabels:
pv: pv1
storageClassName: fast # 存储类别
resources: # 请求空间
requests:
storage: 1Gi
访客模式(accessModes):用于描述用户应用对存储资源的访问权限。需和PV的accessModes一致才能绑定到PV
存储类别(storageClassName):PVC在定义时可以设定需要的后端存储的类别。需和PV的storageClassName一致才能绑定到PV
资源请求(resources):描述对存储资源的请求。PVC请求的资源必须等于小于PV的资源,才能绑定到PV
新建pvc.yaml文件,内容如下。然后创建PVC
[root@k8s-master ~]# cat pvc.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: 2Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc3
namespace: dev
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pvc.yaml
persistentvolumeclaim/pvc1 created
persistentvolumeclaim/pvc2 created
persistentvolumeclaim/pvc3 created
[root@k8s-master ~]#
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pvc -n dev -o wide
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
pvc1 Bound pv1 1Gi RWX 56s Filesystem
pvc2 Bound pv2 2Gi RWX 56s Filesystem
pvc3 Pending 55s Filesystem
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Bound dev/pvc1 14m Filesystem
pv2 2Gi RWX Retain Bound dev/pvc2 14m Filesystem
pv3 3Gi RWX Retain Available 14m Filesystem
[root@k8s-master ~]#
新建pod.yaml文件,内容如下。然后创建Pod
[root@k8s-master ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: dev
spec:
containers:
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "while true; do echo pod1 >> /root/logs/pod.log; sleep 10; done;"]
volumeMounts:
- name: volume
mountPath: /root/logs
volumes:
- name: volume
persistentVolumeClaim:
claimName: pvc1
readOnly: false
---
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: dev
spec:
containers:
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "while true; do echo pod2 >> /root/logs/pod.log; sleep 10; done;"]
volumeMounts:
- name: volume
mountPath: /root/logs
volumes:
- name: volume
persistentVolumeClaim:
claimName: pvc2
readOnly: false
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod.yaml
pod/pod1 created
pod/pod2 created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pvc -n dev -o wide
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
pvc1 Bound pv1 1Gi RWX 9m31s Filesystem
pvc2 Bound pv2 2Gi RWX 9m31s Filesystem
pvc3 Pending 9m30s Filesystem
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 1Gi RWX Retain Bound dev/pvc1 23m Filesystem
pv2 2Gi RWX Retain Bound dev/pvc2 23m Filesystem
pv3 3Gi RWX Retain Available 23m Filesystem
[root@k8s-master ~]#
[root@docker ~]# ll data/nfs/pv1/pod.log
-rw-r--r--. 1 root root 80 5月 23 18:17 data/nfs/pv1/pod.log
[root@docker ~]#
[root@docker ~]# ll data/nfs/pv2/pod.log
-rw-r--r--. 1 root root 70 5月 23 18:17 data/nfs/pv2/pod.log
[root@docker ~]#
[root@docker ~]# ll data/nfs/pv3
总用量 0
[root@docker ~]#
PV和PVC之间的相互作用遵循如下的生命周期: