Use a disk as a dynamic volume

更新时间:
复制 MD 格式

Mount a dynamic disk volume to an application and verify its data persistence.

Background

Disks are block-level storage products that use a distributed, multi-replica mechanism for low latency, high performance, durability, and reliability. They are well suited to non-shared data with demanding I/O and latency requirements. For more information, see Storage overview.

Note

ACS supports only dynamic mounting of disk volumes; static mounting is not supported.

Prerequisites

The managed-csiprovisioner component is installed in the 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.

Limitations

  • A disk is non-shared storage and can be mounted to only a single Pod.

  • A disk can be mounted only to a Pod in the same availability zone.

Usage notes

  • We recommend mounting a disk to a StatefulSet or a standalone Pod, not to a Deployment.

    Note

    Because a disk can be mounted to only one Pod, you cannot configure independent volumes for each Pod in a Deployment. Additionally, a Deployment's upgrade strategy can prevent a new Pod from mounting the disk during a restart.

  • Disks created dynamically are billed on a pay-as-you-go basis.

  • If you configure securityContext.fsgroup in your application's YAML file, ACS runs chmod and chown operations after the mount completes, which increases mount time.

    Note

    When securityContext.fsgroup is configured, file ownership in the volume is adjusted during mount. Depending on the number of files, this can significantly increase startup time. Set fsGroupChangePolicy to OnRootMismatch to change ownership only on the first container start; subsequent Pod upgrades or rebuilds are unaffected. If this does not meet your needs, use an initContainer to implement custom permission logic.

Mount a disk volume

kubectl

Step 1: Create a StorageClass

ACS provides a default StorageClass named alicloud-disk-topology-alltype that creates a persistent volume (PV) by trying the following disk types in order: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If this default does not meet your requirements, create a new StorageClass as follows.

  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. Save the following YAML content as disk-sc.yaml. Modify the parameters as described in the table below.

    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

    parameters.type

    The disk type. Valid values:

    • cloud_essd_entry: ESSD Entry disk

    • cloud_auto: ESSD AutoPL disk

    • cloud_essd (default): ESSD

    • cloud_ssd: standard SSD

    • cloud_efficiency: ultra disk

    You can combine these types. For example: type: cloud_efficiency, cloud_ssd, cloud_essd. The system tries each type in order until a disk is created successfully.

    Note

    You can choose a disk type based on your billing and performance needs. For more information, see Block Storage Pricing and Block storage performance.

    parameters.fstype

    The file system type. Default: ext4. Valid values: ext3, ext4, and xfs.

    parameters.performanceLevel

    The ESSD performance level. Default: PL1. Valid values: PL0, PL1, PL2, and PL3. For more information, see ESSDs.

    provisioner

    The storage driver. Set this to diskplugin.csi.alibabacloud.com to use the Alibaba Cloud disk CSI plugin.

    reclaimPolicy

    The reclaim policy for the disk. Only Delete is supported, meaning the PV and underlying disk are deleted when the persistent volume claim (PVC) is deleted.

    volumeBindingMode

    The volume binding mode. We recommend WaitForFirstConsumer for multi-zone clusters.

    • WaitForFirstConsumer: Delays volume binding. The Pod is scheduled first, then the disk is created in the Pod's availability zone.

    • Immediate: Creates the disk before the Pod is scheduled.

    Important

    If you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use nodeSelector or ResourcePolicy to schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod label (alibabacloud.com/acs: "true"), StorageClasses with the WaitForFirstConsumer type are not supported.

    allowVolumeExpansion

    Whether to allow automatic disk expansion.

  3. Create the StorageClass.

    kubectl create -f disk-sc.yaml
  4. View 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. Save the following YAML content as disk-pvc.yaml.

    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. Only ReadWriteOncePod is supported.

    volumeMode

    The volume mode. Valid values:

    • Filesystem (default): a file system

    • Block: a block device

    storage

    The storage capacity to allocate to the Pod, which determines the size of the disk to be created.

    storageClassName

    The name of the StorageClass to use.

  2. Create the PVC.

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

    kubectl get pvc

    The expected output shows that the PVC is not yet bound to a PV because volumeBindingMode 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 disk

  1. Save the following YAML content as disk-test.yaml.

    The following YAML creates a StatefulSet with one Pod. The Pod requests storage through a PVC named disk-pvc and mounts the volume at /data.

    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 the StatefulSet and mount the disk.

    kubectl create -f disk-test.yaml
    Important

    The system automatically provisions a pay-as-you-go disk and a corresponding PV based on the PVC and StorageClass configurations.

  3. Check the deployment status of the Pod in the StatefulSet.

    kubectl get pod | grep disk-test

    The expected output shows one Pod because the replica count of the StatefulSet is 1.

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

    kubectl get pvc

    The expected output shows that the PVC is now bound to the automatically created PV.

    NAME       STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS         VOLUMEATTRIBUTESCLASS   AGE
    disk-pvc   Bound    d-uf698s3h14isyj6b****   20Gi       RWOP           alicloud-disk-essd   <unset>                 111s
  5. Check the mount path to confirm that 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 StorageClass

