Apache Kafka

How to Start Multiple Brokers in Apache Kafka

A Kafka broker is a server that runs the Apache Kafka and is responsible for maintaining the publish-subscribe messaging system in a Kafka cluster. A Kafka cluster can have multiple brokers to provide fault tolerance and increase the overall capacity of the cluster.

Each broker stores one or more partitions of one or more topics and replicates these partitions to other brokers to provide data redundancy. The brokers also handle the routing of incoming messages from the producers to the appropriate partitions and forwarding the messages from the partitions to the consumers.

The broker is responsible for maintaining the order of messages within a partition and for providing the offsets (i.e., the position in a partition) for each message, which is used by the consumers to track their progress through the partition.

The broker also manages the cluster metadata such as the list of topics, the list of partitions for each topic, and the location of the replicas for each partition. The metadata is stored in Zookeeper, and the brokers communicate with Zookeeper to obtain an information about the cluster and coordinate their activities.

This short tutorial teaches us how to create a new Kafka cluster and start the available brokers.

Requirements:
To follow this tutorial, you must ensure that you have an installed Apache Kafka on your target machines. You can also have multiple instances of Apache Kafka that run on the same machine under different addresses.

You can check our tutorial on how to install the Apache Kafka on various operating systems to learn more.

Setup the Brokers
The first step is to assign a unique broker ID to each instance of Apache Kafka that you wish to form the Kafka cluster.

You can do this by editing the “server.properties” file for each Kafka instance. Locate the “broker.id” entry and assign a unique ID to the broker.

For example, you can assign the first Kafka instance an ID of 0, the second one with an ID of 1, and so on.

broker.id=0

Do this for each of the instances that you wish to connect.

Configure the Advertised Listeners

The next step is to specify the advertised listeners for each broker in the “server.properties” configuration file so that the clients can connect to the broker.

Locate the entries and add the addresses for each broker in the “advertised.listeners” entry. An example is shown in the following:

advertised.listeners=PLAINTEXT://192.168.0.100:9092,192.168.0.101:9092

In this case, the setting specifies two listeners using the PLAINTEXT protocol (i.e., unencrypted communication) and listening on IP addresses 192.168.0.100 and 192.168.0.101 on port 9092, respectively.

This setting is essential because the clients, such as producers and consumers, need to know the addresses of the brokers to communicate with the cluster. The “advertised.listeners” setting allows a broker to advertise the different addresses than the ones that it is listening on, which can be helpful in situations where the broker is behind a firewall or a load balancer.

You can do this for each broker in the cluster.

Start the Brokers

Finally, start the brokers with the following command:

./bin/kafka-server-start.sh config/server.properties

Do this for each broker in the cluster.

Once the brokers are up and running, verify that each broker instance has started by checking the log files or using the following command:

./bin/kafka-topics.sh --describe --bootstrap-server <broker-host>:<broker-port> --topic <topic-name> command.

There you have it!

Conclusion

We discussed how we can configure the multiple brokers in a Kafka cluster using the unique broker IDs and advertised addresses.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list