Files in containers are temporarily stored on disks. In this case, when some special applications run in containers, these applications face some issues. First, when a container crashes, the files in the container are lost. This is because the container will be restarted in a clean state. Second, when multiple containers run in the same Elastic Container Instance (ECI), it is often necessary to share files among these containers. To resolve these issues, ECI allows you to use a volume to store files.

Currently, ECI supports the following types of volumes:

  • ConfigFile
  • emptyDir
  • nfs
  • FlexVolume

To use a volume, you need to declare the volume and then mount it to the target ECI.

Declare a volume in the ECI console

  • ConfigFile

You can use a ConfigFile volume to deliver configuration data to an ECI.

The following figure shows the configuration of declaring a ConfigFile volume in the ECI console. Note that you can enter the configuration information in plaintext and the system automatically encodes the configuration information in Base64.

  • emptyDir

An emptyDir volume is accessible to all containers in an ECI. It can be used to share data among all the containers in the ECI. When you delete an ECI to which an emptyDir volume is mounted, the data stored in the emptyDir volume is also deleted.

The following figure shows the configuration of declaring an emptyDir volume in the ECI console.

  • nfs

An nfs volume allows you to mount a Network File System (NFS) to an ECI. Unlike an emptyDir volume, when you delete an ECI to which an nfs volume is mounted, the nfs volume is unmounted from the ECI and the data stored in the nfs volume is retained. This means that an nfs volume can be pre-populated with data to transfer the data among different ECIs.

Note You must create an NFS before you declare an nfs volume. We recommend that you create a file system and add a mount target in the Apsara File Storage NAS console. The target ECI must be deployed in the same Virtual Private Cloud (VPC) as that configured in the mount target. If you want to use other NFS services, make sure that the ECI can access the Internet. For more information, see Access the Internet.

The following figure shows the configuration of declaring an nfs volume in the ECI console.

  • FlexVolume

FlexVolume is an out-of-tree volume plug-in for Kubernetes V1.2 and later. FlexVolume allows you to customize storage drivers.

Currently, you can set the driver to alicloud/disk for an ECI to use an Alibaba Cloud disk as a volume through FlexVolume. More types of drivers will be supported in the future.

Use a disk through FlexVolume

You can declare an existing disk or a new disk as a volume for an ECI. Each ECI can use only one disk.

Declare a new disk as a volume: You need to specify the storage size for the new disk. A disk of the specified size is created when the target ECI is created, and is deleted when the target ECI is deleted. This method is applicable to ECIs that require high I/O performance of the storage.

Declare an existing disk as a volume: If you declare an existing disk as a volume for an ECI, the data stored on the disk is retained when the ECI is deleted. This method is applicable to ECIs that are used to run database applications, such as MySQL and Redis.

The following figure shows the configuration of declaring an existing disk as a volume in the ECI console.

The following figure shows the configuration of declaring a new disk as a volume in the ECI console.

Declare a volume in an API call

  • Declare a ConfigFile volume in an API call.
Volume.1.Name=configfiledemo
Volume.1.Type=ConfigFileVolume // The volume type. It is a fixed value.
Volume.1.ConfigFileVolume.ConfigFileToPath.1.Content=bGl1bWk=
Volume.1.ConfigFileVolume.ConfigFileToPath.1.Path=configpath
Notice The configuration information of the volume must be encoded in Base64.
  • Declare an emptyDir volume in an API call.
Volume.1.Name=emptydirdemo
Volume.1.Type=EmptyDirVolume // The volume type. It is a fixed value.
  • Declare an nfs volume in an API call.
Volume.1.Name=nfsdemo
Volume.1.Type=NFSVolume // The volume type. It is a fixed value.
Volume.1.NFSVolume.Path=/share
Volume.1.NFSVolume.Server=3f9cd4a596-naw76.cn-shanghai.nas.aliyuncs.com
Volume.1.NFSVolume.ReadOnly=False
  • Declare an existing disk as a volume in an API call.
Volume.1.Name=flexvolumedemo
Volume.1.Type=FlexVolume // The volume type. It is a fixed value.
Volume.1.FlexVolume.Driver=alicloud/disk // The driver for using a disk.
Volume.1.FlexVolume.FsType=ext4 // The type of the file system. Valid values: ext3, ext4, and xfs.
Volume.1.FlexVolume.Options={"volumeId":"d-bp1j17ifxfasvts3tf40"} // The volumeId parameter specifies the ID of an existing disk.

Declare a new disk as a volume in an API call.

Volume.1.Name=flexvolumedemo
Volume.1.Type=FlexVolume // The volume type. It is a fixed value.
Volume.1.FlexVolume.Driver=alicloud/disk // The driver for using a disk.
Volume.1.FlexVolume.FsType=ext4 // The type of the file system. Valid values: ext3, ext4, and xfs.
Volume.1.FlexVolume.Options={"volumeSize":"20"} // The volumeSize parameter specifies the storage size of the new disk. Valid values: 20 to 500. Unit: GiB.

Mount a volume to an ECI

To use a volume, you need to mount it to the target ECI.

When you mount a volume to a directory in an ECI, the existing data in the directory is overwritten with the data in the volume. Especially when you mount an emptyDir volume to a directory, all data in the directory is deleted. Exercise caution when you configure the directory.

  • The following figure shows the configuration of mounting a volume in the ECI console.
  • The following sample code shows how to mount a volume in an API call:

    Container.1.VolumeMount.1.Name=volumename // The name of the volume.
    Container.1.VolumeMount.1.MountPath=/volume // The directory to which the volume is mounted.
    Container.1.VolumeMount.1.ReadOnly=False