All Products
Search
Document Center

Container Service for Kubernetes:Use a statically provisioned disk volume

Last Updated:Sep 01, 2023

Alibaba Cloud disks are block-level data storage products for Elastic Compute Service (ECS). Alibaba Cloud disks 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. 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 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.

  • A pay-as-you-go disk is created. The disk ID is d-wz92s6d95go6ki9x****. For more information, see Create a 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 enhanced SSD (ESSD), the capacity must be at least 20 GiB.

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

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 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 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. To use shared storage, you can use Apsara File Storage NAS (NAS) volumes. For more information, see NAS volume overview.

  • 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

    In this example, Cloud Disk is selected.

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

    • ACK Serverless clusters support disk volumes and NAS volumes.

    Volume Plug-in

    You can select Flexvolume or CSI. In this example, CSI is selected.

    Access Mode

    By default, this parameter is set to 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

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

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

    Volumes
  4. Set 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. Create a file named pv-static.yaml based on the following content:

    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>"   # Set the value to the zone ID of the node on which you want to deploy your application. Example: "cn-beijing-b". 

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

    accessModes

    The access mode of the PV.

    persistentVolumeReclaimPolicy

    The reclaim policy of the PV.

    driver

    The type of driver. In this example, 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 cloud 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 cluster 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. Create a file named pvc-static.yaml based on the following content:

    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 cluster details page, choose Volumes > Persistent Volume Claims.

    On the Persistent Volume Claims page, verify that the newly created PVC is displayed.

Step 3: Create an application

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

  1. Create a file named mysql.yaml based on the following content:

    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:5.7
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
            ports:
            - containerPort: 80
              name: mysql
            volumeMounts:
            - name: pvc-disk
              mountPath: /data
          volumes:
            - name: pvc-disk
              persistentVolumeClaim:
                claimName: csi-pvc
    ---
    apiVersion: v1
    kind: Secret
    metadata:
      name: mysql-pass
    type: Opaque
    data:
      username: dGVz****             # The username of the MySQL application, repace it with your actual user name.
      password: dGVzdDEt****     # The password for the MySQL applicationMySQL, replace it with your actual user password.
      
    • mountPath: the path where you want to mount the disk in the container.

    • claimName: the name of the PVC that is mounted to the application.

  2. Run the following command to create an application and mount the statically provisioned PV by using the PVC:

    kubectl apply -f mysql.yaml
  3. In the left-side navigation pane of the cluster details page, choose Workloads > StatefulSets.

    On the SratefulSets page, verify that the newly created MySQL application is displayed.

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

  1. View the pods that run the MySQL application and the files on the mounted disk.

    1. Run the following command to query the pods that run the MySQL application:

      kubectl get pod | grep mysql

      Expected output:

      mysql-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 mysql-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 mysql-1**** -- ls /data

      Expected output:

      lost+found
  2. Run the following command to create a file named static in the /data path:

    kubectl exec mysql-1**** -- touch /data/static
  3. Run the following command to query files in the /data path:

     kubectl exec mysql-1**** -- ls /data

    Expected output:

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

    kubectl delete pod mysql-1****

    Expected output:

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

      kubectl exec mysql-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.