ACS provides a default StorageClass named alicloud-disk-topology-alltype that creates a persistent volume (PV) by trying the following disk types in order: cloud_essd (ESSD), cloud_ssd (standard SSD), and cloud_efficiency (ultra disk). If this default does not meet your requirements, create a new StorageClass as follows.

  1. Log on to the ACS console.

  2. On the Clusters, click the name 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 dialog box that appears, configure the parameters and then click Create.

      Parameter

      Description

      Example

      Name

      The name of the StorageClass. Follow the format requirements on the screen.

      alicloud-disk-essd

      PV Type

      Select Disk.

      Disk

      Parameter

      The default parameter is type, which specifies the disk type. Valid values:

      • cloud_essd_entry: ESSD Entry disk

      • cloud_auto: ESSD AutoPL disk

      • cloud_essd (default): ESSD

      • cloud_ssd: standard SSD

      • cloud_efficiency: ultra disk

      You can combine these types. For example: type: cloud_efficiency, cloud_ssd, cloud_essd. The system tries each type in order until a disk is created successfully.

      Note

      You can choose a disk type based on your billing and performance needs. For more information, see Block Storage Pricing and Block storage performance.

      You can add the following parameters:

      • fstype

        The file system type. Default: ext4. Valid values: ext3, ext4, and xfs.

      • performanceLevel

        The ESSD performance level. Default: PL1. Valid values: PL0, PL1, PL2, and PL3. For more information, see ESSDs.

      type: cloud_essd

      Reclaim Policy

      The reclaim policy for the disk. Only Delete is supported, meaning the PV and underlying disk are deleted when the persistent volume claim (PVC) is deleted.

      Delete

      Binding Mode

      The volume binding mode. We recommend WaitForFirstConsumer for multi-zone clusters.

      • WaitForFirstConsumer: Delays volume binding. The Pod is scheduled first, then the disk is created in the Pod's availability zone.

      • Immediate: Creates the disk before the Pod is scheduled.

      Important

      If you use ACS computing power in an ACK cluster and the ACS Pod needs to mount a disk, use nodeSelector or ResourcePolicy to schedule the Pod to a virtual node. If you schedule the Pod by adding the Pod label (alibabacloud.com/acs: "true"), StorageClasses with the WaitForFirstConsumer type are not supported.

      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 dialog box that appears, configure the parameters and then click Create.

    Parameter

    Description

    Example

    PVC Type

    Select Disk.

    Disk

    Name

    The name of the PVC. Follow the format requirements on the screen.

    disk-pvc

    Allocation Mode

    By default, Use StorageClass is selected.

    Use StorageClass

    Existing Storage Class

    Select the StorageClass to use.

    alicloud-disk-essd

    Capacity

    The storage capacity to allocate to the Pod, which determines the size of the disk to be created.

    20Gi

    Access Mode

    Only ReadWriteOncePod is supported, which means the volume can be mounted as read-write by a single Pod.

    ReadWriteOncePod

    After the PVC is created, it appears on the Persistent Volume Claims page. Because the binding mode in the StorageClass is WaitForFirstConsumer, the PVC is not yet bound to a PV and its status is Pending.

Step 3: Create an application and mount disk

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

  2. On the StatefulSets page, click Create from Image.

  3. Configure the parameters for the StatefulSet, and then click Create.

    Set the following parameters and keep the defaults for the rest. For more information, see Create a StatefulSet workload.

    Configuration section

    Parameter

    Description

    Example

    Basic Information

    Application Name

    The name of the StatefulSet. Follow the format requirements on the screen.

    disk-test

    Replicas:

    The number of replicas for the StatefulSet.

    1

    Container

    Image Name

    The container image address for the application.

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

    Required Resources

    The requested vCPU and memory resources.

    0.25 vCPU, 0.5 GiB

    Volume

    Click Add PVC and configure the parameters.

    • Mount Source: Select the PVC that you created.

    • Container Path: Enter the path in the container to mount the disk.

    • Mount Source: disk-pvc

    • Container Path: /data

  4. Check the application deployment status.

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

    2. On the Pods tab, confirm that the Pod is running properly (its status is Running).

  5. Check the volume and the claim.

    • On the Persistent Volumes page, you will find an automatically created PV. The name of the PV corresponds to the disk ID.

    • On the Persistent Volume Claims page, the PVC is now bound to the PV, and its status is Bound.

Verify disk data persistence

The StatefulSet created above contains one Pod with a mounted disk. When the Pod is deleted, a new Pod is automatically created and re-mounts the same disk, preserving the data. Follow these steps to verify data persistence.

  1. View the data on the disk at the mount path.

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

    Expected output:

    lost+found
  2. Write a test file to the disk.

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

    kubectl delete pod disk-test-0
    Note

    After you delete a Pod in a StatefulSet, the system automatically creates a new Pod.

  4. View the newly created Pod.

    kubectl get pod

    The output shows that the new Pod has the same name as the deleted one, which is standard StatefulSet behavior.

    NAME          READY   STATUS    RESTARTS   AGE
    disk-test-0   1/1     Running   0          27s
  5. Verify that the new Pod has re-mounted the disk and the data persists.

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

    The output shows that the test file written earlier is still on the disk.

    lost+found  test