K8S Persistent Volume (PV and PVC)


Reference article: https://www.cnblogs.com/dengbingbing/p/10399207.html
Reference video: https://ke.qq.com/user/index/index.html#/plan/cid=1709963&term_id=102815140

1. Concept

1.1.PersistentVolume(PV)

It is a section of network storage configured by the administrator in the cluster. It is a resource in the cluster, just like a node is a cluster resource. The life cycle is independent of any single pod that uses PV.

1.2.PersistentVolumeClaim(PVC)

It is a storage request made by the user, similar to a pod. Pod consumes node resources, and PVC consumes PV resources. A specific size and access mode can be requested (for example, it can be read/write once or read-only multiple times).

1.3.访问模式

RWO - ReadWriteOnce			#单个Pod挂载并且可读写
ROX - ReadOnlyMany			#可以有多个Pod以只读方式挂载
RWX - ReadWriteMany			#可以有多个Pod以读写方式挂载

1.4.PV回收策略

Retain			#人工回收,保留,默认选项
Recycle			#清除PV中的数据
Delete			#直接删除后端数据

2. PV static supply

2.1.创建PV

Create two PVs, one 5G, one 10G
Note: /data/nfs/pv0001 and /data/nfs/pv0002 need to be created in advance

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /data/nfs/pv0001
    server: 192.168.1.10

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0002
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /data/nfs/pv0002
    server: 192.168.1.10

kubectl get pv
Insert picture description here

2.2.创建PVC

According 访问模式and 容量unbinding corresponding PV, PV always ensure equal size greater than the size of the binding of PVC;
PVC and PV is one to
delete delete PVC Pod

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

kubectl get pvc
Insert picture description here

2.3.Pod使用PVC

Actually bind the /data/nfs/pv0001 directory of the NFS share to the container's /usr/share/nginx/html directory

spec:
  containers:
  - name: empty
    image: nginx
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
    volumeMounts:
      - name: data
        mountPath: /usr/share/nginx/html
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: my-pvc

3. PV dynamic supply

3.1.概念

The core of the Dynamic Provisioning mechanism is the API object of StorageClass

StorageClass declares storage plug-in, used to automatically create PV

3.2.流程图

Insert picture description here

3.3.部署外部插件(支持nfs)

Download link: https://github.com/kubernetes-retired/external-storage/tree/master/nfs-client/deploy
Insert picture description here

kubectl apply -f rbac.yaml			#授权访问apiserver

vi deployment.yaml

修改镜像为gmoney23/nfs-client-provisioner
填写正确的NFS服务器的地址和目录

Insert picture description here

kubectl apply -f deployment.yaml

Insert picture description here

kubectl apply -f class.yaml			#创建存储类

Insert picture description here

3.4.创建PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: managed-nfs-storage
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 25Gi

kubectl apply -f pvc.yaml

PV and PVC will be created automatically, and the
Insert picture description here
pod will be bound to use PVC in the same way as static provisioning, not repeating

Will automatically create directories under NFS shared storage
Insert picture description here

Guess you like

Origin blog.csdn.net/anqixiang/article/details/108903341