All Products
Search
Document Center

Elastic Container Instance:Mount a disk volume

Last Updated:Dec 29, 2023

Disks are block-level storage products provided by Alibaba Cloud. Disks feature low latency, high performance, high durability, and high reliability. You can use the FlexVolume plug-in to mount disks to elastic container instance as volumes. This topic describes how to mount a disk to an elastic container instance.

Precautions

  • Only pay-as-you-go disks can be mounted. For more information, see EBS devices.

  • Disks cannot be shared. A disk can be mounted to only one instance.

  • A maximum of six disk volumes can be mounted to an elastic container instance.

  • The instance to which a disk is mounted must reside in the same zone as the disk.

  • NVMe disks, for which the multi-mount feature is enabled, cannot be mounted to elastic container instances.

  • You cannot mount a disk that has partitions.

Configuration description

When you call the CreateContainerGroup API operation to create an elastic container instance, you can use Volume-related parameters to specify volumes, and then use Container.VolumeMount-related parameters to mount the volumes to containers. The following part describes the relevant parameters. For more information, see CreateContainerGroup.

Specify the volume to be mounted

When you use Volume-related parameters to specify volumes, you must first specify the types and names of the volumes. Then, you must configure other Volume-related parameters based on the value of the Volume.N.Type parameter.

Parameter

Type

Example

Description

Volume.N.Name

String

disk-demo

The name of volume N.

Volume.N.Type

String

FlexVolume

Set the value to FlexVolume. The value specifies a volume that is mounted by using the FlexVolume plug-in.

Volume.N.FlexVolume.Driver

String

alicloud/disk

The driver type when you use the FlexVolume plug-in to mount a volume. Set the value to alicloud/disk, which specifies to mount Alibaba Cloud disks.

Volume.N.FlexVolume.FsType

String

ext4

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

Volume.N.FlexVolume.Options

String

{"volumeSize":"50","tags":"test:eci"}

The options when you set the Volume.N.Type parameter to FlexVolume. Each option is a key-value pair in the JSON string.

When you use the FlexVolume plug-in to mount a disk, this parameter represents the configuration parameters of the disk volume. The following parameters can be configured:

Important

If you do not specify the volumeId parameter, you must specify the volumeSize parameter. If you specify the volumeId parameter, the values of the attribute parameters of the disk, such as volumeSize, are ignored.

  • (Statically) Specify an existing disk

    • volumeId: the ID of the disk.

  • (Dynamically) Create a new disk

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

      Note

      If the disk to be mounted is created based on a snapshot (you use the snapshotid parameter to specify the snapshot), the actual size of the created disk is the greater of the following two values:

      • The value of the volumeSize parameter

      • The size of the disk that is created based on a snapshot.

    • performanceLevel: the performance level of the disk. The system automatically uses an enhanced SSD (ESSD) as the volume. You cannot specify the category of the disk. For more information, see ESSDs.

    • deleteWithInstance: specifies whether to release the disk when the elastic container instance is released. Default value: true.

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

    • kmsKeyId: the ID of the Key Management Service (KMS) key that is used to encrypt the disk.

    • tags: the tags to be bound to the disk. You can bind up to 20 tags. Format: "tagkey1:tagvalue1,tagkey2:tagvalue2".

    • snapshotId: the ID of the snapshot. A snapshot is a stateful data file of a disk at a specific point in time. You can obtain the data on a disk that is created based on a snapshot. For more information, see Snapshot overview.

      Note

      If a disk that is created based on a snapshot has partitions, you cannot mount the disk to an elastic container instance.

Mount volumes

After you specify volumes, you can use VolumeMount-related parameters to mount the volumes to containers.

Parameter

Type

Example

Description

Container.N.VolumeMount.N.Name

String

test-volume

The name of volume N to be mounted to container N. This value is the value of Volume.N.Name.

Container.N.VolumeMount.N.MountPath

String

/usr/share

The directory to which volume N is mounted.

Data under this directory is overwritten by the data on the volume. Make sure that this value is correct.

Container.N.VolumeMount.N.SubPath

String

/usr/sub

The subdirectory of the volume. This parameter specifies different subdirectories of the same volume that the instance can mount to different subdirectories of containers.

Container.N.VolumeMount.N.ReadOnly

Boolean

false

Specifies whether the mount directory is read-only. Default value: false.

Container.N.VolumeMount.N.MountPropagation

String

None

The mount propagation setting of volume N. Mount propagation allows you to share volumes that are mounted to one container to other containers in the same elastic container instance, or even to other elastic container instances on the same host. Valid values:

  • None: The volume mount does not receive subsequent mounts that are mounted to this volume or its subdirectories.

  • HostToContainer: The volume mount receives all subsequent mounts that are mounted to this volume or its subdirectories.

  • Bidirectional: This value has a similar effect as HostToContainer. The volume mount receives all subsequent mounts that are mounted to this volume or its subdirectories. In addition, all volume mounts created by container N are propagated back to the host and to all containers of all elastic container instances that use the same volume.

