Kafka Setup Guide
This guide provides instructions for setting up Apache Kafka (version 3.7.0) on your system. The setup includes necessary directories, binaries, configurations, and libraries for running Kafka and its components effectively.Running Apache Kafka and Zookeeper on Your Server
Table of Contents
- Prerequisites
- Installing Java
- Installing Zookeeper
- Installing Kafka
- Configuring Zookeeper
- Configuring Kafka
- Setting Up Systemd Services
- Starting Services
- Verifying the Installation
- Troubleshooting
Prerequisites
- A server running a Linux-based operating system (e.g., Ubuntu, CentOS).
- Access to the terminal with
sudoprivileges. - Basic understanding of terminal commands.
Installing Java
Apache Kafka requires Java to run. Make sure you have Java installed:-
Update your package index:
-
Install OpenJDK:
-
Verify the installation:
Installing Zookeeper
Zookeeper is required for managing Kafka brokers. Here’s how to install it:-
Download Zookeeper:
-
Extract the downloaded file:
-
Move to
/opt: -
Create a configuration file:
Add the following configuration:
-
Create the data directory:
Installing Kafka
-
Download Kafka:
-
Extract the downloaded file:
-
Move to
/opt:
Configuring Zookeeper
Make sure Zookeeper is configured correctly. The configuration file was created earlier. You may edit it if needed.Configuring Kafka
-
Create a configuration file for Kafka:
Add or modify the following configurations:
-
Create the logs directory:
Setting Up Systemd Services
Create a Zookeeper Service
-
Create the service file:
-
Add the following content:
Create a Kafka Service
-
Create the service file:
-
Add the following content:
Starting Services
-
Reload systemd to recognize the new services:
-
Start Zookeeper:
-
Start Kafka:
-
Enable both services to start on boot:
Verifying the Installation
-
Check the status of Zookeeper:
-
Check the status of Kafka:
-
Create a test topic:
-
List topics to confirm:
Troubleshooting
- Check logs if Kafka or Zookeeper fail to start:
- Zookeeper logs:
/opt/zookeeper/logs/zookeeper.out - Kafka logs:
/opt/kafka/logs/server.log
- Zookeeper logs:
- Ensure the correct version of Java is installed.
- Verify that no other services are using the same ports (2181 for Zookeeper, 9092 for Kafka).
Conclusion
You have successfully installed and configured Apache Kafka and Zookeeper on your server. Both services are set up to run automatically at startup, ensuring reliable operation. You can now start producing and consuming messages using Kafka.Directory Structure
The following is the directory structure for the Kafka installation:Key Directories and Files
- bin/: Scripts to start, stop, and manage Kafka and Zookeeper instances.
- config/: Contains properties files for configuring Kafka and its components.
- libs/: Contains all the necessary jar files required for Kafka to function.
Prerequisites
Before installing Kafka, ensure that you have the following prerequisites:- Java: Kafka requires Java 8 or later. Ensure that it is installed and the
JAVA_HOMEenvironment variable is set correctly. - Zookeeper: Kafka uses Zookeeper for cluster management. It can be run separately or within Kafka.
Installation Steps
-
Download Kafka:
- Download the Kafka binary from the Apache Kafka website.
- Extract the downloaded tar.gz file to your desired installation directory.
-
Verify Installation:
- Navigate to the Kafka directory:
- Note : Tested Kafka Version is
kafka_2.12-3.7.0( https://archive.apache.org/dist/kafka/3.7.0/kafka_2.12-3.7.0.tgz ) - Check if the
bindirectory contains all necessary scripts.
- Navigate to the Kafka directory:
Configuration Files
The configuration files are located in theconfig/ directory. Key files include:
- server.properties: Configuration for Kafka brokers.
- zookeeper.properties: Configuration for Zookeeper.
- connect-distributed.properties: Configuration for running Kafka Connect in distributed mode.
Example of server.properties
Example of zookeeper.properties
Starting Kafka
-
Start Zookeeper:
- Use the following command to start Zookeeper:
- Use the following command to start Zookeeper:
-
Start Kafka Server:
- Once Zookeeper is running, start the Kafka server:
- Once Zookeeper is running, start the Kafka server:
Using Kafka Command-Line Tools
Kafka provides various command-line tools located in thebin/ directory. Here are some commonly used commands:
-
Create a Topic:
-
List Topics:
-
Produce Messages:
-
Consume Messages:
Python Integration
For Python integration, thekafka-consumer.py and kafka-logger.py scripts can be used. These scripts demonstrate how to interact with Kafka using Python.
Example of Using kafka-consumer.py
Make sure you have the kafka-python library installed:
Conclusion
This guide provides a basic setup for Apache Kafka, covering installation, configuration, and usage of command-line tools. For advanced configurations and deployments, refer to the official Kafka documentation.For troubleshooting, please check the logs located in the/tmp/kafka-logsdirectory or adjust the logging settings in thelog4j.propertiesfile found in theconfig/directory.
