All Products
Search
Document Center

Container Service for Kubernetes:Use a dynamically provisioned disk volume

Last Updated:Dec 19, 2023

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

Procedure

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

    Select Cloud Disk.

    Volume Plug-in

    Select CSI.

    Parameter

    By default, the type parameter is added and set to cloud_essd. The type parameter specifies the disk types. Valid values: 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, if you specify type: cloud_efficiency, cloud_ssd, cloud_essd, 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 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. 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 mount mode of the disk. Default value: Immediate, which specifies that the system creates a disk before it creates a pod.

  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, 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
      encrypted: "false"
    reclaimPolicy: Retain
    allowVolumeExpansion: true
    volumeBindingMode: Immediate

    Parameter

    Description

    provisioner

    Set the value to diskplugin.csi.alibabacloud.com. This specifies 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, if you 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 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 once a disk is created.

    Note

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

    encrypted

    Optional. This parameter specifies whether the disk is encrypted. Default value: false. This specifies 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 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, which specifies 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. 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. 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-b

    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-ssd.yaml
  3. View the created PVC.

    In the left-side navigation pane of the cluster 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-pvc

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