All Products
Search
Document Center

Container Compute Service:Mount a dynamically provisioned disk volume

Last Updated:Dec 17, 2024

If your applications require high IOPS and low latency but do not require data sharing, you can mount disks as persistent volumes (PVs) to your applications. This topic describes how to mount a dynamically provisioned disk volume to an application and check whether the disk volume can be used to persist data.

Background information

Disks are block-level storage devices that use a distributed multi-replica mechanism to ensure low latency, high performance, high durability, and high reliability. Disks are suitable for applications that require high IOPS and low latency but do not require data sharing. For more information, see Storage overview.

Note

Alibaba Cloud Container Compute Service (ACS) supports only dynamically provisioned disk volumes. ACS does not support statically provisioned disk volumes.

Prerequisites

The latest version of managed-csiprovisioner is installed in your Alibaba Cloud Container Compute Service (ACS) cluster.

Note

Go to the ACS cluster management page in the ACS console. In the left-side navigation pane of the cluster management page, choose Operations > Add-ons. On the Storage tab, you can check whether managed-csiprovisioner is installed.

Limits

  • Disks cannot be shared. A disk can be mounted only to one pod.

  • You can mount a disk only to a pod that resides in the same zone as the disk.

Usage notes

  • We recommend that you mount disks to pods or StatefulSets instead of Deployments.

    Note

    A disk can be mounted only to one pod. If you want to mount a disk to a Deployment, you must set the number of pod replicas to one for the Deployment. If you configure multiple pods for the Deployment, you cannot mount a separate disk volume to each pod. In addition, you cannot specify the volume mount and unmount priorities of pods. When you restart the pod of a Deployment, the disk may fail to be mounted to the restarted pod due to the update policy used by Deployments. We recommend that you do not mount disks to Deployments.

  • When you mount dynamically provisioned disk volumes, the disks that are automatically created use the pay-as-you-go billing method.

  • When you mount a dynamically provisioned disk volume to an application, if the application configuration includes the securityContext.fsgroup parameter, ACS automatically runs the chmod and chown commands after the volume is mounted, which may slow down the volume mount process.

    Note

    After you add the securityContext.fsgroup parameter to the application configuration, ACS automatically modifies the ownership of files in the volume when mounting the disk to the application. The time required for ownership modification depends on the number of files in the volume. If a large number of files exist in the volume, the modification process may be time-consuming. You can set the fsGroupChangePolicy parameter in the pod configuration to OnRootMismatch. This way, ACS modifies file ownership only when the pod is started for the first time. When you update or recreate the pod after creation, the volume mount process does not include ownership modification. If the preceding setting does not meet your business requirements, we recommend that you create init containers and grant the init containers the permissions to perform the relevant operations.

Mount a dynamically provisioned disk volume

kubectl

Step 1: Create a StorageClass

ACS provides a default StorageClass named alicloud-disk-topology-alltype. This StorageClass can be used to provision a PV of the following types in descending order of priority: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If the default StorageClass does not meet your business requirements, perform the following steps to create a custom StorageClass:

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

  2. Create a file named disk-sc.yaml and copy the following template content into the file. Modify the parameters in the template based on your business requirements.

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

    The following table describes the parameters.

    Parameter

    Description

    type

    The category of the disk. Valid values:

    • cloud_essd_entry: ESSD Entry disk.

    • cloud_auto: ESSD AutoPL disk.

    • cloud_essd: Enterprise SSD (ESSD). This is the default value.

    • cloud_ssd: standard SSD.

    • cloud_efficiency: ultra disk.

    You can set this parameter to any combination of the preceding values. For example, you can set this parameter to type: cloud_efficiency, cloud_ssd, cloud_essd. In this case, the system tries to create a disk of a specified category in sequence. The system stops trying once a disk is created.

    Note

    You can select disk categories based on your requirements for billing and performance. For more information, see Prices of block storage devices and Block storage performance.

    fstype

    The file system type of the disk. Default value: ext4. Valid values: ext3, ext4, and xfs.

    performanceLevel

    The performance level (PL) of the ESSD. Default value: PL1. Valid values: PL0, PL1, PL2, and PL3. For more information, see ESSDs.

    provisioner

    The type of the driver. In this example, the parameter is set to diskplugin.csi.alibabacloud.com, which indicates that the Container Storage Interface (CSI) plug-in provided by Alibaba Cloud is used.

    reclaimPolicy

    The reclaim policy of the disk. You must set the value to Delete, which indicates that the PV and disk are automatically deleted when the persistent volume claim (PVC) is deleted.

    volumeBindingMode

    The binding mode of the disk. You must set the value to WaitForFirstConsumer, which indicates that the binding and provisioning of a PV is delayed 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

    Specifies whether to automatically resize the disk.

  3. Create a StorageClass.

    kubectl create -f disk-sc.yaml
  4. Check the StorageClass.

    kubectl get sc

    Expected 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

  1. Create a file named disk-pvc.yaml and copy the following content to the file:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: disk-pvc
    spec:
      accessModes:
      - ReadWriteOncePod
      volumeMode: Filesystem
      resources:
        requests:
          storage: 20Gi
      storageClassName: alicloud-disk-essd

    The following table describes the parameters.

    Parameter

    Description

    accessModes

    The access mode. You must set the value to ReadWriteOncePod.

    volumeMode

    The mode of the volume. Valid values:

    • Filesystem: the file system mode. This is the default value.

    • Block: block gateway: the block device mode.

    storage

    Specify the storage capacity that you want to allocate to the pod. This is also the capacity of the disk.

    storageClassName

    The name of the StorageClass that you want to associate with the PVC.

  2. Create a PVC.

    kubectl create -f disk-pvc.yaml
  3. Check the PVC.

    kubectl get pvc

    The following output shows that no PV is bound to the PVC because the volumeBindingMode parameter is set to WaitForFirstConsumer.

    NAME       STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS         VOLUMEATTRIBUTESCLASS   AGE
    disk-pvc   Pending                                      alicloud-disk-essd   <unset>                 7s

