Dynamically Mounting a Local PV to a StatefulSet
Application Scenarios
Dynamic mounting is available only for creating a StatefulSet. It is implemented through a volume claim template (volumeClaimTemplates field) and depends on dynamic creation of PVs through StorageClass. In this mode, each pod in a multi-pod StatefulSet is associated with a unique PVC and PV. After a pod is rescheduled, the original data can still be mounted to it based on the PVC name. In the common mounting mode for a Deployment, if ReadWriteMany is supported, multiple pods of the Deployment will be mounted to the same underlying storage.
When updating a StatefulSet in Kubernetes, it is not allowed to add or delete the volumeClaimTemplates field. This field can only be configured during the creation of the StatefulSet.
Prerequisites
- You have created a cluster and installed the CSI add-on (Everest) in the cluster.
- To create a cluster using commands, ensure kubectl is used. For details, see Accessing a Cluster Using kubectl.
- You have imported a data disk of a node to the local PV storage pool. For details, see Importing a PV to a Storage Pool.
Dynamically Mounting a Local PV on the Console
- Log in to the CCE console and click the cluster name to access the cluster console.
- Choose Workloads in the navigation pane. In the right pane, click the StatefulSets tab.
- Click Create Workload in the upper right corner. On the displayed page, click Data Storage in the Container Information area under Container Settings and choose Add Volume > VCT.
- Click Create PVC. In the dialog box displayed, configure the volume claim template parameters.
Click Create.
Parameter
Description
PVC Type
In this section, select Local PV.
PVC Name
Enter the name of the PVC. After a PVC is created, a suffix is automatically added based on the number of pods. The format is <Custom PVC name>-<Serial number>, for example, example-0.
Creation Method
You can only select Dynamically provision to create a PVC, PV, and underlying storage on the console in cascading mode.
Storage Classes
The default StorageClass of local PVs is csi-local-topology.
You can customize a StorageClass and configure its reclaim policy and binding mode. For details, see Creating a StorageClass Using the CCE Console.
(Optional) Storage Volume Name Prefix
Available only when the cluster version is v1.23.14-r0, v1.25.9-r0, v1.27.6-r0, v1.28.4-r0, or later, and Everest of v2.4.15 or later is installed in the cluster.
This parameter specifies the name of the underlying storage that is automatically created. The actual underlying storage name is in the format of "Storage volume name prefix + PVC UID". If this parameter is left blank, the default prefix pvc will be used.
For example, if the storage volume name prefix is set to test, the actual underlying storage name is test-{UID}.
Capacity
Requested capacity of the storage volume in GiB or MiB.
NOTE:Local PVs are implemented using LVM, with a basic unit of 4 MiB for LVM logical extents (LEs). The actual usage of LVM logical volumes varies by write mode:
- Linear mode: If the requested PVC capacity is not a multiple of 4 MiB, the LVM logical volume capacity will be rounded up to the nearest 4 MiB multiple.
- Striping mode: LVM allocates storage in complete stripe sets. If the calculated number of stripe sets is not an integer, it will be automatically rounded up to the next whole number.
Access Mode
Local PVs support only ReadWriteOnce, indicating that a PV can be mounted to one node in read/write mode. For details, see Volume Access Modes.
Storage Pool
View the imported storage pool. For details about how to import a new data volume to the storage pool, see Importing a PV to a Storage Pool.
- Enter the path to which the volume is mounted.
Table 1 Mounting a storage volume Parameter
Description
Mount Path
Enter a mount path, for example, /tmp.
This parameter specifies a container path to which a data volume will be mounted. Do not mount the volume to a system directory such as / or /var/run. This may lead to container errors. Mount the volume to an empty directory. If the directory is not empty, ensure that there are no files that affect container startup. Otherwise, the files will be replaced, leading to container startup failures or workload creation failures.
NOTICE:If a volume is mounted to a high-risk directory, use an account with minimum permissions to start the container. Otherwise, high-risk files on the host may be damaged.
Subpath
Enter the subpath of the storage volume and mount a path in the storage volume to the container. In this way, different folders of the same storage volume can be used in a single pod. tmp, for example, indicates that data in the mount path of the container is stored in the tmp folder of the storage volume. If this parameter is left blank, the root path will be used by default.
Permission
- Read-only: You can only read the data in the mounted volumes.
- Read-write: You can modify the data volumes mounted to the path. Newly written data will not be migrated if the container is migrated, which may cause data loss.
In this example, the disk is mounted to the /data path of the container. The container data generated in this path is stored in the local PV.
- Dynamically mount and use storage volumes. For details about other parameters, see Creating a StatefulSet. After the configuration, click Create Workload.
After the workload is created, the data in the container mount directory will be persistently stored. Verify the storage by referring to Verifying Data Persistence.
Dynamically Mounting a Local PV Through kubectl
- Use kubectl to access the cluster.
- Create a file named statefulset-local.yaml. In this example, the local PV is mounted to the /data path.apiVersion: apps/v1kind: StatefulSetmetadata:name: statefulset-localnamespace: defaultspec:selector:matchLabels:app: statefulset-localtemplate:metadata:labels:app: statefulset-localspec:containers:- name: container-1image: nginx:latestvolumeMounts:- name: pvc-local # The value must be the same as that in the volumeClaimTemplates field.mountPath: /data # Location where the storage volume is mountedimagePullSecrets:- name: default-secretserviceName: statefulset-local # Headless Service namereplicas: 2volumeClaimTemplates:- apiVersion: v1kind: PersistentVolumeClaimmetadata:name: pvc-localnamespace: defaultannotations:everest.io/csi.volume-name-prefix: test # (Optional) Storage volume name prefix of the automatically created underlying storagespec:accessModes:- ReadWriteOnce # The value must be ReadWriteOnce for local PVs.resources:requests:storage: 10Gi # Storage volume capacitystorageClassName: csi-local-topology # The StorageClass is local PV.---apiVersion: v1kind: Servicemetadata:name: statefulset-local # Headless Service namenamespace: defaultlabels:app: statefulset-localspec:selector:app: statefulset-localclusterIP: Noneports:- name: statefulset-localtargetPort: 80nodePort: 0port: 80protocol: TCPtype: ClusterIP
Table 2 Key parameters Parameter
Mandatory
Description
everest.io/csi.volume-name-prefix
No
(Optional) This parameter is available only when the cluster version is v1.23.14-r0, v1.25.9-r0, v1.27.6-r0, v1.28.4-r0, or later, and Everest of v2.4.15 or later is installed in the cluster.
This parameter specifies the name of the underlying storage that is automatically created. The actual underlying storage name is in the format of "Storage volume name prefix + PVC UID". If this parameter is left blank, the default prefix pvc will be used.
Enter 1 to 26 characters that cannot start or end with a hyphen (-). Only lowercase letters, digits, and hyphens (-) are allowed.
For example, if the storage volume name prefix is set to test, the actual underlying storage name is test-{UID}.
storage
Yes
PVC capacity, in Gi or Mi. The value must be an integer.
If storage is set to a decimal, the value will be rounded up for the local PV. For example, if storage is set to 10.1Gi, an 11-GiB local PV will be created.
NOTE:Local PVs are implemented using LVM, with a basic unit of 4 MiB for LVM logical extents (LEs). The actual usage of LVM logical volumes varies by write mode:
- Linear mode: If the requested PVC capacity is not a multiple of 4 MiB, the LVM logical volume capacity will be rounded up to the nearest 4 MiB multiple.
- Striping mode: LVM allocates storage in complete stripe sets. If the calculated number of stripe sets is not an integer, it will be automatically rounded up to the next whole number.
storageClassName
Yes
The StorageClass of local PVs is csi-local-topology.
- Run the following command to create a workload to which the local PV is mounted:kubectl apply -f statefulset-local.yaml
After the workload is created, you can try Verifying Data Persistence.
Verifying Data Persistence
- View the deployed application and files.
- Run the following command to view the created pod:kubectl get pod | grep statefulset-local
Expected output:
statefulset-local-0 1/1 Running 0 45sstatefulset-local-1 1/1 Running 0 28s - Run the following command to check whether the local PV has been mounted to the /data path:kubectl exec statefulset-local-0 -- df | grep data
Expected output:
/dev/mapper/vg--everest--localvolume--persistent-pvc-local 10255636 36888 10202364 0% /data - Run the following command to check the files in the /data path:kubectl exec statefulset-local-0 -- ls /data
Expected output:
lost+found
- Run the following command to view the created pod:
- Run the following command to create a file named static in the /data path:kubectl exec statefulset-local-0 -- touch /data/static
- Run the following command to check the files in the /data path:kubectl exec statefulset-local-0 -- ls /data
Expected output:
lost+foundstatic - Run the following command to delete the pod named web-local-auto-0:kubectl delete pod statefulset-local-0
Expected output:
pod "statefulset-local-0" deleted - After the deletion, the StatefulSet controller automatically creates a replica with the same name. Run the following command to check whether the files in the /data path have been modified:kubectl exec statefulset-local-0 -- ls /data
Expected output:
lost+foundstaticThe static file is retained, indicating that the data in the local PV can be stored persistently.
Related Operations
You can also perform the operations listed in Table 3.
Operation | Description | Procedure |
---|---|---|
Viewing events | View event names, event types, number of occurrences, Kubernetes events, first occurrence time, and last occurrence time of the PVC or PV. |
|
Viewing a YAML file | View, copy, or download the YAML file of a PVC or PV. |
|
- Application Scenarios
- Prerequisites
- Dynamically Mounting a Local PV on the Console
- Dynamically Mounting a Local PV Through kubectl
- Verifying Data Persistence
- Related Operations