This consists of a 3-node Apache Kafka cluster using Docker, with a Zookeeper ensemble created to manage the Kafka cluster.
The kafka containers are: kafka1, kafka2, kafak3
The zookeepers containers are: zoo1, zoo2, zoo3
- Clone project to local machine
- Go to root of project directory
- Run
docker-compose up -d
in your terminal
Format:
docker exec -i -t <String: container_name> bash
Example:
docker exec -i -t kafka1 bash
Format:
kafka-topics.sh --create --topic <String: topic> --bootstrap-server <String: server to connect to>
Note:
--bootstrap-server
refers to the kafka servers you want to connect to for this topic.<server to connect to>
is in the format of<hostname : port number>
Example:
kafka-topics.sh --create --topic bulletin --bootstrap-server kafka1:9092, kafka2:9092, kafka3:9092
3. You can either publish (producer role) or view messages (consumer role) to the topic you've just created
a. Publisher Role : Send new messages to the topic
Format:
kafka-console-producer.sh --topic <String: topic> --bootstrap-server <String: server to connect to>
Example:
kafka-console-producer.sh --topic bulletin --bootstrap-server kafka1:9092, kafka2:9092, kafka3:9092
After executing above command, you can start typing in the session
> Hello, this is the first message
> This is the second message
> If you are seeing this, you are a subscriber to this topic
> You can send messages if you run the publisher command
Run Ctrl
+ C
to exit session
b. Subscriber Role : View messages that were published to the topic
Format:
kafka-console-consumer.sh --topic <String: topic> --from-beginning --bootstrap-server <String: server to connect to>
Example:
kafka-console-consumer.sh --topic bulletin --from-beginning --bootstrap-server kafka1:9092, kafka2:9092, kafka3:9092
After executing above command, you can view all the messages published to this topic
Hello, this is the first message
This is the second message
If you are seeing this, you are a subscriber to this topic
You can send messages if you run the publisher command
Run Ctrl
+ C
to exit session
Simply replace the <container_name>
of step 1 to:
- kafka1 or kafka2 or kafka3
Format:
docker exec -i -t <String: container_name> bash
Example:
docker exec -i -t zoo1 bash
if it's a leader, it means this zookeeper is the master node in the cluster
zkServer.sh status
To exit bash session for this zookeeper container, run Ctrl
+ D
Format:
docker stop <String: container_name>
Example:
Scenario - if zoo1 is the master node of the cluster, terminate zoo1 container
docker stop zoo1
You can verify that another node has become the new leader of the kafka cluster by running step 1 - 2 on the other zookeeper containers.
Simply replace the <container_name>
of step 1 to either:
- zoo1 or zoo2 or zoo3