Deploy an application to a Kubernetes cluster in a single batch using the Enterprise Distributed Application Service (EDAS) Java SDK. A single-batch deployment updates all pods simultaneously with a new JAR file, WAR file, or container image. Use this SDK approach to integrate deployments into CI/CD pipelines or automate releases programmatically. For manual deployments, use the EDAS console instead.
Prerequisites
Before you begin, make sure that you have:
A Java application running in an EDAS Kubernetes cluster. To create one, see Create from a JAR or WAR file or Create from an image
The application ID (
AppId). Call ListApplication to query itYour deployment artifact uploaded to the target location:
JAR or WAR file -- uploaded to an Object Storage Service (OSS) bucket. Example:
https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.1-SNAPSHOT.jarContainer image -- pushed to an image repository. Example:
registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0
AccessKey ID and AccessKey secret stored as environment variables (
ACCESS_KEY_IDandACCESS_KEY_SECRET). Use a Resource Access Management (RAM) user instead of your Alibaba Cloud account credentials
Deploy with a JAR or WAR file
Call DeployK8sApplication to deploy an application from a JAR file in a single batch. The following example includes only core parameters. For scheduling rules, startup commands, environment variables, and other advanced options, see the DeployK8sApplication API reference.
Core parameters
| Parameter | Method | Description | Example value |
|---|---|---|---|
| AppId | setAppId | Application ID | 6bbc57a2-a017-4bec-b521-49a15bd3**** |
| Replicas | setReplicas | Number of pods | 2 |
| PackageUrl | setPackageUrl | OSS URL of the JAR or WAR file | https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.2-SNAPSHOT.jar |
| PackageVersion | setPackageVersion | Version label for the deployment package | 20210227 |
| JDK | setJDK | JDK version. Valid values: Open JDK 7, Open JDK 8 | Open JDK 8 |
| CpuLimit | setCpuLimit | Maximum CPU cores. 0 means no limit | 0 |
| MemoryLimit | setMemoryLimit | Maximum memory. 0 means no limit | 0 |
| CpuRequest | setCpuRequest | Minimum CPU cores. 0 means no limit | 0 |
| MemoryRequest | setMemoryRequest | Minimum memory. 0 means no limit | 0 |
| McpuRequest | setMcpuRequest | Minimum CPU cores. 0 means no limit | 0 |
| McpuLimit | setMcpuLimit | Maximum CPU cores. 0 means no limit | 0 |
| ChangeOrderDesc | setChangeOrderDesc | Description of this deployment | Single-release deployment of an application based on a JAR file |
Sample 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.DeployK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.DeployK8sApplicationResponse;
public class DeployK8sApplication {
public static void main(String[] args) {
// Get credentials from environment variables to avoid hardcoding secrets.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// Set the region where your application is deployed.
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
// Build the deployment request.
DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
request.setReplicas(2);
request.setPackageUrl("https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.2-SNAPSHOT.jar");
request.setPackageVersion("20210227");
request.setJDK("Open JDK 8");
// Set resource limits. 0 means no limit.
request.setCpuLimit(0);
request.setMemoryLimit(0);
request.setCpuRequest(0);
request.setMemoryRequest(0);
request.setMcpuRequest(0);
request.setMcpuLimit(0);
request.setChangeOrderDesc("Single-release deployment of an application based on a JAR file");
try {
DeployK8sApplicationResponse response = client.getAcsResponse(request);
System.out.println("ChangeOrderId=" + response.getChangeOrderId() + "\nMessage=" + response.getMessage());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}Sample output:
ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=successNote the ChangeOrderId in the response -- you need it to verify the result.
Additional parameters for WAR files
To deploy a WAR file instead of a JAR file, add the following parameters to the request:
// Tomcat version for the WAR file.
// Required for Spring Cloud and Apache Dubbo applications deployed with WAR files.
request.setWebContainer("apache-tomcat-7.0.91");
// EDAS Container version for the WAR file.
// Required for High-speed Service Framework (HSF) applications deployed with WAR files.
request.setEdasContainerVersion("3.5.9");Deploy with a container image
To deploy with an image instead of a JAR or WAR file, replace PackageUrl, PackageVersion, and JDK with the Image parameter:
| Parameter | Method | Description | Example value |
|---|---|---|---|
| Image | setImage | Full image address including the tag | registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0 |
Sample code
The following example shows the complete code for image-based deployment. The only difference from the JAR/WAR example is replacing the package parameters (PackageUrl, PackageVersion, JDK) with Image.
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.DeployK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.DeployK8sApplicationResponse;
public class DeployK8sApplication {
public static void main(String[] args) {
// Get credentials from environment variables to avoid hardcoding secrets.
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);
DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
request.setAppId("c44227c7-fc5e-46ca-80ac-8f342d45****");
request.setReplicas(2);
// Specify the image address instead of a package URL.
request.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0");
// Set resource limits. 0 means no limit.
request.setCpuLimit(0);
request.setMemoryLimit(0);
request.setCpuRequest(0);
request.setMemoryRequest(0);
request.setMcpuRequest(0);
request.setMcpuLimit(0);
request.setChangeOrderDesc("Single-release deployment of an application based on an image");
try {
DeployK8sApplicationResponse response = client.getAcsResponse(request);
System.out.println("ChangeOrderId=" + response.getChangeOrderId() + "\nMessage=" + response.getMessage());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}Sample output:
ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=successVerify the result
After deployment, call GetChangeOrderInfo with the ChangeOrderId from the deployment response to check whether the deployment succeeded.
Status codes
The changeOrderInfo.Status field indicates the deployment state:
| Status code | Meaning |
|---|---|
| 0 | Ready |
| 1 | In progress |
| 2 | Successful |
| 3 | Failed |
| 6 | Terminated |
| 8 | Pending manual confirmation for the next phase (manual phased-release) |
| 9 | Pending automatic confirmation for the next phase (automatic phased-release) |
| 10 | Failed due to a system exception |
Sample response for a JAR or WAR deployment
{
"Message": "success",
"RequestId": "A9DD1E44-DB9E-46D8-9A32-1D45C847E564",
"Code": 200,
"changeOrderInfo": {
"Status": 2,
"Desc": "Single-release deployment of an application (JAR file)",
"PipelineInfoList": {
"PipelineInfo": [
{
"TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
}
]
}
}
}Status: 2 confirms a successful deployment.
Sample response for an image deployment
{
"Message": "success",
"RequestId": "8DEDB234-979D-40AA-A508-187279B1C0D1",
"Code": 200,
"changeOrderInfo": {
"Status": 1,
"Desc": "Single-release deployment of an application (image)",
"PipelineInfoList": {
"PipelineInfo": [
{
"TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
}
]
}
}
}Status: 1 means the deployment is still in progress. Poll GetChangeOrderInfo until the status changes to 2 (successful) or 3 (failed).
Handle failures
If changeOrderInfo.Status returns 3, check the TaskMessage field for the root cause, fix the issue, and retry the deployment.