All Products
Search
Document Center

Enterprise Distributed Application Service:Create a Java application from an image

Last Updated:Jun 12, 2023

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call an API operation to create a Java application from an image in a Kubernetes cluster.

Prerequisites

  • EDAS SDK for Java is installed. For more information, see Use EDAS SDK for Java to call EDAS API.
  • The ID of the region where you want to deploy the application is obtained.

  • A microservice namespace is created. For more information, see Create a namespace.

    If a microservice namespace already exists, you can call the ListUserDefineRegion operation to query the microservice namespace and obtain the region ID (RegionId) of the microservice namespace.

  • The destination cluster is imported into the microservice namespace. In this example, the cluster whose ID is da60f685-558a-4e00-b549-15e9143d**** is used. For more information, see Manage Kubernetes clusters by calling API operations.

    If a cluster is already imported into the microservice namespace, you can call the ListCluster operation to query the cluster and obtain the cluster ID (ClusterId).

  • The image from which you want to create the application is stored in the image repository. Sample image repository: image-demo-project.

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

Create a Java application from an image

The following code provides an example on how to create an application from an image in a Kubernetes cluster in EDAS. Advanced parameters related to scheduling rules, startup commands, and environment variables are not specified in the code. For more information about API parameters, see InsertK8sApplication .

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)  {
        // 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 Resource Access Management (RAM) console. 
        // In this example, the AccessKey ID and AccessKey secret are saved in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file based on your business requirements. 
        // To prevent password leaks, we recommend that you do not save your AccessKey ID and AccessKey secret to your 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 you want to create the application. 
        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 configure parameters. 
        InsertK8sApplicationRequest request = new InsertK8sApplicationRequest();
        // The name of the application. 
        request.setAppName("edas-SDK0311image");
        // The ID of the cluster. 
        request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
        // The number of application instances. 
        request.setReplicas(2);
        // The CPU quota and memory quota for application instances. The value 0 indicates that no quotas are specified. 
        request.setLimitCpu(0);
        request.setLimitMem(0);
        request.setRequestsCpu(0);
        request.setRequestsMem(0);
        // The ID of the EDAS microservice namespace. If you do not want to use the default microservice namespace, set a value for this parameter. 
        request.setLogicalRegionId("cn-hangzhou:doc");
        // The name of the image repository and the image address. 
        request.setImageUrl("registry-vpc.cn-hangzhou.aliyuncs.com/image-demo-project/provider:1.0");
        request.setRepoId("image-demo-project");

        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();
        }
    }
}

Sample response:

AppId=d8130adc-2298-49ea-9c64-f19165fa****
AppName=edas-SDK0311image

Verify the result

After you create the application, you can call the GetChangeOrderInfo operation to view the change details and obtain the change state of the application. For more information about parameters in the operation, see GetChangeOrderInfo.

The following sample code provides an example of the response that is returned after you call the GetChangeOrderInfo operation:

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

You can check whether the application is created based on the value of the changeOrderInfo.Status parameter. Valid values of the changeOrderInfo.Status parameter:

  • 0: ready

  • 1: in process

  • 2: successful

  • 3: failed

  • 6: terminated

  • 8: waiting for you to manually trigger the next phase during a manual phased release

  • 9: waiting for the system to trigger the next phase during an automatic phased release.

  • 10: failed to be created due to a system exception

Note

If the value 3 is returned for the changeOrderInfo.Status parameter, the application failed to be created. You can troubleshoot the issue based on the value of the TaskMessage parameter. After the issue is fixed, create the application again.