Help Center/ Cloud Container Engine/ Kubernetes Basics/ Persistent Storage/ PersistentVolume, PersistentVolumeClaim, and StorageClass
Updated on 2022-09-08 GMT+08:00

PersistentVolume, PersistentVolumeClaim, and StorageClass

hostPath volumes are used for persistent storage. However, hostPath volumes are node-specific. Writing data into hostPath volumes after a node restart may cause data inconsistency.

If you want to read the previously written data after a pod is rebuilt and scheduled again, you can count on network storage. Typically, a cloud vendor provides at least three classes of network storage: block storage, file storage, and object storage, such as EVS, SFS, and OBS provided by HUAWEI CLOUD. Kubernetes decouples how storage is provided from how it is consumed by introducing two API objects: PersistentVolume (PV) and PersistentVolumeClaim (PVC). You only need to request the storage resources you want, without being exposed to the details of how they are implemented.

  • A PV describes a persistent data storage volume. It defines a directory for persistent storage on a host machine, for example, a mount directory of a network file system (NFS).
  • A PVC describes the attributes of the PV that a pod wants to use, such as the volume capacity and read/write permissions.

To allow a pod to use PVs, a Kubernetes cluster administrator needs to set the network storage class and provides the corresponding PV descriptors to Kubernetes. You only need to create a PVC and bind the PVC with the volumes in the pod so that you can store data. The following figure shows the relationship between PVs and PVCs.

Figure 1 Binding a PVC to a PV

CSI

Kubernetes Container Storage Interface (CSI) can be used to develop plug-ins to support specific storage volumes. For example, in the namespace named kube-system, as shown in Namespace for Grouping Resources, everest-csi-controller-* and everest-csi-driver-* are the storage controllers and drivers developed by HUAWEI CLOUD CCE. With these drivers, you can use cloud storage services on HUAWEI CLOUD, such as EVS, SFS, and OBS.

$ kubectl get po --namespace=kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
everest-csi-controller-6d796fb9c5-v22df   2/2     Running   0          9m11s
everest-csi-driver-snzrr                  1/1     Running   0          12m
everest-csi-driver-ttj28                  1/1     Running   0          12m
everest-csi-driver-wtrk6                  1/1     Running   0          12m

PV

Each PV contains the specification and status of the volume. For example, a file system is created in HUAWEI CLOUD SFS. The file system ID is 68e4a4fd-d759-444b-8265-20dc66c8c502, and the mount point is sfs-nas01.cn-north-4b.myhuaweicloud.com:/share-96314776. If you want to use this file system in CCE, you need to create a PV to describe the volume. The following is an example PV.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-example
spec:
  accessModes:
  - ReadWriteMany                      # Read/write mode
  capacity:
    storage: 10Gi                      # PV capacity
  csi:
    driver: nas.csi.everest.io         # Driver to be used.
    fsType: nfs                        # File system type
    volumeAttributes:
      everest.io/share-export-location: sfs-nas01.cn-north-4b.myhuaweicloud.com:/share-96314776   # Mount point
    volumeHandle: 68e4a4fd-d759-444b-8265-20dc66c8c502                                            # File system ID

Fields under csi in the example above are exclusively used in HUAWEI CLOUD CCE.

Next, create the PV.

$ kubectl create -f pv.yaml
persistentvolume/pv-example created

$ kubectl get pv
NAME                 CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM    STORAGECLASS   REASON   AGE
pv-example           10Gi       RWX            Retain           Available                                    4s

RECLAIM POLICY indicates the PV reclaim policy. The value Retain indicates that the PV is retained after the PVC is released. If the value of STATUS is Available, the PV is available.

PVC

A PVC can be bound to a PV. The following is an example:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-example
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi              # Storage capacity
  volumeName: pv-example         # PV name

Create the PVC.

$ kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-example created

$ kubectl get pvc
NAME          STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-example   Bound    pv-example   10Gi       RWX                           9s