Step 3: Create an application and mount a disk to the application

  1. Create a file named disk-test.yaml and copy the following content to the file.

    The following code block specifies the configuration of a StatefulSet that provisions one pod. The pod applies for storage resources by using the disk-pvc PVC, which is mounted to the /data path of the pod.

    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
  2. Create a StatefulSet and mount a disk to the StatefulSet.

    kubectl create -f disk-test.yaml
    Important

    During the deployment process of the StatefulSet, the system automatically creates a pay-as-you-go disk and PV based on the PVC and StorageClass specified in the StatefulSet configuration.

  3. Check whether the pod provisioned by the StatefulSet is deployed.

    kubectl get pod | grep disk-test

    The following output shows that one pod is deployed for the StatefulSet.

    disk-test-0   1/1     Running   0          52s
  4. Check the PVC.

    kubectl get pvc

    The following output shows that a PV of the disk type is automatically created and bound to the PVC.

    NAME       STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS         VOLUMEATTRIBUTESCLASS   AGE
    disk-pvc   Bound    d-uf698s3h14isyj6b****   20Gi       RWOP           alicloud-disk-essd   <unset>                 111s
  5. View files in the mount path to check whether the disk is mounted.

    kubectl exec disk-test-0 -- df | grep data

    Expected output:

    /dev/vdb        20466256      24  20449848   1% /data

Console

Step 1: Create a StorageClass

ACS provides a default StorageClass named alicloud-disk-topology-alltype. This StorageClass can be used to provision a PV of the following types in descending order of priority: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If the default StorageClass does not meet your business requirements, perform the following steps to create a custom StorageClass:

  1. Log on to the ACS console.

  2. On the Clusters, click the ID of the cluster to go to the cluster management page.

  3. In the left-side navigation pane of the cluster management page, choose Volumes > StorageClasses.

  4. Create a StorageClass.

    1. On the StorageClasses page, click Create.

    2. In the Create dialog box, configure the parameters and click Create.

      Parameter

      Description

      Example

      Name

      Enter a custom name for the StorageClass. The name must meet the format requirements displayed in the console.

      alicloud-disk-essd

      PV Type

      Select Cloud Disk.

      Cloud Disk

      Parameter

      By default, the type parameter is added. This parameter specifies the disk category. Valid values:

      • cloud_essd_entry: ESSD Entry disk.

      • cloud_auto: ESSD AutoPL disk.

      • cloud_essd: Enterprise SSD (ESSD). This is the default value.

      • cloud_ssd: standard SSD.

      • cloud_efficiency: ultra disk.

      You can set this parameter to any combination of the preceding values. For example, you can set this parameter to type: cloud_efficiency, cloud_ssd, cloud_essd. In this case, the system tries to create a disk of a specified category in sequence. The system stops trying once a disk is created.

      Note

      You can select disk categories based on your requirements for billing and performance. For more information, see Prices of block storage devices and Block storage performance.

      You can add the following parameters:

      • fstype

        The file system type of the disk. Default value: ext4. Valid values: ext3, ext4, and xfs.

      • performanceLevel

        The performance level (PL) of the ESSD. Default value: PL1. Valid values: PL0, PL1, PL2, and PL3. For more information, see ESSDs.

      type:cloud_essd

      Reclaim Policy

      The reclaim policy of the disk. You must set the value to Delete, which indicates that the PV and disk are automatically deleted when the persistent volume claim (PVC) is deleted.

      Delete

      Binding Mode

      The binding mode of the disk. You must set the value to WaitForFirstConsumer, which indicates that the binding and provisioning of a PV is delayed 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.

      WaitForFirstConsumer

