All Products
Search
Document Center

Container Service for Kubernetes:Mount a statically provisioned disk volume

Last Updated:Jan 20, 2025

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 an empty data 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. In the left-side navigation pane, click Clusters.

      2. 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.

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

      4. In the upper-right corner of the Persistent Volumes page, click Create.

      5. In the Create PV dialog box, set the parameters.

        Parameter

        Description

        PV Type

        In this example, Cloud Disk is selected.

        Volume Plug-in

        In this example, Flexvolume is selected.

        Access Mode

        By default, this parameter is set to ReadWriteOnce.

        Disk ID

        Select a mountable disk that is deployed in the same region and zone as your cluster.

        File System Type

        Select the file system type of the disk. Valid values: ext4, ext3, xfs, and vfat. Default value: ext4.

        Label

        Add labels to the PV.

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