All Products
Search
Document Center

Enterprise Distributed Application Service:Call an API operation to implement single-release deployment in a Kubernetes cluster

Last Updated:Jan 24, 2024

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call an API operation to implement single-release deployment of an application by using JAR, WAR, or image files in a Kubernetes cluster.

Prerequisites

Sample image address: registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0.

Implement single-release deployment of an application by using a JAR or WAR file

The following sample code provides an example on how to implement single-release deployment of an application in a Kubernetes cluster by using an API operation. Advanced parameters that are related to scheduling rules, startup commands, and environment variables are not included in the code. For more information about API parameters, see DeployK8sApplication.

Note

If you want to use a WAR file to implement single-release deployment of an application, add the following parameters to the sample code based on your business requirements:

// The version of Tomcat on which the WAR file depends. This parameter is applicable to Spring Cloud and Apache Dubbo applications that are deployed by using WAR files. 
request.setWebContainer("apache-tomcat-7.0.91");  
// The version of EDAS Container on which the WAR file depends. This parameter is applicable to High-speed Service Framework (HSF) applications that are deployed by using WAR files.      
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 pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in EDAS is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        // In this sample code, the AccessKey ID and AccessKey secret are included in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file. 
        // To prevent key leaks, we recommend that you do not include your AccessKey ID and AccessKey secret into the code. 
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
        // The ID of the region where the application resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Initiate an API request and configure the related parameters. 
        DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
        // The ID of the application. 
        request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
        // The number of pods. 
        request.setReplicas(2);
        // The CPU quota and the memory quota of pods. The value 0 indicates that no limits are specified. 
        request.setCpuLimit(0);
        request.setMemoryLimit(0);
        request.setCpuRequest(0);
        request.setMemoryRequest(0);
        // The URL and version of the JAR or WAR file. 
        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 depends. Valid values: Open JDK 7 and Open JDK 8. 
        request.setJDK("Open JDK 8");
        // The minimum number of CPU cores that are required. Unit: cores. The value 0 indicates that the minimum number of CPU cores has no limit. 
        request.setMcpuRequest(0);
        // The maximum number of CPU cores that are allowed. Unit: cores. The value 0 indicates that the maximum number of CPU cores has no limit. 
        request.setMcpuLimit(0);
        // The description of the change record. 
        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 response:

ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=success

Implement single-release deployment of an application by using an image

The following sample code provides an example on how to implement single-release deployment of an application by using an image in a Kubernetes cluster. Advanced parameters that are related to scheduling rules, startup commands, and environment variables are not included in the code. For more information about API parameters, 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 pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in EDAS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        // In this sample code, the AccessKey ID and AccessKey secret are included in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file. 
        // To prevent key leaks, we recommend that you do not include your AccessKey ID and AccessKey secret in the code. 
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
        // The ID of the region where you want to release the application. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Initiate an API request and configure the related parameters. 
        DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
        // The ID of the application. 
        request.setAppId("c44227c7-fc5e-46ca-80ac-8f342d45****");
        // The number of pods. 
        request.setReplicas(2);
        // The URL of the image. 
        request.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:2.0");
        // The CPU quota and the memory quota of pods. The value 0 indicates that no limits are specified. 
        request.setCpuLimit(0);
        request.setMemoryLimit(0);
        request.setCpuRequest(0);
        request.setMemoryRequest(0);
        // The minimum number of CPU cores that are required. Unit: cores. The value 0 indicates that the minimum number of CPU cores has no limit. 
        request.setMcpuRequest(0);
        // The maximum number of CPU cores that are allowed. Unit: cores. The value 0 indicates that the maximum number of CPU cores has no limit. 
        request.setMcpuLimit(0);
        // The description of the change record. 
        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 response:

ChangeOrderId=1c18b409-07ac-4a37-990b-66bd87d7****
Message=success

Check the result

After you implement single-release deployment of an application, you can call the GetChangeOrderInfo operation to view the change details and change status of the application. For more information about specific API parameters, see GetChangeOrderInfo.

Sample returned responses after you call the GetChangeOrderInfo operation:

  • Change details of the application that is released in single-release deployment mode based on a JAR or WAR file

    {
        "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"
                        ......
                        // The sample response is incomplete and is only for reference.

  • Change details of the application that is released in single-release deployment mode based on an image

    {
        "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"
                        ......
                       // The sample response is incomplete and is only for reference.

You can check whether the single-release deployment of the application in the specified Kubernetes cluster is successful based on the value of the changeOrderInfo.Status parameter. Valid values of the changeOrderInfo.Status parameter:

  • 0: Ready

  • 1: In progress

  • 2: Successful

  • 3: Failed

  • 6: Terminated

  • 8: Waiting to manually confirm the next phase during a manual phased-release

  • 9: Waiting to automatically confirm 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 single-release deployment of the application in the specified Kubernetes cluster failed. To find the cause of the failure, check the value of the TaskMessage parameter. After the issue is fixed, retry the single-release deployment of the application in the specified Kubernetes cluster.