This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call the DeployK8sApplication operation to release an application in a Kubernetes cluster by using a JAR package, a WAR package, or an image.

Prerequisites

Before you call the API operation to release an application at a time, make sure that the following prerequisites are met:

Release an application at a time by using a JAR or WAR package

The following sample code provides an example on how to release an application in a Kubernetes cluster at a time by using a JAR package. For more information about advanced parameters, such as parameters that are related to scheduling rules, startup commands, and environment variables, see DeployK8sApplication.

Note If you want to use a WAR package to deploy an application, add the following two parameters to the sample code as needed:
// The version of Tomcat on which the deployment package of the application depends. This parameter is applicable to Spring Cloud and Apache Dubbo applications that are deployed by using WAR packages. 
request.setWebContainer("apache-tomcat-7.0.91");  
// The version of EDAS Container on which the deployment package of the application depends. This parameter is applicable to High-speed Service Framework (HSF) applications that are deployed by using WAR packages.      
request.setEdasContainerVersion("3.5.9");
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)  {
        // 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 Alibaba Cloud account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the application to be released 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. 
        DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
        // The ID of the application. 
        request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
        // The number of pods for the application. 
        request.setReplicas(2);
        // The CPU quota and memory quota for the pods. A value of 0 indicates that no quotas are set. 
        request.setCpuLimit(0);
        request.setMemoryLimit(0);
        request.setCpuRequest(0);
        request.setMemoryRequest(0);
        // The URL and version of the JAR or WAR package. 
        request.setPackageUrl("https:doc***.oss-cn-hangzhou.aliyuncs.com/sc-****-D-0.0.2-SNAPSHOT.jar");
        request.setPackageVersion("20210227");
        The version of the Java development kit (JDK) on which the deployment package of the application depends. Valid values: Open JDK 7 and Open JDK 8. 
        request.setJDK("Open JDK 8");
        // The minimum number of CPU cores required. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuRequest(0);
        // The maximum number of CPU cores allowed. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuLimit(0);
        // The description of the change record. 
        request.setChangeOrderDesc("Release an application at a time by using a JAR package");

        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 success response:
ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=success

Release an application at a time by using an image

The following sample code provides an example on how to release an application in a Kubernetes cluster at a time by using an image. For more information about advanced parameters, such as parameters that are related to scheduling rules, startup commands, and environment variables, see DeployK8sApplication.

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)  {
        // 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 Alibaba Cloud account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the application to be released 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. 
        DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
        // The ID of the application. 
        request.setAppId("c44227c7-fc5e-46ca-80ac-8f342d45****");
        // The number of pods for the application. 
        request.setReplicas(2);
        // The address of the image. 
        request.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0");
        // The CPU quota and memory quota for the pods. A value of 0 indicates that no quotas are set. 
        request.setCpuLimit(0);
        request.setMemoryLimit(0);
        request.setCpuRequest(0);
        request.setMemoryRequest(0);
        // The minimum number of CPU cores required. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuRequest(0);
        // The maximum number of CPU cores allowed. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuLimit(0);
        // The description of the change record. 
        request.setChangeOrderDesc("Release an application at a time by using 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 success response:
ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=success

Verify the results

After you release an application at a time, you can call the GetChangeOrderInfo operation to view the change details and obtain the status of the application. For more information, see GetChangeOrderInfo.

Sample success responses that are returned after you call the GetChangeOrderInfo operation:
  • Change details for the application that is released at a time by using a JAR or WAR package
    {
        "Message": "success",
        "RequestId": "A9DD1E44-DB9E-46D8-9A32-1D45C847E564",
        "Code": 200,
        "changeOrderInfo": {
            "Status": 2,
            "Desc": "Release an application at a time by using a JAR package",
            "PipelineInfoList": {
                "PipelineInfo": [
                    {
                        ......
                        "TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
                        ......
                        // The sample response is incomplete and is only for reference. 
  • Change details for the application that is released at a time by using an image
    {
        "Message": "success",
        "RequestId": "8DEDB234-979D-40AA-A508-187279B1C0D1",
        "Code": 200,
        "changeOrderInfo": {
            "Status": 1,
            "Desc": "Release an application at a time by using an image",
            "PipelineInfoList": {
                "PipelineInfo": [
                    {
                        ......
                        "TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
                        ......
                       // The sample response is incomplete and is only for reference. 
You can view the value of the changeOrderInfo.Status parameter to check whether the application is released at a time in the specified Kubernetes cluster. Valid values of the changeOrderInfo.Status parameter:
  • 0: ready
  • 1: in progress
  • 2: successful
  • 3: failed
  • 6: terminated
  • 8: wait for manual confirmation to trigger the next phase during a manual phased release
  • 9: wait to trigger the next phase during an automatic phased release
  • 10: failed due to a system exception
Note If a value of 3 is returned for the changeOrderInfo.Status parameter, the application fails to be released at a time. To find the cause of the failure, check the value of the TaskMessage parameter. After the issue is fixed, release the application at a time again.