Elastic Container Instance (ECI) allows you to use an Alibaba Cloud disk as a volume.

In this case, set the volume type to the following value:

  • DiskVolume

API mode

When you call the CreateContainerGroup operation to create an ECI, you can specify a static disk or a dynamic disk as a volume of the ECI.

Use a static disk

A static disk refers to an existing disk. You can mount an existing disk to an ECI. When the ECI is deleted, the data on the disk is retained. This method is applicable to ECIs that are used to run database applications, such as MySQL and Redis.

Volume.N.Name=my-diskvolume1
Volume.N.Type=DiskVolume
Volume.N.DiskVolume.FsType=ext4
Volume.N.DiskVolume.DiskId=d-2ze4rk800li49moq5kxw
			

Use a dynamic disk

A dynamic disk is created when the target ECI is created, and is deleted when the target ECI is deleted. If an ECI requires high I/O performance of the storage and stores a large amount of temporary files in the storage, such as log files that do not need to be retained after the ECI is deleted, we recommend that you mount a dynamic disk to the ECI.

Volume.N.Name=my-diskvolume1
Volume.N.Type=DiskVolume
Volume.N.DiskVolume.FsType=ext4
Volume.N.DiskVolume.DiskSize=20
			

Parameter description:

  • DiskId: the ID of the disk that you have created. ECI supports all types of disks. For more information, see Disk overview.
  • DiskSize: the storage size of the disk to be dynamically created. Valid values: 20 to 500. Unit: GiB.
  • If the declared disk is not formatted, the system automatically formats the disk to a type specified by the FsType parameter. Currently, only ext3, ext4, and xfs are supported.

Note:

  • If the DiskId parameter is not set, the DiskSize parameter must be set. If both the parameters are set, the value of the DiskSize parameter is ignored.
  • If you mount multiple disks to an ECI, the drive letters of these disks may change unexpectedly in certain cases, for example, when you unmount a disk. Therefore, you cannot mount multiple disks to an ECI currently.
  • Except for a disk that is used to store image caches, each disk can be mounted to only one ECI.
  • A dynamic disk is created and deleted along with the target ECI.

Sample code

  • Use the ECI Python SDK V1.0.8 or later to create an ECI for running the MySQL application, whose data files are stored on disk d-2zefzudpm3649e****.

    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')
    # Declare two volumes. The first volume is of the DiskVolume type and is used to store MySQL data files. The second volume is of the ConfigFileVolume type and is used to store the configuration file.
    volume1 = {
      'Name': 'mysql-data-volume',
      'Type': 'DiskVolume',
      'DiskVolume.FsType': 'ext4',
      'DiskVolume.DiskId': 'd-2zefzudpm3649e****',
    }
    # The value of the Content parameter must be encoded in Base64.
    configFileToPath = {
      'Path': 'my.cnf',
      'Content': 'aGVsbG8gd29ybGQ=',
    }
    volume2 = {
      'Name': 'mysql-config-volume',
      'Type': 'ConfigFileVolume',
      'ConfigFileVolume.ConfigFileToPaths': [configFileToPath],
    }
    # Set the two volumes for the ECI.
    request.set_Volumes([volume1, volume2])
    
    # Mount the two volumes to directories in the ECI.
    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,
    }
    # Set the environment variable.
    env ={
      'Key': 'MYSQL_ROOT_PASSWORD',
      'Value': '123456',
    }
    #Set the container port.
    port = {
      'Protocol':'TCP',
      'Port': 3306,
    }
    # Set the container.
    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
    					

Virtual Kubelet mode

For more information about how to use a disk as a volume in Alibaba Cloud Container Service for Kubernetes, see Use static disks to create PVs for stateful services.

The following examples show how to specify a disk as a volume in the scenarios where pods are scheduled to the virtual-kubelet node in Alibaba Cloud Container Service for Kubernetes and Alibaba Cloud Serverless Kubernetes.

Use a dynamic disk

In the following sample code, the volumeType field specifies the type of the disk and the volumeSize field specifies the storage size of the disk.

  • Specify a disk as a volume when you create a pod.
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
  namespace: default
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
      - name: myhtml
        mountPath: "/usr/share/nginx/html" 
  volumes:
    - name: myhtml
      flexVolume:
        driver: "alicloud/disk"
        fsType: "ext4"
        options:
          volumeType: "alicloud-disk-essd"
          volumeSize: "20Gi"
			
  • Specify a disk as a volume when you create a deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dynamic
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
          - name: myhtml
            mountPath: "/usr/share/nginx/html"
      volumes:
        - name: myhtml
          flexVolume:
            driver: "alicloud/disk"
            fsType: "ext4"
            options:
              volumeType: "alicloud-disk-essd"
              volumeSize: "20Gi"
			

Use a static disk

Assume that you have created a disk whose ID is d-1234567. In the following sample code, the volumeId field specifies the ID of the disk.

apiVersion: v1
kind: Pod
metadata:
  name: mysql
  namespace: default