Default value: None.

Note

Volumes can also be mounted to init containers. The required parameters are similar to the parameters in the preceding table. You only need to change Container to InitContainer.

Configuration examples

  • Call an API operation

    Call the CreateContainerGroup API operation to create an elastic container instance and mount a disk to the instance as a volume. The following part describes the parameters. For more information, see CreateContainerGroup.

    • Example 1: (Statically) Specify an existing disk

      ContainerGroupName=test-disk1
      SecurityGroupId=sg-bp1daxpbz9lzpvvc****
      VSwitchId=vsw-bp1gds63lmlm7ib05****
      # Specify the volume to be mounted.
      Volume.1.Name=disk-demo
      Volume.1.Type=FlexVolume
      Volume.1.FlexVolume.Driver=alicloud/disk
      Volume.1.FlexVolume.FsType=ext4
      Volume.1.FlexVolume.Options={"volumeId":"d-2zebuamrpar7xnj****"}
      # Mount the volume to a container.
      Container.1.Name=nginx
      Container.1.Image=registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx:1.14.2
      Container.1.VolumeMount.1.Name=disk-demo
      Container.1.VolumeMount.1.MountPath=/data
      Note

      When you statically specify an existing disk, the specified disk must be in the Pending state and reside in the same zone as the elastic container instance.

    • Example 2: (Dynamically) Create a new disk

      ContainerGroupName=test-disk2
      # Specify the volume to be mounted.
      Volume.1.Name=disk-demo
      Volume.1.Type=FlexVolume
      Volume.1.FlexVolume.Driver=alicloud/disk
      Volume.1.FlexVolume.FsType=ext4
      Volume.1.FlexVolume.Options={"volumeSize":"50","tags":"test:eci"}
      # Mount the volume to a container.
      Container.1.Name=nginx
      Container.1.Image=registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx:1.14.2
      Container.1.VolumeMount.1.Name=disk-demo
      Container.1.VolumeMount.1.MountPath=/data
  • Use SDK for Python

    Use SDK for Python of v1.0.8 or later to create an elastic container instance for running the MySQL application. The data files of the MySQL application are stored on the disk whose ID is d-2zefzudpm3649e****. Sample code:

    from aliyunsdkeci.request.v20180808.CreateContainerGroupRequest import CreateContainerGroupRequest
    request = CreateContainerGroupRequest()
    request.set_SecurityGroupId(secureGroup-xxx)
    request.set_VSwitchId(vSwitch-xxx)
    request.set_ContainerGroupName(name-xxx)
    request.set_EipInstanceId(eip-xxx)
    request.set_RestartPolicy('Always')
    # volume1 is a disk volume, which is used to store MySQL data files.
    volume1 = {
        'Name': 'mysql-data-volume',
        'Type': 'FlexVolume',
        'FlexVolume.Driver': 'alicloud/disk'
        'FlexVolume.FsType': 'ext4',
        'FlexVolume.Options': '{\"volumeId\": \"d-2zefzudpm3649e****\"}',
    }
    # volume2 is a ConfigFile volume, which is used to pass in configuration data. Content must be Base64-encoded.
    configFileToPath = {
        'Path': 'my.cnf',
        'Content': 'aGVsbG8gd29ybGQ=',
    }
    volume2 = {
        'Name': 'mysql-config-volume',
        'Type': 'ConfigFileVolume',
        'ConfigFileVolume.ConfigFileToPaths': [configFileToPath],
    }
    # Attach the two volumes to the elastic container instance.
    request.set_Volumes([volume1, volume2])
    # Configure volumeMounts-related parameters to mount the two volumes to container directories.
    volume_mount1 = {
        'Name': 'mysql-data-volume',
        'MountPath': '/var/lib/mysql',
        'ReadOnly': False,
    }
    volume_mount2 = {
        'Name': 'mysql-config-volume',
        'MountPath': '/etc/mysql/conf.d/',
        'ReadOnly': False,
    }
    #environment variable
    env ={
        'Key': 'MYSQL_ROOT_PASSWORD',
        'Value': '123456',
    }
    #container ports
    port = {
        'Protocol':'TCP',
        'Port': 3306,
    }
    # containers
    container = {
        'Image': 'mysql:5.7',
        'Name': 'mysql',
        'Cpu': 0.5,
        'Memory': 1.0,
        'ImagePullPolicy': 'Always',
        'VolumeMounts': [volume_mount1, volume_mount2],
        'EnvironmentVars': [env],
        'Ports': [port],
    }
    request.set_Containers([container])
    response = eciClient.do_action_with_exception(request)
    print response