Mount a dynamic disk volume to an application and verify its data persistence.
Background
Disks are block-level storage products that use a distributed, multi-replica mechanism for low latency, high performance, durability, and reliability. They are well suited to non-shared data with demanding I/O and latency requirements. For more information, see Storage overview.
ACS supports only dynamic mounting of disk volumes; static mounting 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
-
A disk is non-shared storage and can be mounted to only a single Pod.
-
A disk can be mounted only to a Pod in the same availability zone.
Usage notes
-
We recommend mounting a disk to a StatefulSet or a standalone Pod, not to a Deployment.
NoteBecause a disk can be mounted to only one Pod, you cannot configure independent volumes for each Pod in a Deployment. Additionally, a Deployment's upgrade strategy can prevent a new Pod from mounting the disk during a restart.
-
Disks created dynamically are billed on a pay-as-you-go basis.
-
To learn how disks are billed, see Block storage billing.
-
For disk pricing, see Block Storage Pricing.
-
-
If you configure
securityContext.fsgroupin your application's YAML file, ACS runschmodandchownoperations after the mount completes, which increases mount time.NoteWhen
securityContext.fsgroupis configured, file ownership in the volume is adjusted during mount. Depending on the number of files, this can significantly increase startup time. SetfsGroupChangePolicytoOnRootMismatchto change ownership only on the first container start; subsequent Pod upgrades or rebuilds are unaffected. If this does not meet your needs, use an initContainer to implement custom permission logic.
Mount a disk volume
kubectl
Step 1: Create a StorageClass
ACS provides a default StorageClass named alicloud-disk-topology-alltype that creates 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 this default does not meet your requirements, create a new StorageClass as follows.
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.
-
Save the following YAML content as disk-sc.yaml. Modify the parameters as 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 disk type. Valid values:
-
cloud_essd_entry: ESSD Entry disk -
cloud_auto: ESSD AutoPL disk -
cloud_essd(default): ESSD -
cloud_ssd: standard SSD -
cloud_efficiency: ultra disk
You can combine these types. For example:
type: cloud_efficiency, cloud_ssd, cloud_essd. The system tries each type in order until a disk is created successfully.NoteYou can choose a disk type based on your billing and performance needs. For more information, see Block Storage Pricing and Block storage performance.
parameters.fstypeThe file system type. Default: ext4. Valid values:
ext3,ext4, andxfs.parameters.performanceLevelThe ESSD performance level. Default:
PL1. Valid values:PL0,PL1,PL2, andPL3. For more information, see ESSDs.provisionerThe storage driver. Set this to
diskplugin.csi.alibabacloud.comto use the Alibaba Cloud disk CSI plugin.reclaimPolicyThe reclaim policy for the disk. Only
Deleteis supported, meaning the PV and underlying disk are deleted when the persistent volume claim (PVC) is deleted.volumeBindingModeThe volume binding mode. We recommend
WaitForFirstConsumerfor multi-zone clusters.-
WaitForFirstConsumer: Delays volume binding. The Pod is scheduled first, then the disk is created in the Pod's availability zone. -
Immediate: Creates the disk before the Pod is scheduled.
ImportantIf you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use nodeSelector or ResourcePolicy to schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod label (
alibabacloud.com/acs: "true"), StorageClasses with theWaitForFirstConsumertype are not supported.allowVolumeExpansionWhether to allow automatic disk expansion.
-
-
Create the StorageClass.
kubectl create -f disk-sc.yaml -
View 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 ......
Step 2: Create a PVC
-
Save the following YAML content 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 ReadWriteOncePod is supported.
volumeModeThe volume mode. Valid values:
-
Filesystem(default): a file system -
Block: a block device
storageThe storage capacity to allocate to the Pod, which determines the size of the disk to be created.
storageClassNameThe name of the StorageClass to use.
-
-
Create the PVC.
kubectl create -f disk-pvc.yaml -
View the PVC.
kubectl get pvcThe expected output shows that the PVC is not yet bound to a PV because
volumeBindingModeis 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 disk
-
Save the following YAML content as disk-test.yaml.
The following YAML creates a StatefulSet with one Pod. The Pod requests storage through a PVC named
disk-pvcand mounts the volume at/data.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 the StatefulSet and mount the disk.
kubectl create -f disk-test.yamlImportantThe 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 expected output shows one Pod because the replica count of the StatefulSet is 1.
disk-test-0 1/1 Running 0 52s -
View the PVC.
kubectl get pvcThe expected output shows that the PVC is now bound to the automatically created PV.
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE disk-pvc Bound d-uf698s3h14isyj6b**** 20Gi RWOP alicloud-disk-essd <unset> 111s -
Check 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
Step 1: Create StorageClass
ACS provides a default StorageClass named alicloud-disk-topology-alltype that creates 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 this default does not meet your requirements, create a new StorageClass as follows.
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 Disk.
Disk
Parameter
The default parameter is
type, which specifies the disk type. Valid values:-
cloud_essd_entry: ESSD Entry disk -
cloud_auto: ESSD AutoPL disk -
cloud_essd(default): ESSD -
cloud_ssd: standard SSD -
cloud_efficiency: ultra disk
You can combine these types. For example:
type: cloud_efficiency, cloud_ssd, cloud_essd. The system tries each type in order until a disk is created successfully.NoteYou can choose a disk type based on your billing and performance needs. For more information, see Block Storage Pricing and Block storage performance.
You can add the following parameters:
-
fstypeThe file system type. Default: ext4. Valid values:
ext3,ext4, andxfs. -
performanceLevelThe ESSD performance level. Default:
PL1. Valid values:PL0,PL1,PL2, andPL3. For more information, see ESSDs.
type: cloud_essd
Reclaim Policy
The reclaim policy for the disk. Only
Deleteis supported, meaning the PV and underlying disk are deleted when the persistent volume claim (PVC) is deleted.Delete
Binding Mode
The volume binding mode. We recommend
WaitForFirstConsumerfor multi-zone clusters.-
WaitForFirstConsumer: Delays volume binding. The Pod is scheduled first, then the disk is created in the Pod's availability zone. -
Immediate: Creates the disk before the Pod is scheduled.
ImportantIf you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use nodeSelector or ResourcePolicy to schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod label (
alibabacloud.com/acs: "true"), StorageClasses with theWaitForFirstConsumertype are not supported.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 dialog box that appears, configure the parameters and then click Create.
Parameter
Description
Example
PVC Type
Select Disk.
Disk
Name
The name of the PVC. Follow the format requirements on the screen.
disk-pvc
Allocation Mode
By default, Use StorageClass is selected.
Use StorageClass
Existing Storage Class
Select the StorageClass to use.
alicloud-disk-essd
Capacity
The storage capacity to allocate to the Pod, which determines 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, it appears 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.
Step 3: Create an application and mount disk
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, and then click Create.
Set the following parameters and keep the defaults for the rest. For more information, see Create a StatefulSet workload.
Configuration section
Parameter
Description
Example
Basic Information
Application 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 container image address for the application.
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
Required Resources
The requested vCPU and memory resources.
0.25 vCPU, 0.5 GiB
Volume
Click Add PVC and configure the parameters.
-
Mount Source: Select the PVC that you created.
-
Container Path: Enter the path in the container to mount the disk.
-
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 volume and the claim.
-
On the Persistent Volumes page, you will find an automatically created PV. The name of the PV corresponds to the disk ID.
-
On the Persistent Volume Claims page, the PVC is now bound to the PV, and its status is Bound.
-
Verify disk data persistence
The StatefulSet created above contains one Pod with a mounted disk. When the Pod is deleted, a new Pod is automatically created and re-mounts the same disk, preserving the data. Follow these steps to verify data persistence.
-
View the data on the disk at the mount path.
kubectl exec disk-test-0 -- ls /dataExpected output:
lost+found -
Write a test file to the disk.
kubectl exec disk-test-0 -- touch /data/test -
Delete the Pod.
kubectl delete pod disk-test-0NoteAfter you delete a Pod in a StatefulSet, the system automatically creates a new Pod.
-
View the newly created Pod.
kubectl get podThe output shows that the new Pod has the same name as the deleted one, which is standard StatefulSet behavior.
NAME READY STATUS RESTARTS AGE disk-test-0 1/1 Running 0 27s -
Verify that the new Pod has re-mounted the disk and the data persists.
kubectl exec disk-test-0 -- ls /dataThe output shows that the
testfile written earlier is still on the disk.lost+found test