View a Google Cloud Managed Service for Apache Kafka cluster

To view a cluster, you can use the Google Cloud console, the Google Cloud CLI, the client library, or the Managed Kafka API. You can't use the open source Apache Kafka API to view a cluster.

Required roles and permissions to view a cluster

To get the permissions that you need to view a cluster, ask your administrator to grant you the Managed Kafka Viewer (roles/managedkafka.viewer) IAM role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.

This predefined role contains the permissions required to view a cluster. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to view a cluster:

  • List clusters: managedkafka.clusters.list
  • Get cluster details: managedkafka.clusters.get

You might also be able to get these permissions with custom roles or other predefined roles.

For more information about the Managed Kafka Viewer role, see Managed Service for Apache Kafka predefined roles.

View a cluster

To view a cluster, follow these steps:

Console

  1. In the Google Cloud console, go to the Cluster page.

    Go to Clusters

    The clusters you created in a project are listed.

  2. To view a specific cluster, click a cluster name.
  3. The cluster details page opens. This page has the following tabs:
    • Resources: Displays the list of topics and consumer groups associated with the cluster.
    • Configurations: Displays the configuration of the cluster, including the list of subnets associated with the cluster.
    • Monitoring: Displays the monitoring alerts associated with the cluster.
    • Logs: Displays the logs related to clusters from Logs Explorer.

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. Run the gcloud managed-kafka clusters describe command:

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

    Replace the following:

    • CLUSTER_ID: The ID or name of the cluster.

    • LOCATION: The location of the cluster.

Go

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
	"google.golang.org/api/option"

	managedkafka "cloud.google.com/go/managedkafka/apiv1"
)

func getCluster(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.GetClusterRequest{
		Name: clusterPath,
	}
	cluster, err := client.GetCluster(ctx, req)
	if err != nil {
		return fmt.Errorf("client.GetCluster got err: %w", err)
	}
	fmt.Fprintf(w, "Got cluster: %#v\n", cluster)
	return nil
}

Java

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.managedkafka.v1.Cluster;
import com.google.cloud.managedkafka.v1.ClusterName;
import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
import java.io.IOException;

public class GetCluster {

  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";
    getCluster(projectId, region, clusterId);
  }

  public static void getCluster(String projectId, String region, String clusterId)
      throws Exception {
    try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
      // This operation is being handled synchronously.
      Cluster cluster = managedKafkaClient.getCluster(ClusterName.of(projectId, region, clusterId));
      System.out.println(cluster.getAllFields());
    } catch (IOException | ApiException e) {
      System.err.printf("managedKafkaClient.getCluster got err: %s", e.getMessage());
    }
  }
}

Python

from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# cluster_id = "my-cluster"

client = managedkafka_v1.ManagedKafkaClient()

cluster_path = client.cluster_path(project_id, region, cluster_id)
request = managedkafka_v1.GetClusterRequest(
    name=cluster_path,
)

try:
    cluster = client.get_cluster(request=request)
    print("Got cluster:", cluster)
except NotFound as e:
    print(f"Failed to get cluster {cluster_id} with error: {e.message}")

What's next?

Apache Kafka® is a registered trademark of The Apache Software Foundation or its affiliates in the United States and/or other countries.