This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to scale out an Elastic Compute Service (ECS) cluster by calling an API operation.

Prerequisites

Before you call the API operation to scale out an ECS cluster, make sure that the following prerequisites are met:
  • EDAS SDK for Java is installed. For more information, see Guide of using EDAS SDK for Java to call the EDAS API.
  • The ID of the region in which you want to scale out an ECS cluster is obtained. For example, the region ID is cn-hangzhou.
  • ECS instances are created. For example, the instance IDs of the ECS instances are i-bp13o01lzmbsvhsl**** and i-bp13o01lzmbsvhsl****.
  • An ECS cluster is created. For more information how to create an ECS cluster, see Create an ECS cluster by calling an API operation.

    If you have created an ECS cluster, you can call the ListCluster operation to query ECS clusters and obtain the ID of the specified ECS cluster. The ID of the ECS cluster is specified by the ClusterId parameter. For example, the ID of the ECS cluster is 369d06d7-450b-4f3d-bf75-9536fcd9****.

Background information

You can call the InstallAgent or InsertClusterMember operation to import ECS instances into the ECS cluster. We recommend that you call InstallAgent operation. We recommend that you do not call InsertClusterMember operation.
  • When you call the InsertClusterMember operation to import an ECS instance, the operating system of the ECS instance is reinstalled. This deletes all data on the ECS instance. In addition, you must specify a new password for logging on to the ECS instance. Therefore, we recommend that you do not call the InsertClusterMember operation to import ECS instances.
  • When you call the InstallAgent operation to import an ECS instance, the operating system of the ECS instance is not reinstalled. Therefore, we recommend that you call the InstallAgent operation to import ECS instances.

Scale out an ECS cluster by purchasing ECS instances

You can scale out an ECS cluster by purchasing ECS instances in one of the following ways:
  • Purchase ECS instances based on the specifications of an existing ECS instance. You must specify the instance ID of the existing ECS instance.
    // The instance ID of the existing ECS instance based on which new ECS instances are purchased. 
    request.setTemplateInstanceId("i-bp13o01lzmbsvhsl****");
  • Purchase ECS instances based on an instance launch template. You must specify the ID and version of the instance launch template.
    // The ID of the instance launch template. 
    request.setTemplateId("lt-****hy9s2");     
    // The version of the instance launch template. A value of -1 indicates that the default version of the instance launch template is used. 
    request.setTemplateVersion("-1");
    Note

    An instance launch template helps you create ECS instances with ease. An instance launch template contains the configurations that you have selected to create instances. All configurations are optional and can meet the personalized requirements of different scenarios.

    An instance launch template cannot be modified. However, you can create multiple versions of each template and set different parameters for each version. This allows you to update Instance configurations by using versions. You can select a version of an instance launch template to create instances.

    For more information about instance launch templates and template versions, see Create a launch template and Create a template version.

The following code provides an example on how to scale out an ECS cluster by purchasing ECS instances.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.ScaleoutApplicationWithNewInstancesRequest;
import com.aliyuncs.edas.model.v20170801.ScaleoutApplicationWithNewInstancesResponse;

public class ScaleoutApplicationWithNewInstances {

