Use a static cloud disk volume

Updated at:

Use a pre-existing cloud disk as a statically provisioned volume for your workloads. Create a PersistentVolume (PV) for the cloud disk manually, bind the PV to a PersistentVolumeClaim (PVC), and then mount the volume in a pod.

Prerequisites

  • Create an empty data disk in the Elastic Compute Service (ECS) console. For more information, see Create an empty data disk.

  • The cloud disk must reside in the same region and zone as the cluster and must be in the Available state.

Use a cloud disk volume with PV and PVC

  1. Create a cloud disk PV.

    Create a cloud disk PV by using a YAML manifest or in the Container Service for Kubernetes (ACK) console.

    • Create a PV by using a YAML manifest.

      1. Create a file named disk-pv.yaml with the following content:

        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 PV name (name) must be the same as the cloud disk ID (volumeId). Replace the other example values with the values of your cloud disk.

      2. Run the following command to create the PV.

        kubectl apply -f disk-pv.yaml
    • Create a PV in the ACK console.

      1. Log on to the ACK console. In the left navigation pane, click Clusters.

      2. On the Clusters page, click the name of the destination cluster or click Details in the Actions column.

      3. In the left-side navigation pane, choose Volumes > Persistent Volumes.

      4. On the Persistent Volumes page, click Create in the upper-right corner.

      5. In the Create PV dialog box, configure the parameters of the volume.

        Parameter

        Description

        PV Type

        In this example, select cloud disk.

        Volume Plug-in

        In this example, select Flexvolume.

        Access Mode

        The default value is ReadWriteOnce.

        Disk ID:

        Select the cloud disk that you prepared.

        File System Type:

        Select the file system type. Valid values are ext4, ext3, xfs, and vfat. The default value is ext4.

        Labels

        Add labels to the volume.

      6. After you configure the parameters, click Create.

  2. Create a PVC.

    1. Create a file named disk-pvc.yaml with the following content:

      kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: pvc-disk
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: disk
        resources:
          requests:
            storage: 20Gi
    2. Run the following command to create the PVC.

      kubectl apply -f disk-pvc.yaml
  3. Create a pod by using a StatefulSet.

    1. Create a file named disk-pod.yaml with the following content to define a Service and a StatefulSet that mounts the pvc-disk PVC at /data:

      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
    2. Run the following command to create the pod.

      kubectl apply -f disk-pod.yaml