To provision an Alibaba Cloud disk as a dynamic persistent volume (PV), you must manually create a StorageClass, and set the storageClassName field in a persistent volume claim (PVC) to specify the disk type.

Create a StorageClass with a specified zone ID (zoneid)

  1. Create a storage-class.yaml file and copy the following content into the file:
    kind: StorageClass
    apiVersion: storage.k8s.io/v1beta1
    metadata:
      name: alicloud-disk-ssd-hangzhou-b
    provisioner: alicloud/disk
    parameters:
      type: cloud_ssd
      regionid: cn-hangzhou
      zoneid: cn-hangzhou-b
    reclaimPolicy: Retain
  2. Run the following command to create a StorageClass:
    kubectl apply -f storage-class.yaml
Note Parameters
  • provisioner: Set this parameter to alicloud/disk. This is a volume plug-in for provisioning cloud disks of Alibaba Cloud.
  • type: This parameter specifies the disk type. Valid values are cloud_efficiency, cloud_ssd, cloud_essd, and available. If you set this parameter to available, the system attempts to create a disk in the order of Enhanced SSD, SSD, and ultra disk. The system keeps trying until a disk is created.
  • regionid: This parameter specifies the region where a disk is created.
  • reclaimPolicy: The policy for reclaiming a disk. By default, this parameter is set to Delete. You can also set this parameter to Retain. If you require high data security, we recommend that you set the Retain mode to avoid data loss caused by user errors.
  • zoneid: This parameter specifies the region where a disk is created.
    For a multi-zone cluster, you can specify multiple zones. Example:
    zoneid: cn-hangzhou-a,cn-hangzhou-b,cn-hangzhou-c
  • encrypted: optional. This parameter specifies whether a created disk is encrypted. By default, this parameter is set to false, which indicates that a created disk is not encrypted.

Create a StorageClass in WaitForFirstConsumer mode.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
   name: alicloud-disk-topology-ssd
provisioner: alicloud/disk
parameters:
    type: cloud_ssd
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
Note
  • If you do not configure a StorageClass in WaitForFirstConsumer mode and the zoneid parameter is not specified, a PV is created in the zone where the Disk-Controller component is deployed.
  • If you do not configure a StorageClass in WaitForFirstConsumer mode but the zoneid parameter is specified, the system attempts to create a PV in the specified zones in a round robin manner.
  • If you configure a StorageClass in WaitForFirstConsumer mode, a cloud disk is created based on the node that is scheduled by the pod that consumes the PVC. The cloud disk is created in the zone that is scheduled by the pod.

Create a PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: disk-ssd
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: alicloud-disk-ssd-hangzhou-b
  resources:
    requests:
      storage: 20Gi
---
kind: Pod
apiVersion: v1
metadata:
  name: disk-pod-ssd
spec:
  containers:
  - name: disk-pod
    image: nginx
    volumeMounts:
      - name: disk-pvc
        mountPath: "/mnt"
  restartPolicy: "Never"
  volumes:
    - name: disk-pvc
      persistentVolumeClaim:
        claimName: disk-ssd

Default StorageClasses:

In a multi-zone cluster, you must manually create a StorageClass to specify the zone where a cloud disk is created.

By default, the following types of StorageClasses are provided for single-zone clusters:
  • alicloud-disk-efficiency: ultra disk.
  • alicloud-disk-ssd: SSD.
  • alicloud-disk-essd: Enhanced SSD.
  • alicloud-disk-available: a high-availability mode. In this mode, the system attempts to create an SSD in priority. If the SSD resources are exhausted, the system attempts to create an ultra disk.
    Notice For versions earlier than alicloud-disk-controller v1.14.8.44-c23b62c5-aliyun, the system attempts to create a disk in the order of Enhanced SSD, SSD, and ultra disk.
  • alicloud-disk-topology: create a cloud disk in WaitForFirstConsumer mode.

Create a multi-instance StatefulSet by using a disk

We recommend that you use the volumeClaimTemplates parameter. This allows you to dynamically create multiple PVCs and PVs, and bind the PVs to PVCs.

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"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: disk-ssd
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: disk-ssd
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "alicloud-disk-ssd-hangzhou-b"
      resources:
        requests:
          storage: 20Gi

You can also use dynamic PVs in the console. For more information, see Use disks to create dynamic persistent volumes in the console.