k8s-持久卷pv和pvc使用
1. k8s-持久卷概述
- PersistentVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理
- PersistentVolumeClaim(PVC):让用户不需要关心具体的Volume实现细节
2. pv与pvc使用流程
- 官网参考地址: 支持持久卷的存储插件:https://kubernetes.io/docs/concepts/storage/persistent-volumes/
2.1 示例代码
-
数据卷定义pv
apiVersion: v1 kind: PersistentVolume # 创建pv metadata: name: my-pv # 创建名称 spec: capacity: storage: 5Gi # pv容量5G磁盘内容 accessModes: - ReadWriteMany nfs: path: /ifs/kubernetes server: 10.100.24.85
-
卷需求模板pvc
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi
-
容器pod应用pvc卷
apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: my-pvc
3. 案例
3.1 配置pv卷
-
创建pv的yaml文件目录
[root@k8s-master yaml]# mkdir -p pv [root@k8s-master yaml]# cd pv/
-
编写pv卷磁盘大小
[root@k8s-master pv]# vim pv.yaml [root@k8s-master pv]# cat pv.yaml apiVersion: v1 kind: PersistentVolume # 创建pv metadata: name: my-pv # 创建名称 spec: capacity: storage: 5Gi # pv容量5G磁盘内容 accessModes: - ReadWriteMany nfs: path: /ifs/kubernetes server: 10.100.24.85
-
启动配置文件
[root@k8s-master pv]# kubectl apply -f pv.yaml persistentvolume/my-pv created
-
验证服务是否启动
[root@k8s-master pv]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE my-pv 5Gi RWX Retain Available 5s
3.2 配置pvc卷
-
创建pvc的yaml文件目录
[root@k8s-master yaml]# mkdir -p pvc/ [root@k8s-master yaml]# cd pvc/
-
编写pvc配置文件
[root@k8s-master pvc]# vim pvc.yaml [root@k8s-master pvc]# cat pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi
-
启动pvc配置文件
[root@k8s-master pvc]# kubectl apply -f pvc.yaml persistentvolumeclaim/my-pvc created
-
验证pvc是否启动
[root@k8s-master pvc-pod]# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE my-pvc Bound my-pv 5Gi RWX 3s
3.3 配置容器使用pvc卷
-
创建pvc容器卷的目录
[root@k8s-master yaml]# mkdir pvc-pod [root@k8s-master yaml]# cd pvc-pod/
-
创建pvc配置文件
[root@k8s-master pvc-pod]# vim pod.yaml [root@k8s-master pvc-pod]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: my-pvc
-
启动配置文件
[root@k8s-master pvc-pod]# kubectl apply -f pod.yaml pod/my-pod created
-
验证服务是否启动
[root@k8s-master pvc-pod]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES configmap-demo-pod 1/1 Running 0 3d17h 10.244.107.209 k8s-node3 <none> <none> my-hostpath 1/1 Running 4 41h 10.244.107.211 k8s-node3 <none> <none> my-pod 1/1 Running 0 5m12s 10.244.36.86 k8s-node1 <none> <none> secret-demo-pod 1/1 Running 0 3d11h 10.244.107.210 k8s-node3 <none> <none> web-nfs-84f8d7bf8d-6mj75 1/1 Running 0 37h 10.244.107.212 k8s-node3 <none> <none> web-nfs-84f8d7bf8d-n4tpk 1/1 Running 0 37h 10.244.169.144 k8s-node2 <none> <none> web-nfs-84f8d7bf8d-qvd2z 1/1 Running 0 37h 10.244.36.84 k8s-node1 <none> <none>
-
curl请求一下
[root@k8s-master pvc-pod]# curl 10.244.36.86 <h1>hello world!</h1>