删除 Google Cloud Managed Service for Apache Kafka 集群

如需删除集群,您可以使用 Google Cloud 控制台、Google Cloud CLI、客户端库或 Managed Kafka API。您无法使用开源 Apache Kafka API 删除集群。

删除集群所需的角色和权限

如需获得删除集群所需的权限,请让您的管理员为您授予项目的 Managed Kafka Cluster Editor (roles/managedkafka.clusterEditor) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色包含删除集群所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

您需要具备以下权限才能删除集群:

  • 删除集群的集群权限: managedkafka.clusters.delete
  • 对包含集群的位置拥有“列出所有集群”权限。只有使用 Google Cloud删除集群时,才需要此权限: managedkafka.clusters.list

您也可以使用自定义角色或其他预定义角色来获取这些权限。

借助 Managed Kafka Cluster Editor 角色,您无法在 Managed Service for Apache Kafka 集群上创建、删除或修改主题和消费者群组。它也不允许数据平面访问在集群内发布或使用消息。如需详细了解此角色,请参阅 Managed Service for Apache Kafka 预定义角色

删除集群

以下是删除集群之前需要考虑的一些重要事项:

  • 数据丢失:删除集群会清除其中存储的所有数据,包括主题、消息、配置和任何其他关联的资源。此操作无法撤消。

  • 服务中断:依赖于集群的任何应用或服务都会失去访问权限并遇到中断。在删除集群之前,请确保您已制定处理此依赖项的方案。

  • 结算:删除集群后,您将不再需要为该集群支付费用。 不过,您可能仍需支付删除之前所用资源的费用。

  • 异步操作:默认情况下,删除命令以异步方式运行。该方法会立即返回,您可以单独跟踪删除进度。

如需删除集群,请按照以下步骤操作:

