All Products
Search
Document Center

Container Service for Kubernetes:Use a dynamically provisioned disk volume

Last Updated:Feb 23, 2024

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. 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.

Table of contents

Prerequisites

Usage notes

Category

Description

Use scenarios

If no disk is available when you deploy an application in an ACK cluster, the system automatically purchases a disk.

Usage

  • 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.

Procedure

You can use dynamically provisioned disk volumes in the following ways:

Use a dynamically provisioned disk volume in the ACK console

Step 1: Create a StorageClass

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Volumes > StorageClasses in the left-side navigation pane.

  3. In the upper-right corner of the StorageClasses page, click Create.

  4. In the Create dialog box, configure the parameters.

    The following table describes 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

    You can select Cloud Disk or NAS. In this example, Cloud Disk is selected.

    Parameter

    • By default, the 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_auto, 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. 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 (ESSDs). For more information, see FAQ about EBS.

    • You can add custom parameters. The following custom parameters are supported:

      • resourceGroupId: Optional. The resource group to which the disk belongs. Default value: "".

      • regionId: Optional. The ID of the region where the disk is created. The value must be set to the region ID of the cluster.

      • zoneId: Optional. The ID of the zone where the disk is 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: cn-beijing-a, cn-beijing-b.

      • fstype: Optional. The file system of the disk. Default value: ext4.

      • mkfsOptions: Optional. The parameters that are 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. Specify whether to encrypt the disk. The default value is false, which indicates that the disk is not encrypted.

      • performanceLevel: Optional. Valid values: PL0, PL1, PL2, and PL3. Default value: PL1. For more information, see Capacity and PLs.

      • volumeExpandAutoSnapshot: Optional. Specify whether to create a snapshot to back up the data stored in the disk before the disk is expanded. Default value: closed. Valid values:

        • forced: cancels the disk expansion task if the system fails to create the snapshot.

        • besteffort: generates a warning event and expands the disk if the system fails to create the snapshot.

        • closed: disables the auto snapshot feature.

        If you have high requirements for data security, we recommend that you set the parameter to forced to prevent data loss that may occur when the system fails to expand the disk.

        Note

        The auto snapshot feature supports only ESSDs. The snapshot is automatically deleted after the system expands the disk and the snapshot is retained for 24 hours if the system fails to expand the disk.

      • provisionedIops: Optional. The provisioned read/write IOPS of the disk.

        Valid values: 0 to min{50,000, 1,000 × Capacity - Baseline IOPS}.

        Baseline IOPS = min{1,800 + 50 × Capacity, 50,000}.

        Note

        The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

      • burstingEnabled: Optional. Specify whether to enable performance burst for the disk. Default value: false.

        • true: enables performance burst for the disk.

        • false: disables performance burst for the disk.

        Note

        The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

    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 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 have high requirements for data security, we recommend that you use the Retain policy to prevent data loss caused by user errors.

    Binding Mode

    The mount 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.

  5. After you complete the parameter configurations, 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, 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.

  4. Click Create.

    After the PVC is created, you can view it in the PVCs list. The PVC is bound to a PV.

Step 3: Deploy 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 you created 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.

    Volumes

  4. Set 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

You can create a StorageClass by using one of the following methods based on your business scenarios.

