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 statically and dynamically provision disk volumes. This topic describes how to use the CSI plug-in to mount a statically provisioned disk volume and how to enable persistent storage by using a statically provisioned disk volume.

Prerequisites

  • An ASK cluster is created. For more information, see Create an ASK cluster.
  • A pay-as-you-go disk is created. The disk ID is d-wz92s6d95go6ki9x****. For more information, see Create a disk.
    Note The minimum capacity of an ultra disk, an SSD, or an Enhanced SSD (ESSD) is 20 GiB.
  • A kubectl client is connected to your cluster. For more information, see Use kubectl to connect to an ASK cluster.

Background information

Scenarios:
  • You want to create applications that require high disk I/O and do not require data sharing. The applications can use storage services such as MySQL and Redis.
  • You want to write log data at a high speed in high concurrency or heavy traffic loads scenarios.
  • You want to persist data in a way that is independent of the pod lifecycle.

To mount a disk as a statically provisioned volume, make sure that you have purchased a disk.

Manually create a persistent volume (PV) and a persistent volume claim (PVC) that are used to statically provision a disk.

Limits

  • Alibaba Cloud disks cannot be shared. A disk can be mounted only to one pod.
  • A disk can be mounted only to a node that is deployed in the same zone as the disk.

Mount a statically provisioned disk volume in the ACK console

Step 1: Create a PV

  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 cluster details page, choose Volumes > Persistent Volumes.
  5. In the upper-right corner of the Persistent Volumes page, click Create.
  6. In the Create PV dialog box, configure the following parameters.
    Parameter Description
    PV Type You can select Cloud Disk or NAS. In this example, Cloud Disk is selected.
    Volume Plug-in Select CSI.
    Access Mode The default setting is ReadWriteOnce.
    Disk ID Select a mountable disk that is deployed in the same region and zone as your cluster.
    File System Type Select the file system of the disk. Valid values: ext4, ext3, xfs, and vfat. Default value: ext4.
    Label Add labels to the PV.
  7. Click Create.

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, Existing Volumes is selected.
    Note If no PV is created, you can set Allocation Mode to Create Volume and configure the required parameters to create a PV. For more information, see Create a PV.
    Existing Volumes Click Select PV. In the dialog box that appears, find the PV that you want to use and click Select in the Actions column.
    Capacity The capacity claimed by the PVC.
    Note The capacity of a PV cannot be greater than the capacity of the disk that is associated with the PV.
    Access Mode The default setting is ReadWriteOnce.
  4. Click Create.
    After the PVC is created, you can view the PVC in the list of PVCs. The PVC is bound to the specified 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 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.

Mount a statically provisioned disk volume by using kubectl

Step 1: Create a PV

  1. Use the following template to create a pv-static.yaml file:
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: csi-pv
      labels:
        alicloud-pvname: static-disk-pv
    spec:
      capacity:
        storage: 25Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      csi:
        driver: diskplugin.csi.alibabacloud.com
        volumeHandle: "<your-disk-id>"
      nodeAffinity:
        required:
          nodeSelectorTerms:
          - matchExpressions:
            - key: topology.diskplugin.csi.alibabacloud.com/zone
              operator: In
              values:
              - "<your-node-zone-id>"
    Parameter Description
    name The name of the PV.
    labels The labels that you want to add to the PV
    storage The available storage of the disk
    accessModes The access mode of the PV
    persistentVolumeReclaimPolicy The reclaim policy of the PV
    driver The type of driver. This parameter is set to diskplugin.csi.alibabacloud.com. This value indicates that the Alibaba Cloud CSI plug-in is used.
    volumeHandle The ID of the disk that is associated with the PV
    nodeAffinity Information about the zone to which the PV and PVC belong.

    You can configure this parameter to specify the zone to which the pod that uses the PV and PVC is scheduled.

  2. Run the following command to create the PV:
    kubectl create -f pv-static.yaml
  3. View the PV that you created.
    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 > Persistent Volumes.
      On the Persistent Volumes page, verify that the newly created PV is displayed.

Step 2: Create a PVC

  1. Use the following template to create a pvc-static.yaml file:
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: csi-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 25Gi
      selector:
        matchLabels:
          alicloud-pvname: static-disk-pv
    Parameter Description
    name The name of the PVC.
    accessModes The access mode of the PVC
    storage The capacity claimed by the PVC. The claimed capacity cannot exceed the capacity of the PV that is bound to the PVC.
    matchLabels The labels that are used to select a PV and bind the PV to the PVC. The labels must be the same as those of the PV.
  2. Run the following command to create the PVC:
    kubectl create -f pvc-static.yaml
  3. In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims.
    On the Persistent Volume Claims page, verify that the newly created PVC is displayed.

Step 3: Deploy an application

In this example, a web application is created and mounted with the PVC you created.

  1. Use the following template to create a web.yaml file:
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: web
    spec:
      selector:
        matchLabels:
          app: nginx
      serviceName: "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: csi-pvc
    • mountPath: the path where you want to mount the disk in the container.
    • claimName: the name of the PVC, which is used to associate the application with the PVC.
  2. Run the following command to create an application and mount the statically provisioned PV by using the PVC:
    kubectl apply -f web.yaml
  3. In the left-side navigation pane of the details page, choose Workloads > StatefulSets.
    On the SratefulSets page, verify that the newly created web application is displayed.

Verify that the statically provisioned disk can be used to persist data

  1. View the pods that run the web application and the files in the mounted disk.
    1. Run the following command to query the pods that run the web application:
      kubectl get pod | grep web

      Expected output:

      web-1**** 1/1 Running 0 32s
    2. Run the following command to check whether a new disk is mounted to the /data path:
      kubectl exec web-1**** df | grep data

      Expected output:

      /dev/vdf 20511312 45080 20449848 1% /data
    3. Run the following command to query files in the /data path:
      kubectl exec web-1**** ls /data

      Expected output:

      lost+found
  2. Run the following command to create a file named static in the /data path:
    kubectl exec web-1**** touch /data/static
  3. Run the following command to query files in the /data path:
     kubectl exec web-1**** ls /data

    Expected output:

    lost+found
    static
  4. Run the following command to delete the web-1**** pod:
    kubectl delete pod web-1****

    Expected output:

    pod "web-1****" deleted
  5. 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 
      web-2****  1/1     Running   0           14s
    2. Run the following command to query files in the /data path:
      kubectl exec web-2**** ls /data

      Expected output:

      lost+found
      static
      The static file still exists in the disk. This indicates that data is persisted to the statically provisioned disk.