How to Configure and Test a Single-Node Apache Kafka Cluster
In the previous article we have downloaded the Apache Kafka and also we have extracted the Apache Kafka downloaded file now it is the time to test the Apache Kafka. By default, Kafka stores data in the temporary /tmp directory, which means your data could vanish after a reboot.
In this article, we will configure persistent storage paths for both Zookeeper and Kafka Broker, and then demonstrate a full "Hello World" cycle: creating a topic, producing messages, and consuming them.
Step 1: Configure Zookeeper
Kafka uses Zookeeper to coordinate the cluster. We need to edit its configuration to ensure it stores data in a permanent directory rather than a temporary one.
- Navigate to the config directory: Copy the below command and paste in the terminal to change the directory.
cd kafka/config- Open the properties file: Copy the below and paste in the terminal to open the file.
nano zookeeper.properties- Modify the
dataDir:
Find the line starting with dataDir=/tmp/zookeeper and change it to your desired path (e.g., inside your home directory).
Change:
dataDir=/tmp/zookeeper
cd kafka/config
- Open the properties file: Copy the below and paste in the terminal to open the file.
nano zookeeper.properties
- Modify the
dataDir: Find the line starting withdataDir=/tmp/zookeeperand change it to your desired path (e.g., inside your home directory).
Change:
To:
dataDir=/home/kafkausernew/zookeeper-kafka
(Note: Ensure the directory /home/ubuntu/zookeeper-kafka exists or that the user has permissions to create it.)
- Save and Exit:
Press
CTRL + X, type Y, and hit ENTER.
CTRL + X, type Y, and hit ENTER.Step 2: Configure the Kafka Broker
Next, we will configure the Kafka server (Broker) to store its logs (the actual data messages) in a permanent location.- Open the server properties file: In the same terminal write the below
command and do the configuration inside the server.properties
file:
nano server.properties
- Modify the
log.dirs:
Find the line for log.dirs and update it to a permanent path.
Change:
log.dirs:
Find the line for log.dirs and update it to a permanent path. log.dirs=/tmp/kafka-logs
To:
log.dirs=/home/ubuntu/kafka-logs
- Save and Exit:
Press
CTRL + X, typeY, and hitENTER.
Step 3: Start the Services
Now that our configuration is secure, let's start the cluster.
1. Start Zookeeper
Run the following command in your terminal to run the
zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
2. Start Kafka Broker
Open a new terminal window and type the below command
to run the Kafka server:
bin/kafka-server-start.sh config/server.properties
Step 4: Test the Installation
Now both Zookeeper and the Kafka Broker is running, we can now verify the system by sending and receiving data.
1. Create a Topic
A "Topic" is a category or feed name to which records are published. Let's create a topic named
test.- In the new terminal type the below command to create the topic
Output:
- Now, let's become a "Producer" and write data into the
testtopic. - Now in another terminal follow the below command to run the producer:
bin/kafka-console-producer.sh --topic test --bootstrap-server localhost:9092
- You will see a > prompt. Type a few messages and press Enter after each one:
- Finally, let's verify that the messages were stored. We will start a "Consumer" to read from the test topic.
- Open a new terminal window and run the consumer script to read messages from the topic:
bin/kafka-console-consumer.sh --topic test --bootstrap-server localhost:9092 --from-beginning
Output:
You should immediately see the messages you typed in the producer terminal appear here:
Conclusion
Now we have successfully configured a persistent Kafka single-node cluster. You have also verified the pipeline by acting as both a Producer (writing data) and a Consumer (reading data).
Comments
Post a Comment