Installing Kafka on Ubuntu involves several steps, including installing Java, downloading and setting up Kafka, and configuring it. Here's a step-by-step guide to installing Kafka on Ubuntu:
Step 1: Install Java
Kafka requires Java to run. Ensure you have Java installed on your system by running the following command:
```bash
sudo apt update
sudo apt install default-jre
```
Step 2: Download Kafka
Go to the Apache Kafka website (https://kafka.apache.org/downloads) and download the latest stable release. At the time of writing, the latest version is 2.8.0. You can use wget to download Kafka directly to your server:
```bash
wget https://downloads.apache.org/kafka/2.8.0/kafka_2.13-2.8.0.tgz
```
Step 3: Extract Kafka
Extract the downloaded Kafka archive:
```bash
tar -xzf kafka_2.13-2.8.0.tgz
```
Step 4: Move Kafka Directory
Move the extracted Kafka directory to a location of your choice (e.g., /opt):
```bash
sudo mv kafka_2.13-2.8.0 /opt/kafka
```
Step 5: Configure Kafka Environment Variables (Optional)
You can set Kafka-related environment variables, such as KAFKA_HOME, by adding the following lines to your ~/.bashrc file:
```bash
export KAFKA_HOME=/opt/kafka
export PATH=$PATH:$KAFKA_HOME/bin
```
Run the following command to reload the bashrc file:
```bash
source ~/.bashrc
```
Step 6: Start ZooKeeper
Kafka uses ZooKeeper for distributed coordination. Start ZooKeeper using the following command:
```bash
cd /opt/kafka
bin/zookeeper-server-start.sh config/zookeeper.properties
```
Step 7: Start Kafka Server
Now, start the Kafka server:
```bash
bin/kafka-server-start.sh config/server.properties
```
Kafka should now be up and running on your Ubuntu system. By default, Kafka will run on port 9092 for broker communication and 2181 for ZooKeeper communication.
You can test Kafka by creating topics and producing/consuming messages. For example, to create a topic named "test-topic," use the following command:
```bash
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
```
To produce a message to the "test-topic," run:
```bash
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
```
To consume messages from the "test-topic," run:
```bash
bin/kafka-console-consumer.sh --topic test-topic --bootstrap-server localhost:9092 --from-beginning
```
This completes the installation of Apache Kafka on Ubuntu, and you can now use it to publish and consume messages from different topics.
No comments:
Post a Comment