    public static void main(String[] args)  {
        // The AccessKey ID of your Alibaba Cloud account or a RAM user within the Alibaba Cloud account. 
        String aliyun_user_ak = "<yourAccessKeyId>";
        // The AccessKey secret of your Alibaba Cloud account or a RAM user within the account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the ECS cluster resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Create an API request and set the parameters. 
        ScaleoutApplicationWithNewInstancesRequest request = new ScaleoutApplicationWithNewInstancesRequest();
        // The ID of the ECS cluster to be scaled out. 
        request.setClusterId("369d06d7-450b-4f3d-bf75-9536fcd9****");
        // The instance ID of the existing ECS instance based on which new ECS instances are purchased. To purchase ECS instances based on an instance launch template, you do not need to set this parameter. Instead, you must specify the ID and version of the instance launch template. 
        request.setTemplateInstanceId("i-bp13o01lzmbsvhsl****");
        // The number of ECS instances to be purchased. 
        request.setScalingNum(2);
        // The billing method of the ECS instances to be purchased. A value of PrePaid indicates the subscription billing method. A value of PostPaid indicates the pay-as-you-go billing method. 
        request.setInstanceChargeType("PrePaid");
        // The unit of the subscription period. A value of Month indicates that subscription period is counted by month. A value of Week indicates that the subscription period is counted by week. 
        request.setInstanceChargePeriodUnit("Month");
        // The subscription period. If the unit is month, you can set the value to 1 to 12 or an integer multiple of 12. If the unit is week, you can set the value to 1 to 4. 
        request.setInstanceChargePeriod(12);
        // Specifies whether to enable auto-renewal. A value of true indicates that auto-renewal is enabled. A value of false indicates that auto-renewal is disabled. 
        request.setAutoRenew(true);
        // The subscription period to be extended in each auto-renewal. If the unit is month, you can set the value to 1 to 12 or an integer multiple of 12. If the unit is week, you can set the value to 1 to 4. 
        request.setAutoRenewPeriod(12);


        try {
            ScaleoutApplicationWithNewInstancesResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage() + "\nInstanceIds=" + response.getInstanceIds());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
Message=success
InstanceIds=[i-bp1ilg5114r054eo****, i-bp1ilg5114r054eo****]

Scale out an ECS cluster by adding existing ECS instances to the cluster

The following code provides an example on how to scale out an ECS cluster by adding existing ECS instances to the cluster.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.InstallAgentRequest;
import com.aliyuncs.edas.model.v20170801.InstallAgentResponse;

public class InstallAgent {

    public static void main(String[] args)  {
        // The AccessKey ID of your Alibaba Cloud account or a RAM user within the Alibaba Cloud account. 
        String aliyun_user_ak = "<yourAccessKeyId>";
        // The AccessKey secret of your Alibaba Cloud account or a RAM user within the account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the ECS cluster resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Create an API request and set the parameters. 
        InstallAgentRequest request = new InstallAgentRequest();
        // The ID of the ECS cluster to be scaled out. 
        request.setClusterId("369d06d7-450b-4f3d-bf75-9536fcd9****");
        // The instance IDs of the ECS instances to be added to the cluster. Separate multiple instance IDs with commas (,). 
        request.setInstanceIds("i-bp13o01lzmbsvhsl****,i-bp13o01lzmbsvhsl****");


        try {
            InstallAgentResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
Message=success

Verify the result

After you scale out the ECS cluster by purchasing ECS instances or adding existing ECS instances to the cluster, you can call the ListClusterMembers operation to query the ECS instances in the cluster. Check the instance IDs of ECS instances in the response and determine whether the cluster is scaled out. The instance IDs are specified by the EcsId parameter.

{
  "Message": "success",
  "RequestId": "C326EF8E-048E-45C8-BE87-18D512000573",
  "ClusterMemberPage": {
    "ClusterMemberList": {
      "ClusterMember": [
        {
          "Status": 1,
          "ClusterId": "369d06d7-450b-4f3d-bf75-9536fcd9****",
          "CreateTime": 1618827574965,
          "UpdateTime": 1618827655646,
          "EcuId": "08da39f3-257c-44c4-abce-9ce31ec1****",
          "ClusterMemberId": "49eeb7fa-7c97-436f-b87f-a875a8b8****",
          "EcsId": "i-bp1ilg5114r054eo****"
        },
        {
          "Status": 1,
          "ClusterId": "369d06d7-450b-4f3d-bf75-9536fcd9****",
          "CreateTime": 1618827574957,
          "UpdateTime": 1618827655645,
          "EcuId": "8287bcf6-cde3-4377-8906-086d2c32****",
          "ClusterMemberId": "f5b30a72-7137-4b5c-8d21-a2eac2f0****",
          "EcsId": "i-bp1ilg5114r054eo****"
        }
      ]
    },
    "PageSize": 20000,
    "CurrentPage": 1,
    "TotalSize": 2
  },
  "Code": 200
}