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

Prerequisites

Before you call the API operation to create 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 create an ECS cluster is obtained. For example, the region ID is cn-hangzhou.
  • A microservice namespace is created. For example, a microservice namespace with the ID of cn-hangzhou:doc is created. For more information, see Create a namespace.

    If a microservice namespace already exists, you can call the ListUserDefineRegion operation to query custom microservice namespace and obtain the value of the RegionId parameter for the microservice namespace that you want to use. For example, the value is cn-hangzhou:doc.

  • A virtual private cloud (VPC) is created if you need to create an ECS cluster in a VPC. For more information, see Create a VPC with an IPv4 CIDR block.

    If you have created a VPC, you can call the ListVpc operation to query VPCs and obtain the ID of the specified VPC. The ID of the VPC is specified by the VpcId parameter. For example, the ID of the VPC is vpc-bp1hcg467ekqsv****.

Background information

EDAS provides two types of ECS clusters: Alibaba Cloud ECS clusters and hybrid cloud ECS clusters.
  • An Alibaba Cloud ECS cluster consists of ECS instances that Alibaba Cloud provides. In this topic, an Alibaba Cloud ECS cluster is created.
  • A hybrid cloud ECS cluster consists of instances provided by Alibaba Cloud ECS, self-managed data centers, and other cloud service providers. These instances are interconnected over Express Connect circuits and added to hybrid cloud ECS clusters for management. For more information, see Create a hybrid cloud ECS cluster.

Create an ECS cluster

The following code provides an example on how to create an ECS 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.InsertClusterRequest;
import com.aliyuncs.edas.model.v20170801.InsertClusterResponse;

public class InsertCluster {

    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 you want to create the ECS cluster. 
        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. 
        InsertClusterRequest request = new InsertClusterRequest();
        // The region ID of the microservice namespace in which you want to create the ECS cluster. Example: cn-hangzhou:doc. 
        request.setLogicalRegionId("cn-hangzhou:doc");
        // The custom name of the ECS cluster. Example: ECSClueter_doc. 
        request.setClusterName("ECSClueter_doc");
        // The type of the cluster. Set the value to 2 that indicates an ECS cluster. 
        request.setClusterType(2);
        // The network type of the ECS cluster. A value of 2 indicates the VPC network type. A value of 1 indicates the classic network type. 
        request.setNetworkMode(2);
        // The ID of the VPC. This parameter is required if you set the network type to VPC. 
        request.setVpcId("vpc-bp1hcg467ekqsv****");


        try {
            InsertClusterResponse response = client.getAcsResponse(request);
            System.out.println("Message=" + response.getMessage() + "\nClusterName=" + response.getCluster().getClusterName()+ "\nClusterId=" + response.getCluster().getClusterId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
Message=success
ClusterName=ECSClueter_doc
ClusterId=369d06d7-450b-4f3d-bf75-9536fcd9****

Verify the result

After you create the ECS cluster, you can call the ListCluster operation to query clusters. Check the ClusterName parameter in the response and determine whether the ECS cluster is created.

Sample response that is returned after you call the ListCluster operation:
{
  "ClusterList": {
    "Cluster": [
      {
        "OversoldFactor": 1,
        "NodeNum": 0,
        "ResourceGroupId": "rg-acfm3umruf2****",
        "ClusterId": "369d06d7-450b-4f3d-bf75-9536fcd9****",
        "CreateTime": 1618558401855,
        ......
        "VpcId": "vpc-bp1hcg467ekqsv0zr****",
        "UpdateTime": 1618558401855,
        "ClusterName": "ECSClueter_doc",
        "RegionId": "cn-hangzhou:doc",
        "IaasProvider": "ALIYUN",
        "CpuUsed": 0
      }
    ]
  },
  "Message": "success",
  "RequestId": "65C65D19-36BF-40C1-A3D0-23902D2AF49C",
  "Code": 200
}