Use a static cloud disk volume

更新时间:
复制 MD 格式

This topic describes how to use a pre-existing cloud disk as a statically provisioned volume. This process involves manually creating a PersistentVolume (PV) that references the disk, binding it to a PersistentVolumeClaim (PVC), and then mounting it in a pod.

Prerequisites

To use a cloud disk volume, create a cloud disk in the ECS console. For more information, see Create an empty data disk.

Use a cloud disk volume with PV and PVC

  1. Create a cloud disk PV.

    You can create the PV by using a YAML manifest or in the 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 Alibaba Cloud disk ID (volumeId).

      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 View 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, set the following parameters.

        Parameter

        Description

        PV Type

        Select cloud disk.

        Volume Plug-in

        Select Flexvolume.

        Access Mode

        Defaults to ReadWriteOnce.

        Disk ID:

        Select an Available cloud disk in the same region and zone as the cluster.

        File System Type:

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

        Labels

        Add labels to the volume.

      6. After configuring 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.

    1. Create a file named disk-pod.yaml 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: 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