When you need to automate container deployments or integrate application creation into CI/CD pipelines, the Enterprise Distributed Application Service (EDAS) console is insufficient. The InsertK8sApplication API operation, called through the EDAS SDK for Java, lets you programmatically deploy a Java application from a container image to a Kubernetes cluster managed by EDAS.
Quick start
The following Java snippet creates an application from a container image on a Kubernetes cluster in EDAS. Replace the placeholder values with your actual configuration.
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.InsertK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.InsertK8sApplicationResponse;
public class InsertK8sApplication {
public static void main(String[] args) {
// Retrieve credentials from environment variables.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
InsertK8sApplicationRequest request = new InsertK8sApplicationRequest();
request.setAppName("edas-SDK0311image");
request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
request.setReplicas(2);
request.setLimitCpu(0);
request.setLimitMem(0);
request.setRequestsCpu(0);
request.setRequestsMem(0);
request.setLogicalRegionId("cn-hangzhou:doc");
request.setRepoId("image-demo-project");
request.setImageUrl("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:1.0");
try {
InsertK8sApplicationResponse response = client.getAcsResponse(request);
System.out.println("AppId=" + response.getApplicationInfo().getAppId()
+ "\nAppName=" + response.getApplicationInfo().getAppName());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}Expected output:
AppId=d8130adc-2298-49ea-9c64-f19165fa****
AppName=edas-SDK0311imageFor parameter details, see the
Prerequisites
Before you begin, ensure that you have:
EDAS SDK for Java installed
The region ID for the target deployment environment
A microservice namespace created in EDAS
A Kubernetes cluster imported into the microservice namespace
An image stored in an image repository (example:
registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:1.0)
To look up an existing namespace or cluster, call ListUserDefineRegion for the namespace RegionId or ListCluster for the ClusterId.
Step 1: Configure the SDK client
Initialize a DefaultAcsClient with your AccessKey credentials and target region. Store credentials in environment variables to keep them out of source code.
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.InsertK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.InsertK8sApplicationResponse;
public class InsertK8sApplication {
public static void main(String[] args) {
// Retrieve credentials from environment variables.
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations.
// For production workloads, use a Resource Access Management (RAM) user with
// least-privilege permissions instead.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// Replace with your target region ID
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);Step 2: Build the deployment request
Create an InsertK8sApplicationRequest and set the required parameters: application name, cluster ID, replica count, resource quotas, namespace, and image URL.
// Build the request
InsertK8sApplicationRequest request = new InsertK8sApplicationRequest();
// Application name
request.setAppName("edas-SDK0311image");
// Cluster ID of the target Kubernetes cluster
request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
// Number of application instances
request.setReplicas(2);
// CPU and memory quotas. Value 0 means no quota is enforced.
request.setLimitCpu(0);
request.setLimitMem(0);
request.setRequestsCpu(0);
request.setRequestsMem(0);
// Microservice namespace ID. Omit this parameter to use the default namespace.
request.setLogicalRegionId("cn-hangzhou:doc");
// Image repository name and full image address
request.setRepoId("image-demo-project");
request.setImageUrl("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:1.0");Replace the values in the code with your actual configuration:
| Parameter | Description | Example |
|---|---|---|
AppName | Application name | edas-SDK0311image |
ClusterId | Target Kubernetes cluster ID | da60f685-558a-4e00-b549-15e9143d**** |
Replicas | Number of application instances | 2 |
LimitCpu | Maximum CPU cores per instance. 0 disables the limit. | 0 |
LimitMem | Maximum memory (MiB) per instance. 0 disables the limit. | 0 |
RequestsCpu | Reserved CPU cores per instance. 0 means no reservation. | 0 |
RequestsMem | Reserved memory (MiB) per instance. 0 means no reservation. | 0 |
LogicalRegionId | Microservice namespace ID. Omit to use the default namespace. | cn-hangzhou:doc |
ImageUrl | Full image address including registry, namespace, repository, and tag | registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:1.0 |
RepoId | Image repository name | image-demo-project |
Setting LimitCpu and LimitMem to 0 disables resource quotas. This is acceptable for testing but not for production. For production deployments, set appropriate CPU and memory limits to prevent resource contention.
This example omits advanced parameters such as scheduling rules, startup commands, and environment variables. For the full parameter list, see InsertK8sApplication.
Step 3: Submit the request and check the response
Send the request to EDAS. On success, EDAS creates a change order that schedules application instances on the target cluster, pulls the specified image, and starts the containers. The response returns the AppId and AppName of the newly created application.
try {
InsertK8sApplicationResponse response = client.getAcsResponse(request);
System.out.println("AppId=" + response.getApplicationInfo().getAppId()
+ "\nAppName=" + response.getApplicationInfo().getAppName());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}Expected output:
AppId=d8130adc-2298-49ea-9c64-f19165fa****
AppName=edas-SDK0311imageStep 4: Verify the deployment
After submitting the deployment request, call GetChangeOrderInfo to track the change order progress. For parameter details, see GetChangeOrderInfo.
The changeOrderInfo.Status field indicates the deployment state:
| Status value | Meaning |
|---|---|
0 | Ready |
1 | In progress |
2 | Successful |
3 | Failed |
6 | Terminated |
8 | Waiting for manual trigger (manual phased release) |
9 | Waiting for automatic trigger (automatic phased release) |
10 | Failed due to system exception |
Sample response (abbreviated):
{
"Message": "success",
"RequestId": "C26DBA1C-9A38-442D-A4D5-587515C5E522",
"Code": 200,
"changeOrderInfo": {
"Status": 3,
"Desc": "Version: 20210423:1442 | Package Name: sc-provider-D-0.0.1-SNAPSHOT.jar",
"PipelineInfoList": {
"PipelineInfo": [
{
"TaskMessage": "Apply success. <br>application is ready at desired state, version: 1"
}
]
}
}
}If changeOrderInfo.Status returns 3 (failed), check the TaskMessage field for error details and troubleshoot the issue before retrying the deployment.
Related information
InsertK8sApplication API reference -- complete parameter list, including scheduling rules, startup commands, and environment variables
Manage Kubernetes clusters by calling API operations -- import and manage clusters programmatically
Use EDAS SDK for Java to call EDAS API -- SDK installation and authentication