All Products
Search
Document Center

Container Service for Kubernetes:Use a statically provisioned disk volume

Last Updated:Nov 01, 2024

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

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

  • A kubectl client is connected to the ACK 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.

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. In the upper-right corner of the Persistent Volumes page, 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, NAS volumes, and 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 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.

  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, you can set Allocation Mode to Create Volume and set 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 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. 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. 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>"   # 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. 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 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 and bind a PV to the PVC. The labels must be the same as those of the PV to be bound to the PVC.

  2. Run the following command to create a 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: 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 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: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. Replace it with the actual user name. 
      password: dGVzdDEt****     # The password of the MySQL application. Replace it with the actual 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 details page, choose Workloads > StatefulSets.

    On the SratefulSets page, you can view the newly created MySQL application.

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

  1. View the pods of the MySQL application and the files in the mounted disk.

    1. Run the following command to query the pods of 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 of the pod:

      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 of one pod:

      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 of one pod:

     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 of one pod:

      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.