Alibaba Cloud disks are block-level data storage products for Elastic Compute Service (ECS). Alibaba Cloud disks provide low latency, high performance, high durability, and high reliability. Container Service for Kubernetes (ACK) allows you to use the Container Storage Interface (CSI) plug-in to create dynamically provisioned disk volumes. This topic describes how to use a dynamically provisioned disk volume and how to verify that a dynamically provisioned disk volume can be used to persist data.
Prerequisites
- An ACK cluster is created and the CSI plug-in is deployed in the cluster. For more information, see Create an ACK managed cluster.
- You are connected to the cluster by using kubectl. For more information, see Connect to ACK clusters by using kubectl.
Background information
Use scenarios: If no disk is available when you deploy an application in an ACK cluster, the system automatically purchases a disk and mounts it as a dynamically provisioned volume.
- Manually create a persistent volume claim (PVC) and specify a StorageClass in the PVC.
- When you deploy an application, the system automatically creates a persistent volume (PV) based on the specified StorageClass.
For more information about StorageClasses, see StorageClasses.
Use a dynamically provisioned disk volume in the ACK console
Step 1: Create a StorageClass
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose Volumes > StorageClasses.
- In the upper-right corner of the StorageClasses page, click Create.
- In the Create dialog box, set the parameters. The following table describes some of the parameters.
Parameter Description Name The name of the StorageClass. The name must start with a lowercase letter and can contain only lowercase letters, digits, periods (.), and hyphens (-).
PV Type Select Cloud Disk or NAS. In this example, Cloud Disk is selected. Volume Plug-in By default, CSI is selected. Parameter By default, a type parameter is added and set to cloud_essd. The type parameter specifies the type of disk. The supported values of the type parameter include cloud_efficiency, cloud_ssd, cloud_essd, and available. You can specify one or more values. However, if you specify available, you cannot specify other values. For example, you can specify type: cloud_efficiency, cloud_ssd, cloud_essd
. This indicates that the system attempts to create a disk of the specified types in sequence. The system stops trying once a disk is created. If you set the type parameter to available, the system first attempts to create a standard SSD. If the attempt fails, the system attempts to create an ultra disk. The system stops trying once a disk is created.Note Some ECS instance types do not support enhanced SSDs. For more information, see FAQ.You can add custom parameters. For example, you can add the zoneId parameter to specify the IDs of the zones where you want to create the disk. If your cluster is deployed in a single zone, set the value to the ID of the zone. Example:
cn-beijing-a
. If your cluster is deployed across zones, you can set the zoneId parameter to multiple zone IDs based on your business requirements. Example:cn-beijing-a, cn-beijing-b
.Reclaim Policy The reclaim policy of the disk. By default, this parameter is set to Delete. You can also set this parameter to Retain. - Delete mode: When PVCs are deleted, the related PVs and disks are deleted.
- Retain mode: When PVCs are deleted, the related PVs and disks are retained. The PVs and disk data can only be manually deleted.
If you require higher data security, we recommend that you use the Retain mode to avoid data loss caused by user errors.
Binding Mode The binding mode of the disk. Default value: Immediate. You can also set the value to WaitForFirstConsumer. - Immediate: creates a disk when the PVC that uses the StorageClass is created. This way, a disk is created and provisioned before you create a pod that uses the disk.
- WaitForFirstConsumer: delays the binding and provisioning of a PV 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.
- After you set the parameters, click Create. You can find the created StorageClass in the StorageClasses list.
Step 2: Create a PVC
- In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims.
- In the upper-right corner of the Persistent Volume Claims page, click Create.
- In the Create PVC dialog box, set the following parameters:
Parameter Description PVC Type You can select Cloud Disk, NAS, or OSS. In this example, Cloud Disk is selected. Name The name of the PVC. The name must be unique in the namespace. Allocation Mode In this example, Use StorageClass is selected. Existing Storage Class Click Select. In the Select Storage Class dialog box, find the StorageClass that you want to use and click Select in the Actions column. Capacity The capacity claimed by the PVC. Access Mode Default value: ReadWriteOnce. You can also select ReadOnlyMany or ReadWriteMany. - Click Create. After the PVC is created, you can view it in the PVC list. The PVC is bound to a PV.
Step 3: Create an application
- In the left-side navigation pane of the details page, choose Workloads > StatefulSets.
- In the upper-right corner of the StatefulSets page, click Create from Image.
- Set the application parameters. This example shows how to set the volume parameters. For more information about other parameters, see Use a StatefulSet to create a stateful application.
You can configure local storage volumes and cloud storage volumes for an ACK cluster. In this example, Cloud Storage is selected.
Mount the disk volume that is created in this example to the /tmp path of the container. After the disk volume is mounted, the container data that is generated in the /tmp path is stored in the disk volume. - Set other parameters and click Create. After the application is created, you can use the disk volume to store application data.
Use a dynamically provisioned disk volume by using kubectl
Step 1: Create a StorageClass
You can create a StorageClass by using one of the following methods based on your business scenarios.
Method 1: Create a StorageClass that specifies the WaitForFirstConsumer mode
In WaitForFirstConsumer mode, you can mount a disk to an ECS instance in a different zone. In the following example, a StorageClass is created by using a storage-class-csi-topology.yaml file.
- Use the following template to create a storage-class-topology.yaml file:
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: alicloud-disk-topology-essd provisioner: diskplugin.csi.alibabacloud.com parameters: type: cloud_essd resourceGroupId: "default" regionId: cn-beijing zoneId: cn-beijing-a fstype: ext4 readonly: "true" diskTags: "a:b,b:c" encrypted: "false" performanceLevel: PL1 volumeBindingMode: WaitForFirstConsumer reclaimPolicy: Retain allowVolumeExpansion: true
Parameter Description name The name of the StorageClass. provisioner Set this parameter to diskplugin.csi.alibabacloud.com
. This indicates that the provisioner plug-in for Alibaba Cloud disks is used to create the StorageClass.type The type of disk. The supported values include cloud_efficiency, cloud_ssd, cloud_essd, and available. You can specify one or more values. However, if you specify available, you cannot specify other values. For example, you can set this parameter to type: cloud_efficiency, cloud_ssd, cloud_essd
. This indicates that the system attempts to create a disk of the specified types in sequence. The system stops trying once a disk is created. If you set this parameter to available, the system first attempts to create a standard SSD. If the attempt fails, the system attempts to create an ultra disk. The system stops trying once a disk is created.Note Some ECS instance types do not support enhanced SSDs. For more information, see FAQ.resourceGroupId Optional. The resource group to which the disk belongs. Default value: default
.regionId Optional. The ID of the region where the disk is to be created. The value must be set to the region ID of the cluster. zoneId Optional. The zone where the disk is to be created. - If your cluster is deployed in a single zone, set the value to the ID of the zone.
- If your cluster is deployed across zones, you can set zoneId to multiple zone IDs based on your business requirements. Example:
zoneId: cn-hangzhou-a,cn-hangzhou-b,cn-hangzhou-c
fstype Optional. The file system of the disk. Default value: ext4. readonly Optional. Specifies whether the disk is read-only. - true: The disk is read-only.
- false: The disk is readable and writable.
mkfsOptions Optional. The parameter that is specified to format the disk. Example: mkfsOptions: "-O project,quota"
.diskTags Optional. The custom tags of the disk. Example: diskTags: "a:b,b:c"
.encrypted Optional. Specifies whether to encrypt the created disk. Default value: false. This indicates that the created disk is not encrypted. performanceLevel Optional. Valid values: PL1, PL2, and PL3. For more information, see Capacity and PLs. reclaimPolicy The reclaim policy of the disk. By default, this parameter is set to Delete. You can also set this parameter to Retain. - Delete mode: When PVCs are deleted, the related PVs and disks are deleted.
- Retain mode: When PVCs are deleted, the related PVs and disks are retained. The PVs and disk data can only be manually deleted.
If you require higher data security, we recommend that you use the Retain mode to avoid data loss caused by user errors.
volumeBindingMode The binding mode of the disk. Default value: Immediate. You can also set the value to WaitForFirstConsumer. - Immediate: creates a disk when the PVC that uses the StorageClass is created. This way, a disk is created and provisioned before you create a pod that uses the disk.
- WaitForFirstConsumer: delays the binding and provisioning of a PV 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 If you set this parameter to true, the disk can be automatically expanded. - Run the following command to create a StorageClass:
kubectl apply -f storage-class-topology.yaml
- View the created StorageClass.
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose Volumes > StorageClasses. You can view the created StorageClass on the StorageClasses page.
Method 2: Create a StorageClass that specifies the WaitForFirstConsumer mode
- Use the following template to create a storage-class-csi.yaml file:
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: alicloud-disk-ssd-b provisioner: diskplugin.csi.alibabacloud.com parameters: type: cloud_ssd regionId: cn-beijing zoneId: cn-beijing-b encrypted: false reclaimPolicy: Retain allowVolumeExpansion: true volumeBindingMode: Immediate
Parameter Description provisioner Set this parameter to diskplugin.csi.alibabacloud.com
. This indicates that the provisioner plug-in for Alibaba Cloud disks is used to create the StorageClass.type The type of disk. The supported values include cloud_efficiency, cloud_ssd, cloud_essd, and available. You can specify one or more values. However, if you specify available, you cannot specify other values. For example, you can set this parameter to type: cloud_efficiency, cloud_ssd, cloud_essd
. This indicates that the system attempts to create a disk of the specified types in sequence. The system stops trying once a disk is created. If you set this parameter to available, the system first attempts to create a standard SSD. If the attempt fails, the system attempts to create an ultra disk. The system stops trying once a disk is created.Note Some ECS instance types do not support enhanced SSDs. For more information, see FAQ.regionId Optional. The region where you want to create a disk. zoneId Optional. The zone where you want to create a disk. encrypted Optional. This parameter specifies whether the disk is encrypted. Default value: false. This indicates that the created disk is not encrypted. reclaimPolicy The reclaim policy of the disk. By default, this parameter is set to Delete. You can also set this parameter to Retain. - Delete mode: When PVCs are deleted, the related PVs and disks are deleted.
- Retain mode: When PVCs are deleted, the related PVs and disks are retained. The PVs and disk data can only be manually deleted.
If you require higher data security, we recommend that you use the Retain mode to avoid data loss caused by user errors.
allowVolumeExpansion If you set this parameter to true, the disk can be automatically expanded. volumeBindingMode The binding mode of the disk. Default value: Immediate. You can also set the value to WaitForFirstConsumer. - Immediate: creates a disk when the PVC that uses the StorageClass is created. This way, a disk is created and provisioned before you create a pod that uses the disk.
- WaitForFirstConsumer: delays the binding and provisioning of a PV 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.
- Run the following command to create a StorageClass:
kubectl apply -f storage-class-csi.yaml
- View the created StorageClass.
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, click Clusters.
- On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
- In the left-side navigation pane of the details page, choose Volumes > StorageClasses. You can view the created StorageClass on the StorageClasses page.
Step 2: Create a PVC
- Use the following template to create a pvc-ssd.yaml file:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: disk-pvc spec: accessModes: - ReadWriteOnce volumeMode: Filesystem resources: requests: storage: 25Gi storageClassName: alicloud-disk-ssd
Parameter Description name The name of the PVC. accessModes The access mode of the PVC. volumeMode Optional. The volume mode of the disk. Valid values: Filesystem and Block. Default value: Filesystem. storageClassName The name of the StorageClass that you want to associate with the PVC. storage The disk size claimed by the PVC. The minimum capacity is 20 GiB. - Run the following command to create a PVC:
kubectl create -f pvc-ssd.yaml
- View the created PVC. In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims. You can view the created PVC on the Persistent Volume Claims page.
Step 3: Create an application
- Create a pvc-dynamic.yaml file.
Use the following template to create an application named nginx-dynamic and mount the PVC to the application.
apiVersion: apps/v1 kind: StatefulSet metadata: name: nginx-dynamic spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 name: web volumeMounts: - name: pvc-disk mountPath: /data volumes: - name: pvc-disk persistentVolumeClaim: claimName: disk-ssd
Parameter Description mountPath The path in which the disk is mounted. claimName The name of the PVC mounted to the application. - Run the following command to create an application that has the PVC mounted.
kubectl create -f pvc-dynamic.yaml
- View the deployed application. Go to the ACK console. In the left-side navigation pane of the cluster details page, choose Workloads > StatefulSets. You can view the created application on the StatefulSets page.
Verify that the dynamically provisioned disk volume can be used to persist data
Data is persisted in the disk. After a pod is deleted and recreated, the disk data is the same as before the pod is deleted.
Perform the following steps to verify that data is persisted to the disk.
- View the pod that runs the nginx-dynamic application and the files in the disk.
- Run the following command to query the pod that runs the nginx-dynamic application:
kubectl get pod | grep dynamic
Expected output:
nginx-dynamic-1**** 1/1 Running 0 3m
- Run the following command to check whether a new disk is mounted to the /data path:
kubectl exec nginx-dynamic-1**** df | grep data
Expected output:
/dev/vdh 20511312 45080 20449848 1% /data
- Run the following command to query files in the /data path:
kubectl exec nginx-dynamic-1**** ls /data
Expected output:
lost+found
- Run the following command to query the pod that runs the nginx-dynamic application:
- Create a file in the disk.
- Run the following command to create a dynamic file in the /data path:
kubectl exec nginx-dynamic-1**** touch /data/dynamic
- Run the following command to query files in the /data path:
kubectl exec nginx-dynamic-1**** ls /data
Expected output:
dynamic lost+found
- Run the following command to create a dynamic file in the /data path:
- Run the following command to delete the pod named
nginx-dynamic-1****
:kubectl delete pod nginx-dynamic-1****
Expected output:
pod "nginx-dynamic-1****" deleted
- Verify that the file still exists in the disk after the pod is deleted.
- Run the following command to query the pod that is recreated:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE nginx-dynamic-2**** 1/1 Running 0 2m
- Run the following command to query files in the /data path:
kubectl exec nginx-dynamic-2**** ls /data
Expected output:
dynamic lost+found
The dynamic file still exists in the /data path. This indicates that data is persisted to the dynamically provisioned disk.
- Run the following command to query the pod that is recreated: