All Products
Search
Document Center

Enterprise Distributed Application Service:Manage a Kubernetes cluster by calling API operations

Last Updated:Mar 18, 2024

This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to import a Kubernetes cluster and delete a Kubernetes cluster by calling different API operations.

Prerequisites

Before you import or delete a Kubernetes cluster, make sure that the following prerequisites are met:

  • EDAS SDK for Java is installed. For more information, see Use EDAS SDK for Java to call EDAS API.

  • The ID of the region in which the Kubernetes cluster resides is obtained. In this example, the region ID is cn-hangzhou.

  • The Kubernetes cluster is created in the Container Service for Kubernetes (ACK) console. The following section describes how to create different types of clusters:

    If an ACK cluster or ASK cluster is already created, call the GetK8sCluster operation to query the cluster ID specified by the ClusterId parameter. In this example, the cluster ID is da60f685-558a-4e00-b549-15e9143d****.

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

    If a microservice namespace already exists, you can call the ListUserDefineRegion operation to query custom microservice namespaces and obtain the ID of the microservice namespace from the RegionId parameter.

Features of Kubernetes clusters in EDAS

EDAS allows you to import the ACK dedicated clusters, ACK managed clusters, and ACK Serverless (ASK) clusters that you create in the ACK console. For more information about the comparison among the three types of clusters, see What is ACK?

ACK dedicated clusters and ACK managed clusters that are imported to EDAS correspond to ACK clusters in EDAS. ACK Serverless clusters that are imported to EDAS correspond to ACK Serverless clusters in EDAS.

Before you import hybrid cloud clusters such as Kubernetes clusters deployed on third-party clouds or self-managed Kubernetes clusters deployed in data centers to EDAS, you must register the clusters in the ACK console. Hybrid cloud clusters that are imported to EDAS correspond to ACK clusters in EDAS.

For more information about how to register a hybrid cloud cluster in the ACK console, see Create a cluster registration proxy and register a Kubernetes cluster that is deployed in a data center.

Kubernetes clusters that are managed in EDAS have the following advantages over self-managed Kubernetes clusters:

  • On top of cloud-native Kubernetes, EDAS provides the application hosting capability to implement open source microservice governance and lightweight O&M of Kubernetes applications from the application perspective.

    • From the application-centric perspective, EDAS manages Kubernetes-native workloads such as deployments and pods, and provides high-availability deployment of instances across zones.

    • EDAS provides phased release and canary release based on the traffic ratio and request parameters. EDAS monitors the entire change process and therefore makes your change records traceable.

    • EDAS integrates with mainstream DevOps systems to help enterprises implement continuous integration (CI) and continuous delivery (CD). This way, EDAS helps enterprises reduce costs and improve efficiency.

  • On top of open source microservice systems, you can migrate the microservice-oriented applications that you build based on the Spring Cloud and Dubbo frameworks commercially available in the past five years to EDAS without the need to modify code. EDAS supports the following microservice governance capabilities for all the application frameworks:

    • Graceful shutdown and stress testing during application release

    • Service authentication, throttling and degradation, and outlier ejection during the application runtime

    • Service query and testing in application O&M

  • Alibaba Cloud packages its idea of supporting observability, canary release, and rollback for application production security into services, and therefore allows you to immediately implement production security.

    • Observability: End-to-end monitoring in multiple dimensions is implemented based on application overview, release change records, and automatic generation of release reports.

    • Canary release: Canary release is supported for applications based on the traffic ratio or request content policy configuration.

    • Rollback: One-click rollback is supported during the release process, and applications that are running can be rolled back to an earlier version.

Import a Kubernetes cluster

The following code provides an example on how to import a Kubernetes cluster into the specified namespace.

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.ImportK8sClusterRequest;
import com.aliyuncs.edas.model.v20170801.ImportK8sClusterResponse;

public class ImportK8sCluster {

    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. To avoid security risks, we recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. You can log on to the RAM console to create a RAM user. 
        // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables. You can save your AccessKey ID and AccessKey secret in your configuration file based on your business requirements. 
        // To prevent key leaks, we recommend that you do not save 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 in which the Kubernetes cluster 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 configure the related parameters. 
        ImportK8sClusterRequest request = new ImportK8sClusterRequest();
        // The ID of the Kubernetes cluster. 
        request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");
        // The microservices namespace into which the Kubernetes cluster is imported. 
        request.setNamespaceId("cn-hangzhou:doc");
        // Specify whether to install a service mesh for the cluster. Valid values: true and false. A value of true specifies that a service mesh is installed. A value of false specifies that a service mesh is not installed. 
        request.setEnableAsm(true);


        try {
            ImportK8sClusterResponse response = client.getAcsResponse(request);
            System.out.println("Data=" + response.getData() + "\nMessage=" + response.getMessage()+ "\nRequestId=" + response.getRequestId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

The following result is returned after you run the sample code:

Data=4d45f6db-2942-4747-a7ba-b0d43273****
Message=success
RequestId=847CF607-B352-4A71-828A-C0F8DECDEDF6

Cancel the import of a cluster

The following code provides an example on how to cancel the import of a cluster:

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.DeleteClusterRequest;
import com.aliyuncs.edas.model.v20170801.DeleteClusterResponse;

public class DeleteCluster {

    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. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M. You can log on to the RAM console to create a RAM user. 
        // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables. You can save your AccessKey ID and AccessKey secret in your configuration file based on your business requirements. 
        // To prevent key leaks, we recommend that you do not save 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 in which the Kubernetes cluster 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 configure the related parameters. 
        DeleteClusterRequest request = new DeleteClusterRequest();
        // The ID of the Kubernetes cluster. 
        request.setClusterId("da60f685-558a-4e00-b549-15e9143d****");


        try {
            DeleteClusterResponse response = client.getAcsResponse(request);
            System.out.println("Data=" + response.getData() + "\nMessage=" + response.getMessage()+ "\nRequestId=" + response.getRequestId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

The following result is returned after you run the sample code:

Data=true
Message=success
RequestId=68CDDEC0-593E-4C1A-9CDC-96F9FEF0924E

Verify the result

After you import or cancel the import of a Kubernetes cluster, call the GetK8sCluster operation to query the import state specified by the ClusterImportStatus parameter in the result returned.

The following result is returned after you call the GetK8sCluster operation:

{
  "RequestId": "809FCC08-FFE1-417D-8ADF-F5D5BFD968D7",
  "Message": "success",
  "ClusterPage": {
    "ClusterList": {
      "Cluster": [
        {
          "ClusterImportStatus": 1,
          "NodeNum": 6,
          "ClusterId": "da60f685-558a-4e00-b549-15e9143d****",
          ......
        },
        {
          "ClusterImportStatus": 0,
          "NodeNum": 3,
          "ClusterId": "6dca9bc0-b928-4f2c-ae76-396bd108****",
          ......
        }
      ]
    },
    "PageSize": 12,
    "CurrentPage": 0,
    "TotalSize": 12
  },
  "Code": 200
}

Check whether the cluster is imported or whether the import of the cluster is canceled based on the value of the ClusterImportStatus parameter. Valid values of the ClusterImportStatus parameter:

  • 0: The cluster is not imported.

  • 1: The cluster is imported.

  • 2: The cluster failed to be imported.

  • 3: The cluster is being imported.

  • 4: The cluster is deleted.