Production setups of Kafka must have multiple brokers for redundancy. That increases fault tolerance.
That ensures the system uptime even numerous brokers go offline.
Here are the steps to create multiple Kafka brokers.
I create three brokers, so I need three copies.
They are
The listeners must have unique port numbers.
server_0.properties
broker.id=0
listeners=PLAINTEXT://localhost:9092
log.dirs=/tmp/kafka_0-logs
server_1.properties
broker.id=1
listeners=PLAINTEXT://localhost:9093
log.dirs=/tmp/kafka_1-logs
server_2.properties
broker.id=2
listeners=PLAINTEXT://localhost:9094
log.dirs=/tmp/kafka_2-logs
bin/zookeeper-server-start.sh config/zookeeper.properties
Here I have to start all three brokers individually.
bin/kafka-server-start.sh config/server_0.properties
bin/kafka-server-start.sh config/server_1.properties
bin/kafka-server-start.sh config/server_2.properties
Now I need a topic to publish messages. Here is how to create a topic in Kafka.
bin/kafka-topics.sh --create --topic TEST-TOPIC --bootstrap-server localhost:9092,localhost:9093,localhost:9094
Note that I need this topic created across all the brokers. Hence I provide the hosts for all three brokers.
Now I can verify if the topic I created exists in all the brokers.
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
I create a single producer that publishes messages to all three brokers
bin/kafka-console-producer.sh --topic TEST-TOPIC --broker-list localhost:9092,localhost:9093,localhost:9094
The below command creates consumers for each broker I made in the cluster. Repeat it for all the brokers in separate terminal windows. That way, I know that each broker receives the messages published on the topic.
bin/kafka-console-consumer.sh --topic TEST-TOPIC --bootstrap-server localhost:9092
I can listen to all the brokers at once on the fourth window.
bin/kafka-console-consumer.sh --topic TEST-TOPIC --bootstrap-server localhost:9092,localhost:9093,localhost:9094