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

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.

How to use a dynamically provisioned disk 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

  1. Log on to the ACK console.
  2. In the left-side navigation pane of the ACK console, click Clusters.
  3. Log on to the ACK console.
  4. In the left-side navigation pane of the details page, choose Volumes > StorageClasses.
  5. On the StorageClasses page, click Create in the upper-right corner.
  6. 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 can contain lowercase letters, digits, periods (.), and hyphens (-). It must start with a lowercase letter.

    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 Elastic Block Storage 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 prevent 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.
    • The 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.
  7. Click Create.
    After the StorageClass is created, you can view it in the StorageClass list.

Step 2: Create a PVC

  1. In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims.
  2. In the upper-right corner of the Persistent Volume Claims page, click Create.
  3. In the Create PVC dialog box, set the 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 created and click Select in the Actions column.
    Capacity The capacity of the PV that you want to create.
    Access Mode Default value: ReadWriteOnce. You can also set the value to ReadOnlyMany or ReadWriteMany.
  4. 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

  1. In the left-side navigation pane of the details page, choose Workloads > StatefulSets.
  2. In the upper-right corner of the StatefulSets page, click Create from Image.
  3. 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. Data storage
  4. 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.

  1. 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
      regionId: cn-beijing
      zoneId: cn-beijing-a
      fstype: ext4
      readonly: "true"
      mkfsOptions: "-O project,quota"
      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 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 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 Elastic Block Storage FAQ.
    regionId The ID of the region where you want to create a disk. Set the value to the region ID of the cluster.
    zoneId The ID of the zone where you want to create a disk.
    • 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 The file system of the disk. Default value: ext4.
    readonly Specifies whether the disk is read-only. Valid values: true and false. true: The disk is read-only. false: You can perform read and write operations on the disk. Default value: false.
    mkfsOptions The parameter that is specified to format the disk. Example: mkfsOptions: "-O project,quota".
    diskTags The custom tags of the disk. Example: diskTags: "a:b,b:c".
    encrypted Specifies whether to encrypt the created disk. Default value: false. This indicates that the created disk is not encrypted.
    performanceLevel Valid values: PL1, PL2, and PL3 For more information, see Capacity and PLs.
    reclaimPolicy The reclaim policy of the disk. Default value: 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 prevent 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.
  2. Run the following command to create a StorageClass:
    kubectl apply -f storage-class-topology.yaml
  3. View the created StorageClass.
    1. Log on to the ACK console.
    2. In the left-side navigation pane of the ACK console, click Clusters.
    3. 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.
    4. 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

  1. 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 Elastic Block Storage FAQ.
    regionId The region where you want to create a disk.
    zoneId The zone where you want to create a disk.
    encrypted Specifies whether to encrypt the created disk. 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 prevent 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.
  2. Run the following command to create a StorageClass:
    kubectl apply -f storage-class-csi.yaml
  3. View the created StorageClass.
    1. Log on to the ACK console.
    2. In the left-side navigation pane of the ACK console, click Clusters.
    3. 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.
    4. 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

  1. 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 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.
  2. Run the following command to create a PVC:
    kubectl create -f pvc-ssd.yaml
  3. 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

  1. 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
    • mountPath: the path where the disk is mounted.
    • claimName: the name of the PVC that is mounted to the application.
  2. Run the following command to deploy the application and mount the PVC to the application:
    kubectl create -f nginx-dynamic.yaml
  3. View the deployed application.
    In the left-side navigation pane of the details page, choose Workloads > StatefulSets. You can find 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.

  1. View the pod that runs the nginx-dynamic application and the files in the disk.
    1. 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
    2. 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
    3. Run the following command to query files in the /data path:
      kubectl exec nginx-dynamic-1**** ls /data

      Expected output:

      lost+found
  2. Create a file in the disk.
    1. Run the following command to create a dynamic file in the /data path:
      kubectl exec nginx-dynamic-1**** touch /data/dynamic
    2. Run the following command to query files in the /data path:
      kubectl exec nginx-dynamic-1**** ls /data

      Expected output:

      dynamic
      lost+found
  3. 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
  4. Verify that the file still exists in the disk after the pod is deleted.
    1. 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
    2. 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.