All Products
Search
Document Center

Container Service for Kubernetes:Mount a disk as a statically provisioned volume

Last Updated:Nov 04, 2024

Alibaba Cloud disks are block-level data storage resources for Elastic Compute Service (ECS), which 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 statically and dynamically provision disk volumes. You can manually create a persistent volume (PV) and a persistent volume claim (PVC) to mount a statically provisioned disk volume and use the disk volume to persist application data.

Prerequisites

  • An ACK cluster is created and the CSI plug-in is installed in the cluster. For more information, see Create an ACK managed cluster and Create an ACK Serverless cluster.

    Important
    • If your cluster contains nodes that use CentOS 7.9 images or other OS images whose Linux kernel versions are earlier than 4.9 and the xfs file system is used to persist data (the fstype parameter is set to xfs), do not update the CSI plug-in to 1.24.7 or later. Linux kernel versions earlier than 4.9 are incompatible with CSI 1.24.7 or later. In this case, if you update the CSI plug-in to 1.24.7 or later, the pods to which the xfs file system is mounted may fail to start up. Other file systems are not affected. To resolve this issue, submit a ticket.

    • When you update the CSI plug-in to 1.26.4, you need to update both csi-plugin and csi-provisioner.

  • A pay-as-you-go disk is created. The disk ID is d-wz92s6d95go6ki9x****. Make sure that the disk is in the same region as the cluster. For more information, see Create an empty data disk.

    Note

    The disk that you create must meet the following requirements:

    • If you create an ultra disk, the capacity must be at least 20 GiB.

    • If you create a standard SSD, the capacity must be at least 20 GiB.

    • If you create an Enterprise SSD (ESSD), the capacity must be at least 1 GiB.

  • A kubectl client is connected to the cluster. For more information, see Connect to a cluster by using kubectl.

Scenarios

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 logs at high speeds.

  • 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 PV and a PVC that are used to statically provision a disk.

Limits

  • Disks cannot be mounted to pods deployed in zones other than the zone where the disks reside.

  • Disks for which the multi-attach feature is disabled can be mounted only to one pod.

  • The type of disk must match the Elastic Compute Service (ECS) instance types that are used in your cluster before you can launch a pod. For more information about the matching rules between disk types and ECS instance types, see Instance families.

    Important

    If you change the billing method of an ECS instance in your cluster from pay-as-you-go to subscription, you cannot change the billing method of its disks to subscription. Otherwise, the disks cannot be mounted to the cluster. You can purchase storage capacity units (SCUs) to reduce storage costs. For more information about SCUs, see Overview of SCUs.

Mount a statically provisioned disk volume in the ACK console

Step 1: Create a PV

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

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Volumes > Persistent Volumes.

  3. Click the Persistent Volumes tab and click Create.

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

    Parameter

    Description

    PV Type

    In this example, Cloud Disk is selected.

    • ACK clusters support disk volumes, File Storage NAS (NAS) volumes, and Object Storage Service (OSS) volumes.

    • ACK Serverless clusters support disk volumes and NAS volumes.

    Access Mode

    By default, this parameter is set to ReadWriteOnce.

    Disk ID

    Select a disk that is created in the same region and zone as the cluster. The disk must be in the Pending state.

    File System Type

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

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

    In this example, Cloud Disk is selected.

    • ACK clusters support disk volumes, NAS volumes, and OSS volumes.

    • ACK Serverless clusters support disk volumes and NAS volumes.

    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, set the Allocation Mode parameter 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

    By default, this parameter is set to 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: 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. Set the application parameters.

    This example shows how to set the volume parameters. For more information about other parameters, see Use a StatefulSet to create a stateful application.

  4. 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.数据卷

  5. Configure other parameters and click Create.

    After the application is created, you can use the OSS volume to store application data.

Mount a statically provisioned disk volume by using kubectl

