Manage Kubernetes clusters by calling API operations

更新时间:
复制 MD 格式

Use the Enterprise Distributed Application Service (EDAS) SDK for Java to import Kubernetes clusters into EDAS and cancel cluster imports programmatically. After import, EDAS provides application-level management, release strategies, microservice governance, and CI/CD integration on top of your existing Kubernetes infrastructure.

Prerequisites

Before you begin, make sure you have:

If a cluster or namespace already exists, retrieve its ID by calling the corresponding API operation:

Supported cluster types

EDAS supports importing the following ACK cluster types:

ACK cluster typeEDAS cluster type after import
ACK dedicated clusterACK cluster
ACK managed clusterACK cluster
ACK Serverless clusterACK Serverless cluster
Hybrid cloud cluster (third-party or self-managed)ACK cluster

For a comparison of ACK cluster types, see What is ACK?

Register hybrid cloud clusters (Kubernetes clusters deployed on third-party clouds or self-managed clusters in data centers) in the ACK console before importing them to EDAS. See Create a cluster registration proxy and register a Kubernetes cluster.

What EDAS adds to Kubernetes

Importing a Kubernetes cluster into EDAS provides the following capabilities beyond standalone Kubernetes:

CapabilityDescription
Application-level managementManage Kubernetes workloads (deployments, pods) at the application level with cross-zone high-availability deployment
Release strategiesPerform phased and canary releases based on traffic ratio or request parameters, with full change tracking
CI/CD integrationIntegrate with mainstream DevOps systems for continuous integration and delivery
Microservice governanceRun Spring Cloud and Dubbo applications commercially available in the past five years without code changes. Includes graceful shutdown, stress testing during release, service authentication, throttling, degradation, outlier ejection, and service query and testing
Production safetyMonitor releases end-to-end with release reports, configure canary release policies, and roll back with one click

Import a Kubernetes cluster

Call the ImportK8sCluster operation to import a cluster into a specified microservice namespace.

Parameters

ParameterTypeRequiredDescription
ClusterIdStringYesThe ID of the Kubernetes cluster to import
NamespaceIdStringYesThe target microservice namespace (for example, cn-hangzhou:doc)
EnableAsmBooleanNoWhether to install a service mesh. true: install. false: skip

Sample code

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)  {
        // To avoid security risks, retrieve your AccessKey pair from environment variables
        // instead of hardcoding them. Use a RAM user instead of your Alibaba Cloud account
        // for least-privilege access.
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");

        // Replace with the region ID where your Kubernetes cluster is deployed
        String region_id = "<region-id>";

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

        ImportK8sClusterRequest request = new ImportK8sClusterRequest();
        request.setClusterId("<cluster-id>");           // Kubernetes cluster ID
        request.setNamespaceId("<namespace-id>");        // Target microservice namespace
        request.setEnableAsm(true);                      // Install a service mesh

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

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
<region-id>Region ID where the cluster is deployedcn-hangzhou
<cluster-id>Kubernetes cluster IDda60f685-558a-4e00-b549-15e9143d****
<namespace-id>Microservice namespace IDcn-hangzhou:doc

Sample response

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

Cancel a cluster import

Call the DeleteCluster operation to cancel a cluster import.

Parameters

ParameterTypeRequiredDescription
ClusterIdStringYesThe ID of the Kubernetes cluster to remove from EDAS

Sample code

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)  {
        // To avoid security risks, retrieve your AccessKey pair from environment variables
        // instead of hardcoding them. Use a RAM user instead of your Alibaba Cloud account
        // for least-privilege access.
        String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
        String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");

        // Replace with the region ID where your Kubernetes cluster is deployed
        String region_id = "<region-id>";

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

        DeleteClusterRequest request = new DeleteClusterRequest();
        request.setClusterId("<cluster-id>");    // Kubernetes cluster ID to remove from EDAS

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

Sample response

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

A Data value of true confirms the import was canceled.

Verify the result

After importing or canceling a cluster import, call the GetK8sCluster operation and check the ClusterImportStatus field in the response.

Sample response

{
  "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
}

ClusterImportStatus values

ValueStatus
0Not imported
1Imported
2Import failed
3Importing
4Deleted

To confirm a successful import, verify that ClusterImportStatus is 1. To confirm a canceled import, verify that ClusterImportStatus is 0.