控制台

  1. 在 Google Cloud 控制台中,前往集群页面。

    转到“集群”

  2. 从集群列表中,选择要删除的集群。

  3. 点击删除

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 运行 gcloud managed-kafka clusters delete 命令:

    gcloud managed-kafka clusters delete CLUSTER_ID \
        --location=LOCATION
    

    替换以下内容:

    • CLUSTER_ID:集群的 ID 或名称。
    • LOCATION:集群的位置。
  3. REST

    在使用任何请求数据之前,请先进行以下替换:

    • PROJECT_ID:您的 Google Cloud 项目 ID
    • LOCATION:集群的位置
    • CLUSTER_ID:集群的 ID

    HTTP 方法和网址:

    DELETE https://managedkafka.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/clusters/CLUSTER_ID

    如需发送您的请求,请展开以下选项之一:

    您应该收到类似以下内容的 JSON 响应:

    {
      "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.managedkafka.v1.OperationMetadata",
        "createTime": "CREATE_TIME",
        "target": "projects/PROJECT_ID/locations/LOCATION/clusters/CLUSTER_ID",
        "verb": "delete",
        "requestedCancellation": false,
        "apiVersion": "v1"
      },
      "done": false
    }
    

    Go

    在尝试此示例之前,请按照 安装客户端库中的 Go 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Go API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据(ADC)。 如需了解详情,请参阅为本地开发环境设置 ADC

    import (
    	"context"
    	"fmt"
    	"io"
    
    	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
    	"google.golang.org/api/option"
    
    	managedkafka "cloud.google.com/go/managedkafka/apiv1"
    )
    
    func deleteCluster(w io.Writer, projectID, region, clusterID string, opts ...option.ClientOption) error {
    	// projectID := "my-project-id"
    	// region := "us-central1"
    	// clusterID := "my-cluster"
    	ctx := context.Background()
    	client, err := managedkafka.NewClient(ctx, opts...)
    	if err != nil {
    		return fmt.Errorf("managedkafka.NewClient got err: %w", err)
    	}
    	defer client.Close()
    
    	clusterPath := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", projectID, region, clusterID)
    	req := &managedkafkapb.DeleteClusterRequest{
    		Name: clusterPath,
    	}
    	op, err := client.DeleteCluster(ctx, req)
    	if err != nil {
    		return fmt.Errorf("client.DeleteCluster got err: %w", err)
    	}
    	err = op.Wait(ctx)
    	if err != nil {
    		return fmt.Errorf("op.Wait got err: %w", err)
    	}
    	fmt.Fprint(w, "Deleted cluster\n")
    	return nil
    }
    

    Java

    在尝试此示例之前,请按照 安装客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Java API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置 ADC

    
    import com.google.api.gax.longrunning.OperationFuture;
    import com.google.api.gax.longrunning.OperationSnapshot;
    import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
    import com.google.api.gax.retrying.RetrySettings;
    import com.google.api.gax.retrying.TimedRetryAlgorithm;
    import com.google.api.gax.rpc.ApiException;
    import com.google.cloud.managedkafka.v1.ClusterName;
    import com.google.cloud.managedkafka.v1.DeleteClusterRequest;
    import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
    import com.google.cloud.managedkafka.v1.ManagedKafkaSettings;
    import com.google.cloud.managedkafka.v1.OperationMetadata;
    import com.google.protobuf.Empty;
    import java.io.IOException;
    import java.time.Duration;
    
    public class DeleteCluster {
    
      public static void main(String[] args) throws Exception {
        // TODO(developer): Replace these variables before running the example.
        String projectId = "my-project-id";
        String region = "my-region"; // e.g. us-east1
        String clusterId = "my-cluster";
        deleteCluster(projectId, region, clusterId);
      }
    
      public static void deleteCluster(String projectId, String region, String clusterId)
          throws Exception {
    
        // Create the settings to configure the timeout for polling operations
        ManagedKafkaSettings.Builder settingsBuilder = ManagedKafkaSettings.newBuilder();
        TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create(
            RetrySettings.newBuilder()
                .setTotalTimeoutDuration(Duration.ofHours(1L))
                .build());
        settingsBuilder.deleteClusterOperationSettings()
            .setPollingAlgorithm(timedRetryAlgorithm);
    
        try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create(
            settingsBuilder.build())) {
          DeleteClusterRequest request =
              DeleteClusterRequest.newBuilder()
                  .setName(ClusterName.of(projectId, region, clusterId).toString())
                  .build();
          OperationFuture<Empty, OperationMetadata> future =
              managedKafkaClient.deleteClusterOperationCallable().futureCall(request);
    
          // Get the initial LRO and print details. CreateCluster contains sample code for polling logs.
          OperationSnapshot operation = future.getInitialFuture().get();
          System.out.printf("Cluster deletion started. Operation name: %s\nDone: %s\nMetadata: %s\n",
              operation.getName(),
              operation.isDone(),
              future.getMetadata().get().toString());
    
          future.get();
          System.out.println("Deleted cluster");
        } catch (IOException | ApiException e) {
          System.err.printf("managedKafkaClient.deleteCluster got err: %s", e.getMessage());
        }
      }
    }
    

    Python

    在尝试此示例之前,请按照 安装客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Python API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置 ADC

    from google.api_core.exceptions import GoogleAPICallError
    from google.cloud import managedkafka_v1
    
    # TODO(developer)
    # project_id = "my-project-id"
    # region = "us-central1"
    # cluster_id = "my-cluster"
    
    client = managedkafka_v1.ManagedKafkaClient()
    
    request = managedkafka_v1.DeleteClusterRequest(
        name=client.cluster_path(project_id, region, cluster_id),
    )
    
    try:
        operation = client.delete_cluster(request=request)
        print(f"Waiting for operation {operation.operation.name} to complete...")
        operation.result()
        print("Deleted cluster")
    except GoogleAPICallError as e:
        print(f"The operation failed with error: {e.message}")
    

后续步骤

Apache Kafka® 是 Apache Software Foundation 或其关联公司在美国和/或其他国家/地区的注册商标。