All Products
Search
Document Center

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

Last Updated:Jun 14, 2023

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

Prerequisites

    Background information

    If you want to call an operation to release an application in phases, we recommend that you use the automatic phased-release feature. If you use the manual phased-release feature, you must call the ContinuePipeline operation to manually confirm the release of the next phase. For more information, see Manually confirm the next phase.

    The following are two sample policies for phased-release. You can select and modify a policy based on your business requirements.

    • Manually release an application in two phases

      {"type":"BatchUpdate","batchUpdate":{"batch":2,"releaseType":"manual"}}
    • Automatically release an application in two phases at an interval of 2 minutes

      {"type":"BatchUpdate","batchUpdate":{"batch":2,"releaseType":"auto","batchWaitTime":2}}

    Release an application in phases by using a JAR or WAR file

    The following sample code provides an example on how to release an application in phases by using a JAR file in a Kubernetes cluster in EDAS. 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 release an application, add the following parameters to the sample code based on your business requirements:

    //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 files. 
    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 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 save your AccessKey ID and AccessKey secret to the code file. 
            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 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("2021-04-15 14:01:12");
            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 custom policy for phased-release. In this example, the application is automatically released in two phases at an interval of 2 minutes. 
            request.setUpdateStrategy("{\"type\":\"BatchUpdate\",\"batchUpdate\":{\"batch\":2,\"releaseType\":\"auto\",\"batchWaitTime\":2}}");
            // The number of pods. 
            request.setReplicas(3);
            // The CPU quota and the memory quota of the 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. A value of 0 indicates that the minimum number of CPU cores has no limit. 
            request.setMcpuRequest(0);
            // The maximum number of CPU cores that is 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("Phased release (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=e16bdf30-00c9-4b12-87b5-0ad35f3f****
    Message=success

    Release an application in phases by using an image

    The following sample code provides an example on how to release an application in phases by using an image in a Kubernetes cluster in EDAS. 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

    In this example, the manual phased-release mode is used. You can change to the automatic phased-release mode based on your business requirements.

    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 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 address of the image. 
            request.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/edas-demo-project/provider:2.0");
            // The custom policy for phased-release. In this example, the application is manually released in two phases. You can change to the automatic phased-release mode based on your business requirements. 
            request.setUpdateStrategy("{"type":"BatchUpdate","batchUpdate":{"batch":2,"releaseType":"manual"}}");
            // The number of pods. 
            request.setReplicas(3);
            // The CPU quota and the memory quota of the 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 is 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("Phased-release (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=7ed96c93-cbd5-4614-a304-861ede6c****
    Message=success

    Check the result

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

    Sample returned response after you call the GetChangeOrderInfo operation:

    • Change details of the application that is released in phases by using a JAR or WAR file

      {
        "Message": "success",
        "RequestId": "DA4E96FA-4C75-486A-A4F4-30F62C9EA53C",
        "Code": 200,
        "changeOrderInfo": {
          "Status": 2,
          "Phased release (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 phases by using an image

      {
        "Message": "success",
        "RequestId": "FE1722D2-4B81-467C-9828-EFFBA3B3F565",
        "Code": 200,
        "changeOrderInfo": {
          "Status": 8,
          "Desc": "Phased-release (image)",
          "PipelineInfoList": {
            "PipelineInfo": [
              {
                ......
               "PipelineId": "4ff02b63-e14d-4124-86ff-22239f80****"
                ......
                "PipelineId": "6dbbfff8-bcf7-4300-aa59-ccb06909****"
                 .......
                 "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 application is released in phases 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 for manual confirmation to trigger the next phase during a manual phased-release

    • 9: Waiting 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 in phases. To find the cause of the failure, check the value of the TaskMessage parameter. After the issue is fixed, release the application in phases again.

    Manually confirm the next phase

    If you release an application in manual phased-release mode, you must call the ContinuePipeline operation to manually confirm the release of the next phase.

    1. Call the GetChangeOrderInfo operation to query the pipeline ID (PipelineId) of each phase.

      Sample returned response after you call the GetChangeOrderInfo operation:

      {
        "Message": "success",
        "RequestId": "FE1722D2-4B81-467C-9828-EFFBA3B3F565",
        "Code": 200,
        "changeOrderInfo": {
          "Status": 8,
          "Desc": "Phased-release (image)",
          "PipelineInfoList": {
            "PipelineInfo": [
              {
                ......
               "PipelineId": "4ff02b63-e14d-4124-86ff-22239f80****"
                ......
                "PipelineId": "6dbbfff8-bcf7-4300-aa59-ccb06909****"
                 .......
                 "TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
                 ......
                // The sample response is incomplete and is only for reference.

      Note

      In this example, the application is manually released in two phases. Two pipeline IDs are returned after you call the GetChangeOrderInfo operation. The second pipeline ID indicates the phase that you need to manually confirm.

      Similarly, if you manually release the application in three phases, three pipeline IDs are returned. The second and third pipeline IDs indicate the phases that you need to manually confirm.

    2. Call the ContinuePipeline operation to manually confirm the release of the next phase.

      Sample returned response after you call the ContinuePipeline operation:

      {
        "Message": "success",
        "RequestId": "B8ECF6F7-672D-40E9-91DD-1C33F06D4FD8",
        "Code": 200
      }
      Note

      In this example, the application is manually released in two phases. You need to manually confirm only one phase. If the application is released in more than two phases, repeat this step based on the correct pipeline ID to confirm phases one by one.

      Before you repeat this step, take note of the interval between two phases. You can manually confirm the next phase only after an interval elapses. By default, the interval of two phases is 10 seconds.