Method 1: Create a StorageClass in 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-wffc.yaml file.

  1. Create a file named storage-class-wffc.yaml and copy the following content to the file:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: alicloud-disk-wait-for-first-consumer
    provisioner: diskplugin.csi.alibabacloud.com
    parameters:
      type: cloud_auto,cloud_essd,cloud_ssd # The system attempts to create a disk of the specified types in sequence. The actual types of disks that are created depend on the instance types of the ECS instances to which the disks are mounted and the disk types supported by the zones where the ECS instances are deployed. 
      fstype: ext4
      diskTags: "a:b,b:c"
      encrypted: "false"
      performanceLevel: PL1
      volumeExpandAutoSnapshot: "forced" # This parameter takes effect only when the type parameter is set to cloud_essd. 
      provisionedIops: "40000"
      burstingEnabled: "false"
    volumeBindingMode: WaitForFirstConsumer
    reclaimPolicy: Retain
    allowVolumeExpansion: true

    Parameter

    Description

    metadata

    name

    The name of the StorageClass.

    provisioner

    Set the value to diskplugin.csi.alibabacloud.com. This indicates that the provisioner plug-in for Alibaba Cloud disks is used to create the StorageClass.

    parameters

    type

    The type of disk. The supported values include cloud_efficiency, cloud-auto, 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. 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 if a disk is created.

    Note

    Some ECS instance types do not support ESSDs. For more information, see FAQ about EBS.

    resourceGroupId

    Optional. The resource group to which the disk belongs. Default value: "".

    regionId

    Optional. The ID of the region where the disk is created. The value must be set to the region ID of the cluster.

    zoneId

    Optional. The ID of the zone where the disk is 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
    Important

    If you specify the zoneId parameter in a StorageClass that uses the default WaitForFirstConsumer mode, the zone ID generated in this mode does not take effect. Proceed with caution.

    fstype

    Optional. The file system of the disk. Default value: ext4.

    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: .

    encrypted

    Optional. Specify whether to encrypt the disk. The default value is false, which indicates that the disk is not encrypted.

    performanceLevel

    Valid values: PL0, PL1, PL2, and PL3. Default value: PL1. For more information, see Capacity and PLs.

    volumeExpandAutoSnapshot

    Optional. Specify whether to create a snapshot to back up the data stored in the disk before the disk is expanded. Default value: closed. Valid values:

    • forced: cancels the disk expansion task if the system fails to create the snapshot.

    • besteffort: generates a warning event and expands the disk if the system fails to create the snapshot.

    • closed: disables the auto snapshot feature.

    If you have high requirements for data security, we recommend that you set the parameter to forced to prevent data loss that may occur when the system fails to expand the disk.

    Note

    The auto snapshot feature supports only ESSDs. The snapshot is automatically deleted after the system expands the disk and the snapshot is retained for 24 hours if the system fails to expand the disk.

    provisionedIops

    Optional. The provisioned read/write IOPS of the disk.

    Valid values: 0 to min{50,000, 1,000 × Capacity - Baseline IOPS}.

    Baseline IOPS = min{1,800 + 50 × Capacity, 50,000}.

    Note

    The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

    burstingEnabled

    Optional. Specify whether to enable performance burst for the disk. Default value: false.

    • true: enables performance burst for the disk.

    • false: disables performance burst for the disk.

    Note

    The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

    volumeBindingMode

    The mount 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.

    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 have high requirements for data security, we recommend that you use the Retain policy to prevent data loss caused by user errors.

    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-wffc.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. Create a file named storage-class-immediate.yaml and copy the following content to the file:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: alicloud-disk-immediate
    provisioner: diskplugin.csi.alibabacloud.com
    parameters:
      type: cloud_essd,cloud_ssd,cloud_efficiency # The system attempts to create a disk of the specified types in sequence. The actual types of disks that are created depend on the instance types of the ECS instances to which the disks are mounted and the disk types supported by the zones where the ECS instances are deployed.
      regionId: cn-beijing
      zoneId: cn-beijing-b
      encrypted: "false"
    reclaimPolicy: Retain
    allowVolumeExpansion: true
    volumeBindingMode: Immediate

    Parameter

    Description

    provisioner

    Set the value to diskplugin.csi.alibabacloud.com. This indicates that the provisioner plug-in for Alibaba Cloud disks is used to create the StorageClass.

    parameters

    type

    The type of disk. The supported values include cloud_efficiency, cloud-auto, 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. 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 if a disk is created.

    Note

    Some ECS instance types do not support ESSDs. For more information, see FAQ about EBS.

    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.

    provisionedIops

    Optional. The provisioned read/write IOPS of the disk.

    Valid values: 0 to min{50,000, 1,000 × Capacity - Baseline IOPS}.

    Baseline IOPS = min{1,800 + 50 × Capacity, 50,000}.

    Note

    The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

    burstingEnabled

    Optional. Specify whether to enable performance burst for the disk. Default value: false.

    • true: enables performance burst for the disk.

    • false: disables performance burst for the disk.

    Note

    The parameter is supported only by ESSD AutoPL disks. For more information, see Specifications of ESSD AutoPL disks.

    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 high 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 mount 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-immediate.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. Create a file named pvc-disk.yaml and copy the following content to the file:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: disk-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      volumeMode: Filesystem
      resources:
        requests:
          storage: 25Gi
      storageClassName: alicloud-disk-wait-for-first-consumer 

    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 a PVC:

    kubectl create -f pvc-disk.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 mysql.yaml and copy the following content to the file:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: mysql
    spec:
      selector:
        matchLabels:
          app: mysql
      serviceName: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql
            image: mysql:5.7
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
            ports:
            - containerPort: 80
              name: mysql
            volumeMounts:
            - name: pvc-disk
              mountPath: /data
          volumes:
            - name: pvc-disk
              persistentVolumeClaim:
                claimName: disk-pvc
    ---
    apiVersion: v1
    kind: Secret
    metadata:
      name: mysql-pass
    type: Opaque
    data:
      username: dGVz****             
      password: dGVzdDEt****     

    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 deploy the application and mount the PVC to the application:

    kubectl create -f mysql.yaml
  3. View the created 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 MySQL application and the files in the disk.

    1. Run the following command to query the pod that runs the MySQL application:

      kubectl get pod | grep mysql

      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 of the pod:

      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 of the pod:

      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 file named MySQL in the /data path:

      kubectl exec nginx-dynamic-1**** -- touch /data/mysql
    2. Run the following command to query files in the /data path of one pod:

      kubectl exec nginx-dynamic-1**** -- ls /data

      Expected output:

      mysql
      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 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 of one pod:

      kubectl exec nginx-dynamic-2**** -- ls /data

      Expected output:

      mysql
      lost+found

      The MySQL file still exists in the /data path. This indicates that data is persisted to the dynamically provisioned disk.