spec:
  containers:
  - image: mysql
    name: mysql
    volumeMounts:
      - name: mydata
        mountPath: "/var/lib/mysql"
  volumes:
    - name: mydata
      flexVolume:
        driver: "alicloud/disk"
        fsType: "ext4"
        options:
          volumeType: "alicloud-disk-essd"
          volumeId: "d-1234567"
			

Specify a disk as a volume when you create a StatefulSet (1)

  • You can specify a disk as a volume by using the volumeClaimTemplates field in the configuration file of a StatefulSet.

  • You need to manually create a persistent volume (PV).

    • Make sure that the disk with the ID d-1234567 is created in advance.
    • If the StatefulSet has multiple replicas, you need to create multiple disks and PVs.
    • Set the volumeId field to the ID of the disk that you have created.
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: d-1234567
    spec:
      capacity:
      storage: 20Gi
      accessModes:
        - ReadWriteOnce
      flexVolume:
      driver: "alicloud/disk"
      fsType: "ext4"
      options:
          volumeId: "d-1234567"
    					
  • Create the zookeeper StatefulSet with three replicas.

    • If you set the volumeClaimTemplates field, the system automatically creates persistent volume claims (PVCs) and binds them to PVs accordingly.
    • In the following sample code, only the storage size requirement is specified for the PVs to which PVCs are bound. That is, the storage size of the PVs must not be smaller than that specified by the storage field.
    apiVersion: apps/v1beta2
    kind: StatefulSet
    metadata:
      name: zookeeper
    spec:
      selector:
      matchLabels:
          app: zookeeper
      replicas: 3
      serviceName: zookeeper-server
      template:
      metadata:
          labels:
            app: zookeeper
      spec:
          containers:
          - env:
            - name: ZOOKEEPER_SERVERS
              value: "3"
            image: "komljen/zookeeper:3.4.10"
            imagePullPolicy: IfNotPresent
            name: zookeeper
            ports:
            - containerPort: 2181
              name: client
            - containerPort: 2888
              name: server
            - containerPort: 3888
              name: leader-election
            readinessProbe:
              exec:
                command:
                - /opt/zookeeper/bin/zkOK.sh
              initialDelaySeconds: 10
              timeoutSeconds: 2
              periodSeconds: 5
            livenessProbe:
              exec:
                command:
                - /opt/zookeeper/bin/zkOK.sh
              initialDelaySeconds: 120
              timeoutSeconds: 2
              periodSeconds: 5
            volumeMounts:
            - mountPath: /data
              name: zookeeper-vol
          restartPolicy: Always
      volumeClaimTemplates:
      - metadata:
          name: zookeeper-vol
      spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 15Gi
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: zookeeper
    spec:
      ports:
      - name: client
      port: 2181
      targetPort: 2181
      selector:
      app: zookeeper
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: zookeeper-server
    spec:
      clusterIP: None
      ports:
      - name: server
      port: 2888
      targetPort: 2888
      - name: leader-election
      port: 3888
      targetPort: 3888
      selector:
      app: zookeeper
    					

Specify a disk as a volume when you create a StatefulSet (2)

You can also specify a disk as a volume by using the persistentVolumeClaim field in the configuration file of a StatefulSet.

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zookeeper-vol
spec:
  storageClassName: alicloud-disk-essd
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

---
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: zookeeper
spec:
  selector:
    matchLabels:
      app: zookeeper
  replicas: 3
  serviceName: zookeeper-server
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
      - env:
        - name: ZOOKEEPER_SERVERS
          value: "3"
        image: "komljen/zookeeper:3.4.10"
        imagePullPolicy: IfNotPresent
        name: zookeeper
        ports:
        - containerPort: 2181
          name: client
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        readinessProbe:
          exec:
            command:
            - /opt/zookeeper/bin/zkOK.sh
          initialDelaySeconds: 10
          timeoutSeconds: 2
          periodSeconds: 5
        livenessProbe:
          exec:
            command:
            - /opt/zookeeper/bin/zkOK.sh
          initialDelaySeconds: 120
          timeoutSeconds: 2
          periodSeconds: 5
        volumeMounts:
        - mountPath: /data
          name: zookeeper-data
      restartPolicy: Always
      volumes:
      - name: zookeeper-data
        persistentVolumeClaim:
          claimName: zookeeper-vol

---
apiVersion: v1
kind: Service
metadata:
  name: zookeeper
spec:
  ports:
  - name: client
    port: 2181
    targetPort: 2181
  selector:
    app: zookeeper
---
apiVersion: v1
kind: Service
metadata:
  name: zookeeper-server
spec:
  clusterIP: None
  ports:
  - name: server
    port: 2888
    targetPort: 2888
  - name: leader-election
    port: 3888
    targetPort: 3888
  selector:
    app: zookeeper
			

For more information about the example, visit https://github.com/AliyunContainerService/serverless-k8s-examples/tree/master/pvc.