To use a dynamic volume, you need to manually create a StorageClass, and use storageClassName to specify the disk type in a persistent volume claim (PVC).

Create a StorageClass

  1. Create the storage-class-csi.yaml file and copy the following code to the file:
    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
     name: alicloud-disk-ssd-b
    provisioner: diskplugin.csi.alibabacloud.com
    parameters:
      type: cloud_ssd
      regionid: cn-shenzhen
      zoneId: cn-shenzhen-b
    reclaimPolicy: Delete
    volumeBindingMode: Immediate
  2. Run the following command to create a StorageClass:
    kubectl apply -f storage-class-csi.yaml
Note Parameter description:
  • provisioner: Set this parameter to diskplugin.csi.alibabacloud.com, which indicates that the provisioner plug-in for Alibaba Cloud disks is used to create the StorageClass.
  • type: the disk type. Valid values: cloud, cloud_efficiency, cloud_ssd, cloud_essd, and available. If you set this parameter to available, the system tries to create a disk in the order of ultra disk, ESSD, and SSD, and stops trying until a disk is created.
  • regionid: optional. The region of the disk to create.
  • reclaimPolicy: the policy for reclaiming the disk. The default value is Delete. You can set this parameter to Retain. If you require high data security, we recommend that you set this parameter to Retain to avoid accidental deletion.
  • zoneid: optional. The zone of the disk to create. If you specify multiple zones in this parameter, you need to create a persistent volume (PV) in topology mode.
  • encrypted: optional. Specifies whether the disk is encrypted. The default value is false, indicating that the disk is not encrypted.

Create a PVC and a PV

The following sample YAML file shows how to create a PV for using a dynamic volume:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: disk-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 25Gi
  storageClassName: alicloud-disk-ssd
Note storage: the size of the disk to create. The minimum size is 20 GiB.

Default StorageClasses:

In a multi-zone cluster, you must manually create the StorageClass to specify the zone of the required disk.

By default, the following types of StorageClasses are available for single-zone clusters:
  • alicloud-disk-efficiency: ultra disk
  • alicloud-disk-ssd: SSD
  • alicloud-disk-essd: ESSD
  • alicloud-disk-available: This option ensures high availability. The system tries to create an ultra disk first.

    If no ultra disk is available in the specified zone, the system tries to create an SSD. If no SSD is available, the system tries to create an ESSD.

Create an application

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
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: disk-pvc