The command output shows that the PVC is in Bound state and the value of VOLUME is pv-example, indicating that the PVC is bound to a PV.

Now check the PV status.

$ kubectl get pv
NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS   REASON   AGE
pv-example    10Gi       RWX            Retain           Bound    default/pvc-example                            50s

The status of the PVC is also Bound. The value of CLAIM is default/pvc-example, indicating that the PV is bound to the PVC named pvc-example in the default namespace.

Note that PVs are cluster-level resources and do not belong to any namespace, while PVCs are namespace-level resources. PVs can be bound to PVCs of any namespace. Therefore, the namespace name "default" is displayed under CLAIM.

Figure 2 Relationship between PVs and PVCs

StorageClass

Although PVs and PVCs allow you to consume abstract storage resources, you may need to configure multiple fields to create PVs and PVCs (such as the csi field structure in the PV), and PVs/PVCs are generally managed by the cluster administrator, which can be inconvenient when you need PVs with varying attributes for different problems.

To solve this problem, Kubernetes supports dynamic PV provisioning to create PVs automatically. The cluster administrator can deploy a PV provisioner and define the corresponding StorageClass. In this way, developers can select the storage class to be created when creating a PVC. The PVC transfers the StorageClass to the PV provisioner, and the provisioner automatically creates a PV. In CCE, storage classes such as csi-disk, csi-nas, and csi-obs are supported. After storageClassName is added to a PVC, PVs can be automatically provisioned and underlying storage resources can be automatically created.

The following describes how to create a file system in CCE clusters of v1.15 and later, which is different from that for CCE clusters of v1.13 and earlier. For details, see .

Run the following command to query the storage classes that CCE supports. You can use the CSI plug-ins provided by CCE to customize a storage class, which functions similarly as the default storage classes in CCE.

# kubectl get sc
NAME                PROVISIONER                     AGE
csi-disk            everest-csi-provisioner         17d          # Storage class for EVS disks
csi-disk-topology   everest-csi-provisioner         17d          # Storage class for EVS disks with delayed association
csi-nas             everest-csi-provisioner         17d          # Storage class for SFS file systems
csi-obs             everest-csi-provisioner         17d          # Storage class for OBS buckets
csi-sfsturbo        everest-csi-provisioner         17d          # Storage class for SFS Turbo file systems

Use storageClassName to create a PVC.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name:  pvc-sfs-auto-example
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: csi-nas        # StorageClass

PVCs cannot be directly created by using the storageClassName csi-sfsturbo.

Create a PVC and view the PVC and PV details.

$ kubectl create -f pvc2.yaml
persistentvolumeclaim/pvc-sfs-auto-example created

$ kubectl get pvc
NAME                   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-sfs-auto-example   Bound    pvc-1f1c1812-f85f-41a6-a3b4-785d21063ff3   10Gi       RWX            csi-nas        29s

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                         STORAGECLASS   REASON   AGE
pvc-1f1c1812-f85f-41a6-a3b4-785d21063ff3   10Gi       RWO            Delete           Bound    default/pvc-sfs-auto-example  csi-nas                 20s

The command output shows that after a StorageClass is used, a PVC and a PV are created and they are bound to each other.

After a StorageClass is set, PVs can be automatically created and maintained. Users only need to specify StorageClassName when creating a PVC, which greatly reduces the workload.

Note that the types of StorageClassName vary among vendors. In this section, HUAWEI CLOUD SFS is used as an example. For details about other storage classes, see .

Using a PVC in a Pod

After a PVC is available, you can directly bind the PVC to a volume in the pod template and then mount the volume to the pod, as shown in the following example. You can also directly create a PVC in a StatefulSet. For details, see StatefulSet.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers: 
      - image: nginx:alpine
        name: container-0 
        volumeMounts: 
        - mountPath: /tmp                                # Mount path
          name: pvc-sfs-example 
      restartPolicy: Always 
      volumes: 
      - name: pvc-sfs-example 
        persistentVolumeClaim: 
          claimName:  pvc-example                       # PVC name