If your applications require high IOPS and low latency but do not require data sharing, you can mount disks as persistent volumes (PVs) to your applications. This topic describes how to mount a dynamically provisioned disk volume to an application and check whether the disk volume can be used to persist data.
Background information
Disks are block-level storage devices that use a distributed multi-replica mechanism to ensure low latency, high performance, high durability, and high reliability. Disks are suitable for applications that require high IOPS and low latency but do not require data sharing. For more information, see Storage overview.
Alibaba Cloud Container Compute Service (ACS) supports only dynamically provisioned disk volumes. ACS does not support statically provisioned disk volumes.
Prerequisites
The latest version of managed-csiprovisioner is installed in your Alibaba Cloud Container Compute Service (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.Limits
Disks cannot be shared. A disk can be mounted only to one pod.
You can mount a disk only to a pod that resides in the same zone as the disk.
Usage notes
We recommend that you mount disks to pods or StatefulSets instead of Deployments.
NoteA disk can be mounted only to one pod. If you want to mount a disk to a Deployment, you must set the number of pod replicas to one for the Deployment. If you configure multiple pods for the Deployment, you cannot mount a separate disk volume to each pod. In addition, you cannot specify the volume mount and unmount priorities of pods. When you restart the pod of a Deployment, the disk may fail to be mounted to the restarted pod due to the update policy used by Deployments. We recommend that you do not mount disks to Deployments.
When you mount dynamically provisioned disk volumes, the disks that are automatically created use the pay-as-you-go billing method.
For more information about the billing rules of disks, see Block storage devices.
For more information about the prices of disks, see Prices of block storage devices.
When you mount a dynamically provisioned disk volume to an application, if the application configuration includes the
securityContext.fsgroup
parameter, ACS automatically runs thechmod
andchown
commands after the volume is mounted, which may slow down the volume mount process.NoteAfter you add the
securityContext.fsgroup
parameter to the application configuration, ACS automatically modifies the ownership of files in the volume when mounting the disk to the application. The time required for ownership modification depends on the number of files in the volume. If a large number of files exist in the volume, the modification process may be time-consuming. You can set thefsGroupChangePolicy
parameter in the pod configuration toOnRootMismatch
. This way, ACS modifies file ownership only when the pod is started for the first time. When you update or recreate the pod after creation, the volume mount process does not include ownership modification. If the preceding setting does not meet your business requirements, we recommend that you create init containers and grant the init containers the permissions to perform the relevant operations.
Mount a dynamically provisioned disk volume
kubectl
Step 1: Create a StorageClass
ACS provides a default StorageClass named alicloud-disk-topology-alltype. This StorageClass can be used to provision a PV of the following types in descending order of priority: cloud_essd
(ESSD), cloud_ssd
(standard SSD), and cloud_efficiency
(ultra disk). If the default StorageClass does not meet your business requirements, perform the following steps to create a custom StorageClass:
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.
Create a file named disk-sc.yaml and copy the following template content into the file. Modify the parameters in the template based on your business requirements.
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: true
The following table describes the parameters.
Parameter
Description
type
The category of the disk. Valid values:
cloud_essd_entry
: ESSD Entry disk.cloud_auto
: ESSD AutoPL disk.cloud_essd
: Enterprise SSD (ESSD). This is the default value.cloud_ssd
: standard SSD.cloud_efficiency
: ultra disk.
You can set this parameter to any combination of the preceding values. For example, you can set this parameter to
type: cloud_efficiency, cloud_ssd, cloud_essd
. In this case, the system tries to create a disk of a specified category in sequence. The system stops trying once a disk is created.NoteYou can select disk categories based on your requirements for billing and performance. For more information, see Prices of block storage devices and Block storage performance.
fstype
The file system type of the disk. Default value: ext4. Valid values:
ext3
,ext4
, andxfs
.performanceLevel
The performance level (PL) of the ESSD. Default value:
PL1
. Valid values:PL0
,PL1
,PL2
, andPL3
. For more information, see ESSDs.provisioner
The type of the driver. In this example, the parameter is set to
diskplugin.csi.alibabacloud.com
, which indicates that the Container Storage Interface (CSI) plug-in provided by Alibaba Cloud is used.reclaimPolicy
The reclaim policy of the disk. You must set the value to
Delete
, which indicates that the PV and disk are automatically deleted when the persistent volume claim (PVC) is deleted.volumeBindingMode
The binding mode of the disk. You must set the value to
WaitForFirstConsumer
, which indicates that the binding and provisioning of a PV is delayed until a pod that uses the PVC is created. After the pod is scheduled to a node, a disk is created in the zone where the node is deployed and provisioned as the PV.allowVolumeExpansion
Specifies whether to automatically resize the disk.
Create a StorageClass.
kubectl create -f disk-sc.yaml
Check the StorageClass.
kubectl get sc
Expected 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 ......
Step 2: Create a PVC
Create a file named disk-pvc.yaml and copy the following content to the file:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: disk-pvc spec: accessModes: - ReadWriteOncePod volumeMode: Filesystem resources: requests: storage: 20Gi storageClassName: alicloud-disk-essd
The following table describes the parameters.
Parameter
Description
accessModes
The access mode. You must set the value to ReadWriteOncePod.
volumeMode
The mode of the volume. Valid values:
Filesystem
: the file system mode. This is the default value.Block: block gateway
: the block device mode.
storage
Specify the storage capacity that you want to allocate to the pod. This is also the capacity of the disk.
storageClassName
The name of the StorageClass that you want to associate with the PVC.
Create a PVC.
kubectl create -f disk-pvc.yaml
Check the PVC.
kubectl get pvc
The following output shows that no PV is bound to the PVC because the
volumeBindingMode
parameter is set toWaitForFirstConsumer
.NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE disk-pvc Pending alicloud-disk-essd <unset> 7s
Step 3: Create an application and mount a disk to the application
Create a file named disk-test.yaml and copy the following content to the file.
The following code block specifies the configuration of a StatefulSet that provisions one pod. The pod applies for storage resources by using the
disk-pvc
PVC, which is mounted to the/data
path of the pod.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-pvc
Create a StatefulSet and mount a disk to the StatefulSet.
kubectl create -f disk-test.yaml
ImportantDuring the deployment process of the StatefulSet, the system automatically creates a pay-as-you-go disk and PV based on the PVC and StorageClass specified in the StatefulSet configuration.
Check whether the pod provisioned by the StatefulSet is deployed.
kubectl get pod | grep disk-test
The following output shows that one pod is deployed for the StatefulSet.
disk-test-0 1/1 Running 0 52s
Check the PVC.
kubectl get pvc
The following output shows that a PV of the disk type is automatically created and bound to the PVC.
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE disk-pvc Bound d-uf698s3h14isyj6b**** 20Gi RWOP alicloud-disk-essd <unset> 111s
View files in the mount path to check whether the disk is mounted.
kubectl exec disk-test-0 -- df | grep data
Expected output:
/dev/vdb 20466256 24 20449848 1% /data
Console
Step 1: Create a StorageClass
ACS provides a default StorageClass named alicloud-disk-topology-alltype. This StorageClass can be used to provision a PV of the following types in descending order of priority: cloud_essd
(ESSD), cloud_ssd
(standard SSD), and cloud_efficiency
(ultra disk). If the default StorageClass does not meet your business requirements, perform the following steps to create a custom StorageClass:
Log on to the ACS console.
On the Clusters, click the ID 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 Create dialog box, configure the parameters and click Create.
Parameter
Description
Example
Name
Enter a custom name for the StorageClass. The name must meet the format requirements displayed in the console.
alicloud-disk-essd
PV Type
Select Cloud Disk.
Cloud Disk
Parameter
By default, the
type
parameter is added. This parameter specifies the disk category. Valid values:cloud_essd_entry
: ESSD Entry disk.cloud_auto
: ESSD AutoPL disk.cloud_essd
: Enterprise SSD (ESSD). This is the default value.cloud_ssd
: standard SSD.cloud_efficiency
: ultra disk.
You can set this parameter to any combination of the preceding values. For example, you can set this parameter to
type: cloud_efficiency, cloud_ssd, cloud_essd
. In this case, the system tries to create a disk of a specified category in sequence. The system stops trying once a disk is created.NoteYou can select disk categories based on your requirements for billing and performance. For more information, see Prices of block storage devices and Block storage performance.
You can add the following parameters:
fstype
The file system type of the disk. Default value: ext4. Valid values:
ext3
,ext4
, andxfs
.performanceLevel
The performance level (PL) of the ESSD. Default value:
PL1
. Valid values:PL0
,PL1
,PL2
, andPL3
. For more information, see ESSDs.
type:cloud_essd
Reclaim Policy
The reclaim policy of the disk. You must set the value to
Delete
, which indicates that the PV and disk are automatically deleted when the persistent volume claim (PVC) is deleted.Delete
Binding Mode
The binding mode of the disk. You must set the value to
WaitForFirstConsumer
, which indicates that the binding and provisioning of a PV is delayed until a pod that uses the PVC is created. After the pod is scheduled to a node, a disk is created in the zone where the node is deployed and provisioned as the PV.WaitForFirstConsumer
Step 2: Create a PVC
In the left-side navigation pane of the cluster management page, choose
.On the Persistent Volume Claims page, click Create.
In the Create PVC dialog box, configure the parameters and click Create.
Parameter
Description
Example
PVC Type
Select Cloud Disk.
Cloud Disk
Name
Enter a custom name for the PVC. The name must meet the format requirements displayed in the console.
disk-pvc
Allocation Mode
By default, Use StorageClass is selected.
Use StorageClass
Existing Storage Class
Select the StorageClass that you want to associate with the PVC.
alicloud-disk-essd
Capacity
Specify the storage capacity that you want to allocate to the pod. This is also the capacity of the disk.
20Gi
Access Mode
You must select ReadWriteOnce, which indicates that the volume is mounted only to one pod in read-write mode.
ReadWriteOnce
After you create the PVC, you can view the PVC on the Persistent Volume Claims page. The PVC is in the Pending state and no PV is mounted to the PVC because the
WaitForFirstConsumer
binding mode is specified in the StorageClass associated with the PVC.
Step 3: Create an application and mount a disk to the application
In the left-side navigation pane of the cluster management page, choose
.In the upper-right corner of the StatefulSets page, click Create from Image.
Configure the parameters of the StatefulSet and click Create.
The following table describes some of the parameters. Use the default values for other parameters. For more information, see Use a StatefulSet to create a stateful application.
Wizard page
Parameter
Description
Example
Basic Information
Name
Enter a custom name for the StatefulSet. The name must meet the format requirements displayed in the console.
disk-test
Replicas
The number of pod replicas provisioned by the StatefulSet.
1
Container
Image Name
The address of the image used to deploy the application.
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
Required Resources
Specify the number of vCores and the amount of memory required by the application.
0.25 vCores and 0.5 GiB of memory
Volume
Click Add PVC and configure the parameters.
Mount Source: Select the PVC you created.
Container Path: Specify the container path to which you want to mount the disk.
Mount Source: disk-pvc.
Container Path: /data.
Check whether the application is deployed.
On the StatefulSets page, click the name of the application you created.
On the Pods tab, check whether the pod is in the Running state.
Check the PV and PVC.
On the Persistent Volumes page, you can view the PV created by the system. The PV is named after the ID of the mounted disk.
On the Persistent Volume Claims page, you can view that the PVC is in the Bound state and the preceding PV is bound to the PVC.
Check whether data persistence is enabled based on the disk
The StatefulSet created in the preceding example provisions one pod and a disk is mounted to the pod. If you delete the pod, the system automatically recreates the pod. The original disk is mounted to the new pod and data still exists on the disk. Perform the following steps to test whether data is persisted to the disk:
View files in the mount path to check whether files on the disk can be viewed.
kubectl exec disk-test-0 -- ls /data
Expected output:
lost+found
Create a file on the disk.
kubectl exec disk-test-0 -- touch /data/test
Delete the pod.
kubectl delete pod disk-test-0
NoteAfter you delete the pod, the system automatically recreates the pod.
Check the new pod.
kubectl get pod
The following output shows that the new pod has the same name as the pod you deleted.
NAME READY STATUS RESTARTS AGE disk-test-0 1/1 Running 0 27s
Check whether the original disk is mounted to the pod and the file still exists on the disk.
kubectl exec disk-test-0 -- ls /data
The following output shows that the
test
file still exists on the disk.lost+found test