This topic describes how to mount a statically provisioned disk volume by using a persistent volume (PV) and a persistent volume claim (PVC).

Prerequisites

To mount a disk as a volume, you must first create the disk in the Elastic Compute Service (ECS) console. For more information, see Create a disk.

Mount a statically provisioned disk volume by using a PV and a PVC

  1. Create a PV of the disk type.
    You can create a PV of the disk type in the Container Service for Kubernetes (ACK) console or by using a YAML file.
    • Create a PV by using a YAML file.
      1. Create a file named disk-pv.yaml by using the following template:
        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 that is specified by volumeId.
      2. Run the following command to create a PV:
        kubectl apply -f disk-pv.yaml
    • Create a PV in the ACK console.
      1. Log on to the ACK console.
      2. In the left-side navigation pane of the ACK console, click Clusters.
      3. On the Clusters page, find the cluster that you want to manage. Then, click the name of the cluster or click Details in the Actions column.
      4. In the left-side navigation pane of the details page, choose Volumes > Persistent Volumes.
      5. In the upper-right corner of the Persistent Volumes page, click Create.
      6. In the Create PV dialog box, set the parameters.
        ParameterDescription
        PV TypeIn this example, Cloud Disk is selected.
        Volume Plug-inIn this example, Flexvolume is selected.
        Access ModeBy default, this parameter is set to ReadWriteOnce.
        Disk IDSelect a mountable disk that is deployed in the same region and zone as your cluster.
        File System TypeSelect the file system type of the disk. Valid values: ext4, ext3, xfs, and vfat. Default value: ext4.
        LabelAdd labels to the PV.
      7. After you complete the settings, click Create.
  2. Create a PVC.
    1. Create a file named disk-pvc.yaml by using the following template:
      kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: pvc-disk
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: disk
        resources:
          requests:
            storage: 20Gi
    2. Run the following command to create a PVC:
      kubectl apply -f disk-pvc.yaml
  3. Create a pod.
    1. Create a file named disk-pod.yaml by using the following template:
      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 a pod:
      kubectl apply -f disk-pod.yaml