Alibaba Cloud disks are block-level data storage resources for Elastic Compute Service (ECS). Alibaba Cloud disks provide low latency, high performance, high durability, and high reliability. Serverless Kubernetes (ASK) 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

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. 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.
  5. In the upper-right corner of the StorageClasses page, click Create.
  6. In the Create dialog box, configure 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.
    Volume Plug-in Select CSI.
    Parameter By default, a type parameter is added and set to cloud_essd. The type parameter specifies the disk types. The valid values of the type parameter are 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 if 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 if 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 persistent volume claims (PVCs) are deleted, the related persistent volumes (PVs) and disks are also 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, which indicates that the system creates a disk before it creates a pod.
  7. After you set the parameters, click Create.
    You can find the created StorageClass in the StorageClasses 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, configure the following parameters.
    Parameter Description
    PVC Type You can select Cloud Disk or NAS. 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 By default, this parameter is set to ReadWriteOnce.
  4. Click Create.
    After the PVC is created, you can view the PVC 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. Configure the application parameters.
    This example shows how to configure 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. volume
  4. Configure other parameters and click Create.
    After the application is created, you can use the volume to store application data.

Use a dynamically provisioned disk volume by using kubectl

Step 1: Create a StorageClass

If your cluster is deployed across multiple zones, you can create a StorageClass that requires the system to create a disk before it creates a pod.

  1. Use the following template to create a file named storage-class-csi.yaml:
    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 valid values are 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 if 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 if 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 also 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, which indicates that the system creates a disk before it creates a pod.
  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 file named pvc-ssd.yaml:
    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.
  2. Run the following command to create the 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 file named pvc-dynamic.yaml.

    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 to which the disk is mounted.
    claimName The name of the PVC that is mounted to the application.
  2. Run the following command to create an application and mount the PVC to the application:
    kubectl create -f pvc-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.