# Create and view a Managed Service for Apache Kafka cluster

Learn how to create a Managed Service for Apache Kafka cluster with a topic,
and view details about the cluster. You can use the Google Cloud console or the
Google Cloud CLI to complete this tutorial.

## Before you begin

### Console


<br />

### gcloud


<br />

### Required roles


To get the permissions that
you need to create the cluster,

ask your administrator to grant you the
following IAM roles on the project:

- [Managed Kafka Cluster Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/managedkafka.md.txt#managedkafka.clusterEditor) (`roles/managedkafka.clusterEditor`)
- [Managed Kafka Topic Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/managedkafka.md.txt#managedkafka.topicEditor) (`roles/managedkafka.topicEditor`)
- [Compute Network User](https://docs.cloud.google.com/iam/docs/roles-permissions/compute.md.txt#compute.networkUser) (`roles/compute.networkUser`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

## Create a cluster

### Console

1. Go to the **Managed Service for Apache Kafka** \> **Clusters** page.

   [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)
2. Click **Create**.
3. In the **Cluster name** box, enter a name for the cluster.
4. In the **Region** list, select a location for the cluster.
5. For **Network configuration** , configure the subnet where the cluster is accessible:
   1. For **Project**, select your project.
   2. For **Network**, select the VPC network.
   3. For **Subnet**, select the subnet.
   4. Click **Done**.
6. Click **Create**.

After you click **Create** , the cluster state is `Creating`. When the cluster
is ready, the state is `Active`.

### gcloud


To create a Kafka cluster, run the
[`managed-kafka clusters
create`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/clusters/create) command.

```bash
gcloud managed-kafka clusters create KAFKA_CLUSTER \
--location=REGION \
--cpu=3 \
--memory=3GiB \
--subnets=projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME \
--async
```

Replace the following:

- `KAFKA_CLUSTER`: a name for the Kafka cluster
- `REGION`: the location of the cluster
- `PROJECT_ID`: your project ID
- `SUBNET_NAME`: the subnet where you want to create the cluster, for example `default`


For information about supported locations, see
[Managed Service for Apache Kafka locations](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/locations.md.txt).

The command runs asynchronously and returns an operation ID:

```bash
Check operation [projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID] for status.
```


To track the progress of the create operation, use the
[`gcloud managed-kafka
operations describe`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/operations/describe) command:

```bash
gcloud managed-kafka operations describe OPERATION_ID \
  --location=REGION
```


When the cluster is ready, the output from this command includes the entry `state:
ACTIVE`. For more information, see
[Monitor the
cluster creation operation](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/create-cluster#monitor_cluster).

After you follow these steps, it can take up to 30 minutes for the cluster to
be ready.

Optionally, while you wait for this operation to complete, look at the tutorials
listed in [What's next](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart-cluster.md.txt#whats-next). They show how to produce messages for the
cluster using various methods. Once the cluster is ready, you can complete any
of those tutorials.

## Create a topic

When the Managed Service for Apache Kafka cluster is ready, create a topic.

### Console

1. Go to the **Managed Service for Apache Kafka** \>
   **Clusters** page.


   [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)

   <br />

2. Click the name of the cluster.

3. In the cluster details page, click
   **Create Topic**.

4. In the **Topic name** box, enter a name for the topic.

5. Click **Create**.

### gcloud

To create a Kafka topic, run the
[`managed-kafka topics create`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/topics/create)
command.

```bash
gcloud managed-kafka topics create KAFKA_TOPIC_NAME \
--cluster=KAFKA_CLUSTER \
--location=REGION \
--partitions=10 \
--replication-factor=3
```

Replace the following:

- `KAFKA_TOPIC_NAME`: the name of the Kafka topic to create
- `KAFKA_CLUSTER`: the name of the Kafka cluster
- `REGION`: the region where you created the Kafka cluster

## View the cluster

Once the cluster is ready, you can view information about the cluster,
including the topics, consumer groups, networks, and other details.

### Console

1. Go to the **Managed Service for Apache Kafka** \>
   **Clusters** page.


   [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)

   <br />

2. Click the name of the cluster. The **Cluster details** page shows
   information about the cluster. The **Resources** tab shows the resources
   associated with the cluster, including topics.

3. To view details about a topic, select the **Resources** tab and click the
   topic name. The **Topic details** page shows information about the topic,
   such as the number of partitions and replicas.

4. To return to the **Cluster details** page, click
   **Back to parent page**.

### gcloud

1. To view information about the cluster, run the
   [`gcloud managed-kafka clusters describe`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/clusters/describe) command.

   ```bash
   gcloud managed-kafka clusters describe KAFKA_CLUSTER \
     --location=REGION
   ```

   Replace the following:
   - `KAFKA_CLUSTER`: the name of the cluster
   - `REGION`: the region where you created the cluster

   The output of this command is a list of cluster properties, similar to the
   following:

       bootstrapAddress: BOOTSTRAP_ADDRESS
       capacityConfig:
         memoryBytes: '3221225472'
         vcpuCount: '3'
       createTime: 'TIMESTAMP'

2. To get a list of topics in the cluster, run the
   [`gcloud managed-kafka topics list`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/topics/list) command.

   ```bash
   gcloud managed-kafka topics list KAFKA_CLUSTER \
     --location=REGION \
     --format="value(name)"
   ```

   The output includes the topics that you created, as well as topics that
   Managed Service for Apache Kafka creates for internal use.
3. To view information about a topic, run the
   [`gcloud managed-kafka topics describe`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/topics/describe) command.

   ```bash
   gcloud managed-kafka topics describe TOPIC_NAME \
     --cluster=KAFKA_CLUSTER \
     --location=REGION
   ```

   Replace `TOPIC_NAME` with the name of the topic.

   The output of this command is a list of topic properties, similar to the
   following:

        name: projects/PROJECT_ID/locations//clusters/KAFKA_CLUSTER/topics/TOPIC_NAME
        partitionCount: 10
        replicationFactor: 3

## Get the bootstrap address

Kafka clients use the cluster's *bootstrap address* to establish a connection
with the cluster. To get the bootstrap address, perform the following steps:

### Console

1. Go to the **Managed Service for Apache Kafka** \>
   **Clusters** page.


   [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)

   <br />

2. Click the name of the cluster.

3. Select the **Configurations** tab.

4. The bootstrap URL is listed under **Bootstrap URL** . Click
   **Copy** to copy the
   value.

### gcloud

To get the bootstrap URL, run the
[`managed-kafka clusters describe`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/clusters/describe)
command.

```bash
gcloud managed-kafka clusters describe KAFKA_CLUSTER \
  --location=REGION \
  --format="value(bootstrapAddress)"
```

Replace the following:

- `KAFKA_CLUSTER`: the name of the cluster
- `REGION`: the region where you created the cluster

You are now ready to send messages to the cluster. For
more information, see [What's next](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart-cluster.md.txt#whats-next) in this document.

## Clean up


To avoid incurring charges to your Google Cloud account for
the resources used on this page, delete the Google Cloud project with the
resources.

### Console

Delete the cluster.

1. Go to the **Managed Service for Apache Kafka** \>
   **Clusters** page.


   [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)

   <br />

2. Select the Kafka cluster and click **Delete**.

### gcloud

1. To delete the cluster, use the
   [`gcloud managed-kafka clusters delete`](https://docs.cloud.google.com/sdk/gcloud/reference/managed-kafka/clusters/delete)
   command.

   ```bash
   gcloud managed-kafka clusters delete KAFKA_CLUSTER \
     --location=REGION --async
   ```

## What's next

Produce messages for Kafka, either by using the Kafka command-line tools, by
generating synthetic data, or by creating a client application:

- [Use the Kafka command-line tools](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart.md.txt)

- [Generate synthetic data](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart-synthetic-data.md.txt)

- [Develop a Java producer application](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart-java.md.txt)

- [Develop a Python producer application](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/quickstart-python.md.txt#create_a_python_producer_application)