You can use Alibaba Cloud static volumes directly or through PVs and PVCs.

Prerequisites

To use a disk as a volume, you must create the disk in the ECS console first. For more information about how to create a disk, see Create a pay-as-you-go disk.

Directly as volumes

Use the following disk-deploy.yaml file to create a Pod:

  1. Create the disk-deploy.yaml file with the following content.
    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: d-wz9f6l6nm0uvazgh7y17
              mountPath: /data
          volumes:
            - name: "d-wz9f6l6nm0uvazgh7y17"
              flexVolume:
                driver: "alicloud/disk"
                fsType: "ext4"
                options:
                  volumeId: "d-wz9f6l6nm0uvazgh7y17"
  2. Run the following command to create a Pod:
    kubectl apply -f disk-deploy.yaml
Note

In the volumes section, we recommend that you set the name and volumeId to the same value.

If your cluster is deployed across multiple zones, you need to schedule the Pod and disk to the same zone.

You need to add a nodeSelector for the Pod as follows:
nodeSelector:
        failure-domain.beta.kubernetes.io/zone: cn-hangzhou-b

Through PVs and PVCs

  1. Use a disk to create a PV
    You can create a PV through the console or by using a YAML file.
    • Create a PV by using a YAML file
      Use the following disk-pv.yaml file to create a PV:
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: d-bp1j17ifxfasvts3****
        labels:
          failure-domain.beta.kubernetes.io/zone: cn-hangzhou-b
          failure-domain.beta.kubernetes.io/region: cn-hangzhou
      spec:
        capacity:
          storage: 20Gi
        storageClassName: disk
        accessModes:
          - ReadWriteOnce
        flexVolume:
          driver: "alicloud/disk"
          fsType: "ext4"
          options:
            volumeId: "d-bp1j17ifxfasvts3****"
      Note The name of the PV must be the same as the disk ID, volumeId.
    • Create a PV through the console
      1. Log on to the Container Service console.
      2. In the left-side navigation pane, choose Clusters > Persistent Volumes to go to the PVs and PVCs page.
      3. On the Persistent Volumes tab, select a cluster and click Create in the upper-right corner.
        Persistent Volumes
      4. Set the parameters in the Create PV dialog box.
        • PV Type: In this example, set the PV type to Cloud Disk.
        • Access Mode: Default is ReadWriteOnce.
        • Disk ID: Select a disk that is in the same region and zone as your cluster.
        • File System Type: Select the data type in which data is stored in the disk. Supported data types include ext4, ext3, xfs, and vfat. Default is ext4.
        • Label: Add labels for the PV.
        Configure the PV
      5. After the configuration is complete, click Create.
  2. Create a PVC
    Use the following disk-pvc.yaml file to create a PVC:
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pvc-disk
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: disk
      resources:
        requests:
          storage: 20Gi
  3. Create a Pod
    Use the following disk-pod.yaml file to create a Pod:
    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: pvc-disk