This topic describes how to create an ECS cluster by using the EDAS Java SDK.
Prerequisites
Complete the following prerequisites before you create an ECS cluster:
- Install the EDAS Java SDK. For more information, see Java SDK integration guide.
Obtain the region ID where you want to create the ECS cluster. This topic uses cn-hangzhou as an example.
- Create a namespace. For more information, see Create a namespace.
If a namespace already exists, you can call the ListUserDefineRegion operation to query the custom namespaces and retrieve the RegionId of the target namespace.
If you want to create an ECS cluster in a VPC, create the VPC first. For more information, see Create a VPC with an IPv4 CIDR block.
If you have already created a VPC, you can call the ListVpc API operation to query your list of VPCs and obtain the VPC instance ID (VpcId). This topic uses vpc-bp1hcg467ekqsv**** as an example.
Cluster types
EDAS supports two types of ECS clusters. Choose the type that matches your infrastructure:
| Type | Infrastructure | Use when |
|---|---|---|
| Alibaba Cloud | ECS instances on Alibaba Cloud | All your instances run on Alibaba Cloud. |
| Non-Alibaba Cloud (hybrid cloud) | ECS instances from Alibaba Cloud, on-premises data centers, and other cloud providers, connected through Express Connect circuits | You manage instances across multiple environments and need a single cluster to unify them. See Create a hybrid cloud ECS cluster. |
This topic describes how to create an Alibaba Cloud ECS cluster.
Create an ECS cluster
Use the following sample code 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) {
// An AccessKey pair grants full access to your account and poses a high security risk. We strongly recommend that you create and use a RAM user for API calls and daily operations. To create a RAM user, log on to the RAM console.
// This example shows how to load your AccessKey ID and AccessKey Secret from environment variables. You can also store them in a configuration file based on your business needs.
// For security reasons, do not hardcode your AccessKey ID and AccessKey Secret in your code. This can lead to credential leaks.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// The region ID where 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 its parameters.
InsertClusterRequest request = new InsertClusterRequest();
// The region ID of the namespace where you want to create the ECS cluster. Example: cn-hangzhou:doc.
request.setLogicalRegionId("cn-hangzhou:doc");
// The name of the ECS cluster. Example: ECSClueter_doc.
request.setClusterName("ECSClueter_doc");
// The cluster type. A value of 2 specifies an ECS cluster.
request.setClusterType(2);
// The network type of the cluster. A value of 2 specifies VPC, and 1 specifies classic network.
request.setNetworkMode(2);
// The VPC instance ID. This parameter is required if the network type is 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();
}
}
}Running the program returns the following output:
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 API operation to list your clusters. Check the ClusterName parameter in the response to confirm that the cluster was created.
Calling the ListCluster API operation returns the following sample response:
{
"ClusterList": {
"Cluster": [
{
"OversoldFactor": 1,
"NodeNum": 0,
"ResourceGroupId": "rg-acfm3umruf2****",
"ClusterId": "369d06d7-450b-4f3d-bf75-9536fcd9****",
"CreateTime": 1618558401855,
......
"VpcId": "vpc-bp1hcg467ekqsv****",
"UpdateTime": 1618558401855,
"ClusterName": "ECSClueter_doc",
"RegionId": "cn-hangzhou:doc",
"IaasProvider": "ALIYUN",
"CpuUsed": 0
}
]
},
"Message": "success",
"RequestId": "65C65D19-36BF-40C1-A3D0-23902D2AF49C",
"Code": 200
}