For applications that handle non-shared data and require low latency and high I/O performance, you can use a disk as a Persistent Volume (PV). This topic shows how to mount a dynamic disk volume to an application and verify its data persistence.
Background information
Disks are a block storage service for block-level data. They use a distributed, multi-replica architecture to deliver low latency, high performance, durability, and reliability. This makes them ideal for applications that handle non-shared data and require high I/O performance and low latency. For more information, see Storage overview.
Alibaba Cloud Container Compute Service (ACS) supports only dynamic mounting of disk volumes. Statically mounting disk volumes is not supported.
Prerequisites
The managed-csiprovisioner component is installed in the ACS cluster.
Go to the ACS cluster management page in the ACS console. In the left-side navigation pane of the cluster management page, choose . On the Storage tab, you can check whether managed-csiprovisioner is installed.
Limitations
Disks provide non-shared storage. A disk volume can only be mounted to a single Pod.
A disk can only be mounted to a Pod within the same Availability Zone; cross-zone mounting is not supported.
Usage notes
We recommend using a StatefulSet or mounting a disk to a standalone Pod. Using a Deployment is not advised, for the following reason:
To mount a disk to a Deployment, you must set
replicas: 1, because a disk can only attach to one Pod. This prevents per-Pod volumes, offers no guaranteed mount/unmount order, and—due to the Deployment's update strategy—can cause significant mount delays during restarts.
When you use a dynamic disk volume, the automatically created disk uses the pay-as-you-go billing method.
To learn how disks are billed, see Block storage devices.
For disk pricing information, see Prices of block storage devices.
If you configure
securityContext.fsGroupin your application's YAML file when using a dynamic disk volume, ACS will runchmodandchownoperations after the mount is complete. This increases the mount time.NoteWhen
securityContext.fsGroupis configured, the owner of the files in the volume is automatically adjusted during the mount process. Depending on the number of files, this can significantly increase startup time. SetfsGroupChangePolicytoOnRootMismatchto change file ownership only when the container starts for the first time.For subsequent Pod upgrades or rebuilds, the mount time will return to normal. If this approach is unsuitable, consider using an init container to implement your own permission-setting logic.
Mount a disk volume
kubectl
By default, ACS provides a StorageClass named alicloud-disk-topology-alltype. This StorageClass attempts to create a Persistent Volume (PV) by trying the following disk types in order: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If the default StorageClass does not meet your requirements, follow these steps to create a new one.
Connect to your ACS cluster. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster and Use kubectl on Cloud Shell to manage ACS clusters.
Modify and save the following YAML as
disk-sc.yamlbased on the parameters described in the table below.apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: alicloud-disk-essd provisioner: diskplugin.csi.alibabacloud.com parameters: type: cloud_essd fstype: ext4 performanceLevel: PL1 reclaimPolicy: Delete volumeBindingMode: WaitForFirstConsumer allowVolumeExpansion: trueThe following table describes the parameters.
Parameter
Description
parameters.typeThe type of disk. Valid values:
cloud_essd_entry: ESSD Entry diskcloud_auto: ESSD AutoPL diskcloud_essd(default): ESSDcloud_ssd: standard SSDcloud_efficiency: ultra diskYou can combine these parameters, for example,
type: cloud_efficiency, cloud_ssd, cloud_essd. The system will try to create a disk of each specified type in order until one is created.
NoteChoose a disk type based on your billing and performance needs. For more information, see Prices of block storage devices and Block storage performance.
parameters.fstypeThe file system type for the disk. The default is
ext4. Supported types areext3,ext4, andxfs.parameters.performanceLevelThe performance level of the ESSD. The default is
PL1. Supported levels arePL0,PL1,PL2, andPL3. For more information, see ESSDs.provisionerThe driver type. Set this to
diskplugin.csi.alibabacloud.comto use the Alibaba Cloud disk CSI plug-in.reclaimPolicyThe reclaim policy for the disk. Only
Deleteis supported, meaning that when you delete the PVC, the system also deletes the PV and its underlying disk.volumeBindingModeThe volume binding mode. If you are using a multi-zone cluster, we recommend setting this to
WaitForFirstConsumer.WaitForFirstConsumer: Delays volume binding. The Pod is scheduled first, and then the disk is created based on the Pod's Availability Zone.Immediate: The disk is created before the Pod.
ImportantIf you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use
nodeSelectororResourcePolicyto schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod labelalibabacloud.com/acs: "true", StorageClasses withvolumeBindingMode: WaitForFirstConsumerare not supported.allowVolumeExpansionSpecifies whether to automatically expand the disk.
Create a StorageClass.
kubectl create -f disk-sc.yamlCheck the StorageClass.
kubectl get scExpected output:
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE alicloud-disk-essd diskplugin.csi.alibabacloud.com Delete WaitForFirstConsumer true 78s alicloud-disk-topology-alltype diskplugin.csi.alibabacloud.com Delete WaitForFirstConsumer true 23d ......Save the following YAML as
disk-pvc.yaml.apiVersion: v1 kind: PersistentVolumeClaim metadata: name: disk-pvc spec: accessModes: - ReadWriteOncePod volumeMode: Filesystem resources: requests: storage: 20Gi storageClassName: alicloud-disk-essdThe following table describes the parameters.
Parameter
Description
accessModesThe access mode. Only
ReadWriteOncePodis currently supported.volumeModeThe volume mode. Valid values:
Filesystem(default) orBlock.storageThe amount of storage to allocate to the Pod, which is the size of the disk to be created.
storageClassNameThe name of the StorageClass to bind.
Create the PVC.
kubectl create -f disk-pvc.yamlCheck the PVC.
kubectl get pvcThe expected output shows that the PVC has not yet been bound to a PV because
volumeBindingModeis set toWaitForFirstConsumer.NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE disk-pvc Pending alicloud-disk-essd <unset> 7sSave the following YAML as
disk-test.yaml.The following YAML defines a StatefulSet with one Pod. The Pod requests storage resources through a PVC named
disk-pvcand mounts the volume to the/datapath.apiVersion: apps/v1 kind: StatefulSet metadata: name: disk-test spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest ports: - containerPort: 80 volumeMounts: - name: pvc-disk mountPath: /data volumes: - name: pvc-disk persistentVolumeClaim: claimName: disk-pvcCreate the StatefulSet and mount the disk.
kubectl create -f disk-test.yamlImportantWhen you create the StatefulSet, the system automatically provisions a pay-as-you-go disk and a corresponding PV based on the PVC and StorageClass configurations.
Check the deployment status of the Pod in the StatefulSet.
kubectl get pod | grep disk-testThe output shows one Pod, as expected, because
replicasis set to 1 in the StatefulSet.disk-test-0 1/1 Running 0 52sCheck the PVC.
kubectl get pvcThe expected output shows that the PVC is now bound to the automatically created PV of the disk type.
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE disk-pvc Bound d-uf698s3h14isyj6b**** 20Gi RWOP alicloud-disk-essd <unset> 111sCheck the mount path to confirm that the disk is mounted.
kubectl exec disk-test-0 -- df | grep dataExpected output:
/dev/vdb 20466256 24 20449848 1% /data
Console
By default, ACS provides a StorageClass named alicloud-disk-topology-alltype. This StorageClass attempts to create a Persistent Volume (PV) by trying the following disk types in order: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If the default StorageClass does not meet your requirements, follow these steps to create a new one.
Log on to the ACS console.
On the Clusters, click the name of the cluster to go to the cluster management page.
In the left-side navigation pane of the cluster management page, choose .
Create a StorageClass.
On the StorageClasses page, click Create.
In the dialog box that appears, configure the parameters and then click Create.
Parameter
Description
Example
Name
The name of the StorageClass. Follow the format requirements on the screen.
alicloud-disk-essd
PV Type
Select Cloud Disk.
Cloud Disk
Parameter
The default parameter is
type, which specifies the disk type. Valid values are:cloud_essd_entry: ESSD Entry diskcloud_auto: ESSD AutoPL diskcloud_essd(default): ESSDcloud_ssd: standard SSDcloud_efficiency: ultra diskYou can combine these parameters, for example,
type: cloud_efficiency, cloud_ssd, cloud_essd. The system will try to create a disk of each specified type in order until one is created.
NoteChoose a disk type based on your billing and performance needs. For more information, see Prices of block storage devices and Block storage performance.
You can add the following parameters:
fstypeThe file system type for the disk. The default is
ext4. Supported types areext3,ext4, andxfs.performanceLevelThe performance level of the ESSD. The default is
PL1. Supported levels arePL0,PL1,PL2, andPL3. For more information, see ESSDs.
type:cloud_essd
Reclaim Policy
The reclaim policy for the disk. Only
Deleteis supported, meaning that when you delete the PVC, the system also deletes the PV and its underlying disk.Delete
Binding Mode
The volume binding mode. If you are using a multi-zone cluster, we recommend setting this to
WaitForFirstConsumer.Delays volume binding. The Pod is scheduled first, and then the disk is created based on the Pod's Availability Zone.
Immediate: The disk is created before the Pod.
ImportantIf you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use
nodeSelectororResourcePolicyto schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod labelalibabacloud.com/acs: "true", StorageClasses withvolumeBindingMode: WaitForFirstConsumerare not supported.WaitForFirstConsumer
In the left-side navigation pane of the cluster management page, choose .
On the Persistent Volume Claims page, click Create.
In the dialog box that appears, configure the parameters and then click Create.
Parameter
Description
Example
PVC Type
Select Cloud Disk.
Cloud Disk
Name
The name of the PVC. Follow the format requirements on the screen.
disk-pvc
Allocation Mode
The default selection is Use StorageClass.
Use StorageClass
Existing Storage Class
Select the StorageClass to bind.
alicloud-disk-essd
Capacity
The amount of storage to allocate to the Pod, which is the size of the disk to be created.
20Gi
Access Mode
Only ReadWriteOncePod is supported, which means the volume can be mounted as read-write by a single Pod.
ReadWriteOncePod
After the PVC is created, you can see it on the Persistent Volume Claims page. Because the binding mode in the StorageClass is
WaitForFirstConsumer, the PVC is not yet bound to a PV and its status is Pending.In the left-side navigation pane of the cluster management page, choose .
On the StatefulSets page, click Create from Image.
Configure the parameters for the StatefulSet, then click Create.
Configure the following parameters and keep the default values for the others. For more information, see Use a StatefulSet to create a stateful application.
Configuration page
Parameter
Description
Example
Basic Information
Name
The name of the StatefulSet. Follow the format requirements on the screen.
disk-test
Replicas
The number of replicas for the StatefulSet.
1
Container
Image Name
The address of the image to use for the application deployment.
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
Required Resources
The vCPU and memory resources to request.
0.25 vCPU, 0.5 GiB
Volume
Click Add PVC and configure the parameters.
Mount Source: Select the PVC you created earlier.
Container Path: Enter the path in the container where the disk should be mounted.
Mount Source: disk-pvc.
Container Path: /data.
Check the application deployment status.
On the StatefulSets page, click the name of the application.
On the Pods tab, confirm that the Pod is running properly (its status is Running).
Check the PV and PVC.
On the Persistent Volumes page, you can see that a PV has been automatically created. The name of the PV corresponds to the disk ID.
On the Persistent Volume Claims page, you can see that the PVC is now bound to the PV, and its status is Bound.
Verify disk data persistence
The StatefulSet from the previous section contains one Pod with a mounted disk. When this Pod is deleted, a new one is automatically created and re-mounts the same disk, preserving the data. To verify the disk's data persistence, follow these steps.
Check the mount path to view the data on the disk.
kubectl exec disk-test-0 -- ls /dataExpected output:
lost+foundWrite a file to the disk.
kubectl exec disk-test-0 -- touch /data/testDelete the Pod.
kubectl delete pod disk-test-0NoteAfter you delete a Pod in a StatefulSet, the system automatically creates a new one.
Check the newly created Pod.
kubectl get podThe expected output shows that the new Pod has the same name as the old one, which is a feature of StatefulSets.
NAME READY STATUS RESTARTS AGE disk-test-0 1/1 Running 0 27sConfirm that the new Pod has re-mounted the disk and that the data persists.
kubectl exec disk-test-0 -- ls /dataThe expected output shows that the
testfile you wrote earlier is still on the disk.lost+found test