1. 简介2. 创建PV池2.1 创建PV2.2 运行2.2 查看3. PVC创建与绑定3.1 创建PVC3.1.1 创建文件3.1.2 写一份申请书3.1.3 删除3.1.4 再次创建申请书3.2 创建Pod绑定PVC3.2.1 创建文件3.2.2 运行文件3.2.3 查看信息3.2.4 测试
1. 简介
PV:持久卷(Persistence Volume),将应用需要持久化的数据保存到指定位置
PVC:持久卷申明(Persistence Volume Claim),申明需要使用的持久卷规格
2. 创建PV池
# nfs主节点 mkdir -p /nfs/data/01 mkdir -p /nfs/data/02 mkdir -p /nfs/data/03
2.1 创建PV
创建文件
pv.yaml
文件,输入如下内容:apiVersion: v1 kind: PersistentVolume metadata: name: pv01-10m spec: capacity: storage: 10M accessModes: - ReadWriteMany storageClassName: nfs nfs: path: /nfs/data/01 server: 192.168.92.138 --- apiVersion: v1 kind: PersistentVolume metadata: name: pv02-1gi spec: capacity: storage: 1Gi accessModes: - ReadWriteMany storageClassName: nfs nfs: path: /nfs/data/02 server: 192.168.92.138 --- apiVersion: v1 kind: PersistentVolume metadata: name: pv03-3gi spec: capacity: storage: 3Gi accessModes: - ReadWriteMany storageClassName: nfs nfs: path: /nfs/data/03 server: 192.168.92.138
2.2 运行
root@docker01:/home# kubectl apply -f pv.yaml
2.2 查看
root@docker01:/home# kubectl get persistentvolume NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv01-10m 10M RWX Retain Available nfs 2m41s pv02-1gi 1Gi RWX Retain Available nfs 79s pv03-3gi 3Gi RWX Retain Available nfs 79s
3. PVC创建与绑定
3.1 创建PVC
3.1.1 创建文件
root@docker01:/home# vim pvc.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: nginx-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 200Mi storageClassName: nfs
3.1.2 写一份申请书
root@docker01:/home# kubectl apply -f pvc.yaml
3.1.3 删除
# 1. 删除 root@docker01:/home# kubectl delete -f pvc.yaml # 2. 查看 root@docker01:/home# kubectl get pv
查看,在释放资源
3.1.4 再次创建申请书
root@docker01:/home# kubectl apply -f pvc.yaml
查看,由于还在释放,所以绑定了3Gi
3.2 创建Pod绑定PVC
3.2.1 创建文件
deploy-pvc.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-deploy-pvc name: nginx-deploy-pvc spec: replicas: 2 selector: matchLabels: app: nginx-deploy-pvc template: metadata: labels: app: nginx-deploy-pvc spec: containers: - image: nginx name: nginx volumeMounts: - name: html mountPath: /usr/share/nginx/html volumes: - name: html persistentVolumeClaim: claimName: nginx-pvc
claimName
与申请书空间一样
3.2.2 运行文件
root@docker01:/home# kubectl apply -f deploy-pvc.yaml deployment.apps/nginx-deploy-pvc created
3.2.3 查看信息
root@docker01:/home# kubectl get pod root@docker01:/home# kubectl get pvc,pv
3.2.4 测试
主机写入内容,Pod查看内容同步
root@docker01:/home# cd /nfs/data/ root@docker01:/nfs/data# ls 01 02 03 a nginx-pv root@docker01:/nfs/data# cd 03/ root@docker01:/nfs/data/03# ls root@docker01:/nfs/data/03# echo "Hello,PVC and PV!" > index.html root@docker01:/nfs/data/03#
Pod中查看,内容同步!
目前这个是PV池静态供应,后续补充PV池动态供应
- 这种方式适合挂载目录、文件,挂载配置文件推荐是使用ConfigMap