Step 1: Create a PV and a PVC

  1. Create a file named disk-static.yaml and copy the following content to the file. Replace the following parameters in the file:

    • <YOUR-DISK-ID>: the ID of the disk that you created. Example: d-wz92s6d95go6ki9x****.

    • <YOUR-DISK-SIZE>: the size of the disk. Example: 25Gi.

    • <YOUR-DISK-CATEGORY>: the type of the disk. Example: cloud_essd.

    • <YOUR-DISK-ZONE-ID>: the zone where the disk is located. Example: cn-beijing-i.

    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: "<YOUR-DISK-ID>"
      annotations:
        csi.alibabacloud.com/volume-topology: '{"nodeSelectorTerms":[{"matchExpressions":[{"key":"node.csi.alibabacloud.com/disktype.<YOUR-DISK-CATEGORY>","operator":"In","values":["available"]}]}]}'
    spec:
      capacity:
        storage: "<YOUR-DISK-SIZE>"
      claimRef:
        apiVersion: v1
        kind: PersistentVolumeClaim
        namespace: default
        name: disk-pvc
      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-DISK-ZONE-ID>"
      storageClassName: disk
      volumeMode: Filesystem
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: disk-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: "<YOUR-DISK-SIZE>"
      storageClassName: disk
      volumeName: "<YOUR-DISK-ID>"

    Parameter

    Description

    accessModes

    The access mode of the PVC. This parameter is usually set to ReadWriteOnce. The setting for the PV must be the same as that for the PVC. For more information, see the specification table in the Step 2: Create a PVC section of Use a dynamically provisioned disk volume.

    persistentVolumeReclaimPolicy

    The reclaim policy of the PV.

    • Delete: When PVCs are deleted, the related PVs and disks are also deleted.

    • Retain: When PVCs are deleted, the related PVs and disks are retained. The PVs and disk data can only be manually deleted.

    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.

    storageClassName

    This parameter is invalid for statically provisioned volumes, but the setting for the PV must be the same as that for the PVC. You do not need to create a StorageClass object in advance.

    nodeAffinity

    Information about the zone to which the PV belongs.

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

    annotations["csi.alibabacloud.com/volume-topology"]

    Additional limits on nodes to which the statically provisioned disk volume can be mounted. We recommend that you specify the type of disk to ensure that the pod can be scheduled to nodes that support the specified type of disk.

    claimRef

    Specify the PVC that can be bound to the PV. Delete this parameter if you want the PV to be bound to any PVC.

    volumeName

    Specify the PV that can be bound to the PVC. Delete this parameter if you want the PVC to be bound to any PV.

  2. Run the following command to create the PV and PVC:

    kubectl apply -f disk-static.yaml
  3. Run the following command to view the PV and PVC that you created:

    kubectl get pv <YOUR-DISK-ID>
    kubectl get pvc disk-pvc

    Expected output:

    NAME                     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM              STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
    d-wz92s6d95go6ki9x****   20Gi       RWO            Retain           Bound       default/disk-pvc   disk           <unset>                          27s
    
    NAME       STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
    disk-pvc   Bound    d-wz92s6d95go6ki9x****   20Gi       RWO            disk           <unset>                 27s

    The PV and PVC enter the Bound state after a few seconds.

Step 2: Create an application

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

  1. Run the following command to create a Secret that stores the root account and password of the MySQL application: Change the password in a production environment.

    kubectl create secret generic mysql-pass --from-literal=password=mypassword
  2. 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:8
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: pvc-disk
              mountPath: /var/lib/mysql
          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 mounted to the application.

  3. Run the following command to create an application and mount the PVC to the application:

    kubectl create -f mysql.yaml
  4. View the application.

    In the left-side navigation pane of the cluster details page in the ACK console, choose Workloads > StatefulSets. You can view the created application on the StatefulSets page.

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

After a pod is deleted and recreated, the pod still stores the data that is written before the pod is deleted. You can use the following method to verify that the statically provisioned disk volume can be used to persist data.

  1. Make sure that a disk is mounted to the MySQL application. Run the following command to check whether a new disk is mounted to the /var/lib/mysql path:

    kubectl exec mysql-0 -- df -h /var/lib/mysql

    Expected output:

    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vdd         25G  213M   25G   1% /var/lib/mysql
  2. Create a file in the disk.

    1. Run the following command to create a file named test-persistent in the /var/lib/mysql path:

      kubectl exec mysql-0 -- touch /var/lib/mysql/test-persistent
    2. Run the following command to confirm that the file is created:

      kubectl exec mysql-0 -- ls /var/lib/mysql/test-persistent

      Expected output:

      /var/lib/mysql/test-persistent
  3. Run the following command to delete the pod named mysql-0:

    kubectl delete pod mysql-0

    Expected output:

    pod "mysql-0" deleted
  4. Test whether the file still exists after the pod is deleted.

    1. Run the following command to check whether the recreated pod runs as normal:

      kubectl get pod mysql-0

      Expected output:

      NAME      READY   STATUS    RESTARTS   AGE
      mysql-0   1/1     Running   0          12s
    2. Run the following command to view the files that you created:

      kubectl exec mysql-0 -- ls /var/lib/mysql/test-persistent

      Expected output:

      /var/lib/mysql/test-persistent

      The test-persistent file still exists in the disk. This indicates that data is persisted to the statically provisioned disk volume.