All Products
Search
Document Center

Use FlexVolume to mount a disk volume

Last Updated: Nov 17, 2021

This topic describes how to use FlexVolume to mount a volume of the disk type to an elastic container instance.

Prerequisites

Your Kubernetes cluster is deployed with Virtual Kubelet.

Note

Alibaba Cloud Serverless Kubernetes (ASK) clusters are integrated with Virtual Kubelet. For other types of clusters, you must manually deploy Virtual Kubelet. For more information, see Connect an elastic container instance to a Kubernetes cluster.

Description

Alibaba Cloud disks are Elastic Block Storage products. They feature low latency, as well as high performance, durability, and reliability. For more information, see Disks.

FlexVolume allows you to mount existing disks or automatically mounts newly created disks. The following code provides an example YAML file of a pod.

apiVersion: v1
kind: Pod
metadata:
  name: test-flexvolume-disk
spec:
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
  volumes:
  - name: cache-volume
    flexVolume:
      driver: alicloud/disk
      fsType: ext4
      options:
        volumeId: "d-2zehdahrwoa7srga****"

In the preceding example, an existing disk is specified in options. If you want to create a disk, you can declare the disk size and type in options. The following table describes the parameters contained in options from the preceding sample code.

Parameter

Type

Example

Description

volumeId

String

d-2zebuamrpar7xnjf****

Specifies the ID of the existing disk. The disk must be in the Unattached state.

volumeSize

Integer

200

Specifies the size of the disk. Unit: GiB. Valid values: 20 to 32768.

This parameter is required when you create a disk.

performanceLevel

String

PL1

Specifies the performance level of the disk. For more information, see EBS performance.

This parameter is optional when you create a disk.

deleteWithInstance

Boolean

false

Specifies whether to release the disk when the instance to which the disk is attached is released.

This parameter is optional when you create a disk.

encrypted

Boolean

false

Specifies whether to encrypt the disk. Default value: false. For more information, see Encryption overview.

This parameter is optional when you create a disk.

kmsKeyId

String

2ea91e17-6de6-4b4f-a3ea-0e382cc8****

The ID of the Key Management Service (KMS) key for encrypted disks.

This parameter is optional when you create a disk.

Configuration examples

  1. Prepare a YAML configuration file.

    vim flexvolume_disk.yaml

    The following code provides an example flexvolume_disk.yaml file:

    • Example 1: Specify an existing disk

      apiVersion: v1
      kind: Pod
      metadata:
        name: test-flexvolume-disk
        labels: 
          alibabacloud.com/eci: "true"
      spec:
        containers:
        - image: nginx:latest
          name: test-container
          volumeMounts:
          - mountPath: /cache-test
            name: cache-volume
        volumes:
        - name: cache-volume
          flexVolume:
            driver: alicloud/disk
            fsType: ext4
            options:
              volumeId: "d-2zehdahrwoa7srga****"
    • Example 2: Create a disk

      apiVersion: v1
      kind: Pod
      metadata:
        name: test-flexvolume-disk
        labels: 
          alibabacloud.com/eci: "true" 
      spec:
        containers:
        - image: nginx:latest
          name: test-container
          volumeMounts:
          - mountPath: /cache-test
            name: cache-volume
        volumes:
        - name: cache-volume
          flexVolume:
            driver: alicloud/disk
            fsType: ext4
            options:
                volumeType: "alicloud-disk-essd"
                volumeSize: "100Gi"
                performanceLevel: "PL2"
    • Example 3: Create an encrypted disk

      apiVersion: v1
      kind: Pod
      metadata:
        name: test-flexvolume-disk2
        labels: 
          alibabacloud.com/eci: "true" 
      spec:
        containers:
        - image: nginx:latest
          name: test-container
          volumeMounts:
          - mountPath: /cache-test
            name: cache-volume
        volumes:
        - name: dzf-disk
          flexVolume:
            driver: "alicloud/disk"
            fsType: "ext4"
            options:
              volumeSize: "20Gi"
              encrypted: "true"
              kmsKeyId: "2ea91e17-6de6-4b4f-a3ea-0e382cc8****"
  2. Deploy a pod.

    kubectl create -f flexvolume_disk.yaml
  3. View the mount result.

    Run the ls command to view the file directory in the pod. The mount directory /cache-test of the disk volume is generated in the pod.

    Mount a disk volume to an elastic container instance