Installing Apache Kafka involves setting up a Kafka broker along with Zookeeper, which manages the cluster metadata and coordinates broker actions. This guide provides step-by-step instructions for installing Kafka on a Linux-based system, along with necessary configurations to start a Kafka server.
Before installing Kafka, ensure that your system meets the following prerequisites:
Ensure that Java is installed on your system. You can check your Java installation by running the following command:
java -version
If Java is not installed, use the following command to install it:
sudo apt-get install default-jdk
Download the latest Kafka binary package from the official Apache Kafka website. Once downloaded, extract the package using the following command:
tar -xzf kafka_2.13-2.8.0.tgz
Navigate to the Kafka directory:
cd kafka_2.13-2.8.0
Kafka requires Zookeeper to be running. Start the Zookeeper server using the following command:
bin/zookeeper-server-start.sh config/zookeeper.properties
Open a new terminal window and start the Kafka server using the following command:
bin/kafka-server-start.sh config/server.properties
Create a new Kafka topic to verify that Kafka is functioning correctly. Use the following command to create a topic named "test-topic":
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Verify the creation of the topic by listing all topics. Use the following command:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Start a Kafka producer to send messages to the "test-topic" using the following command:
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
Type messages and press Enter to send them.
Open a new terminal window and start a Kafka consumer to read messages from the "test-topic" using the following command:
bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
The messages sent by the producer should appear in the consumer's terminal.
After verifying the installation, you can start building applications that use Kafka for messaging. Ensure to secure and optimize your Kafka setup for production use, including configuring log retention, tuning performance, and implementing security measures.