Step 2: Create a PVC

  1. In the left-side navigation pane of the cluster management page, choose Volumes > Persistent Volume Claims.

  2. On the Persistent Volume Claims page, click Create.

  3. In the Create PVC dialog box, configure the parameters and click Create.

    Parameter

    Description

    Example

    PVC Type

    Select Cloud Disk.

    Cloud Disk

    Name

    Enter a custom name for the PVC. The name must meet the format requirements displayed in the console.

    disk-pvc

    Allocation Mode

    By default, Use StorageClass is selected.

    Use StorageClass

    Existing Storage Class

    Select the StorageClass that you want to associate with the PVC.

    alicloud-disk-essd

    Capacity

    Specify the storage capacity that you want to allocate to the pod. This is also the capacity of the disk.

    20Gi

    Access Mode

    You must select ReadWriteOnce, which indicates that the volume is mounted only to one pod in read-write mode.

    ReadWriteOnce

    After you create the PVC, you can view the PVC on the Persistent Volume Claims page. The PVC is in the Pending state and no PV is mounted to the PVC because the WaitForFirstConsumer binding mode is specified in the StorageClass associated with the PVC.

Step 3: Create an application and mount a disk to the application

  1. In the left-side navigation pane of the cluster management page, choose Workloads > StatefulSets.

  2. In the upper-right corner of the StatefulSets page, click Create from Image.

  3. Configure the parameters of the StatefulSet and click Create.

    The following table describes some of the parameters. Use the default values for other parameters. For more information, see Use a StatefulSet to create a stateful application.

    Wizard page

    Parameter

    Description

    Example

    Basic Information

    Name

    Enter a custom name for the StatefulSet. The name must meet the format requirements displayed in the console.

    disk-test

    Replicas

    The number of pod replicas provisioned by the StatefulSet.

    1

    Container

    Image Name

    The address of the image used to deploy the application.

    registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest

    Required Resources

    Specify the number of vCores and the amount of memory required by the application.

    0.25 vCores and 0.5 GiB of memory

    Volume

    Click Add PVC and configure the parameters.

    • Mount Source: Select the PVC you created.

    • Container Path: Specify the container path to which you want to mount the disk.

    • Mount Source: disk-pvc.

    • Container Path: /data.

  4. Check whether the application is deployed.

    1. On the StatefulSets page, click the name of the application you created.

    2. On the Pods tab, check whether the pod is in the Running state.

  5. Check the PV and PVC.

    • On the Persistent Volumes page, you can view the PV created by the system. The PV is named after the ID of the mounted disk.

    • On the Persistent Volume Claims page, you can view that the PVC is in the Bound state and the preceding PV is bound to the PVC.

Check whether data persistence is enabled based on the disk

The StatefulSet created in the preceding example provisions one pod and a disk is mounted to the pod. If you delete the pod, the system automatically recreates the pod. The original disk is mounted to the new pod and data still exists on the disk. Perform the following steps to test whether data is persisted to the disk:

  1. View files in the mount path to check whether files on the disk can be viewed.

    kubectl exec disk-test-0 -- ls /data

    Expected output:

    lost+found
  2. Create a file on the disk.

    kubectl exec disk-test-0 -- touch /data/test
  3. Delete the pod.

    kubectl delete pod disk-test-0
    Note

    After you delete the pod, the system automatically recreates the pod.

  4. Check the new pod.

    kubectl get pod

    The following output shows that the new pod has the same name as the pod you deleted.

    NAME          READY   STATUS    RESTARTS   AGE
    disk-test-0   1/1     Running   0          27s
  5. Check whether the original disk is mounted to the pod and the file still exists on the disk.

    kubectl exec disk-test-0 -- ls /data

    The following output shows that the test file still exists on the disk.

    lost+found  test