Advertisement

How to install Kafka and Zookeeper on Ubuntu

阅读量:

To install Apache Kafka with Zookeeper on Ubuntu, follow these steps:


1. Install Java

Kafka requires Java. Install OpenJDK 11 (or higher):

复制代码
    sudo apt update
    sudo apt install openjdk-11-jdk

Verify installation:

复制代码
    java -version

2. Download Apache Kafka

Access the most recent Kafka binary from Apache Kafka, replacing X.X.X with the current release.

复制代码
    wget https://downloads.apache.org/kafka/X.X.X/kafka_2.13-X.X.X.tgz

Extract the file:

复制代码
    tar -xzf kafka_2.13-X.X.X.tgz
    cd kafka_2.13-X.X.X

3. Configure Zookeeper

  • Create a data directory for Zookeeper:
复制代码
    mkdir -p /tmp/zookeeper
  • Edit the Zookeeper configuration (optional):
复制代码
    vim config/zookeeper.properties

Ensure dataDir=/tmp/zookeeper is set (default).


4. Start Zookeeper

Run Zookeeper as a background service :

复制代码
    bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

Verify it’s running:

复制代码
    netstat -tnlp | grep 2181

5. Configure Kafka

  • Edit config/server.properties:
复制代码
    vim config/server.properties
  • Configure the log directory path (defaults to log.dirs=/tmp/kafka-logs).
    • (Optional) if accessed remotely, specify the
复制代码
    listeners=PLAINTEXT://0.0.0.0:9092

    advertised.listeners=PLAINTEXT://<YOUR_IP>:9092

6. Start Kafka

Run Kafka as a background service :

复制代码
    bin/kafka-server-start.sh -daemon config/server.properties

Verify Kafka is running:

复制代码
    netstat -tnlp | grep 9092

7. Test the Setup

  • Create a topic :
复制代码
    bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
  • Produce messages :
复制代码
    bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
  • Consume messages (open a new terminal):
复制代码
    bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092

8. (Optional) Create Systemd Services

For auto-start on reboot:

Zookeeper Service :

复制代码
    sudo vim /etc/systemd/system/zookeeper.service
复制代码
    [Unit]
    Description=Zookeeper Service
    After=network.target
    
    [Service]
    Type=simple
    User=ubuntu
    ExecStart=/path/to/kafka/bin/zookeeper-server-start.sh /path/to/kafka/config/zookeeper.properties
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target

Kafka Service :

复制代码
    sudo vim /etc/systemd/system/kafka.service
复制代码
    [Unit]
    Description=Kafka Service
    After=zookeeper.service
    
    [Service]
    Type=simple
    User=ubuntu
    ExecStart=/path/to/kafka/bin/kafka-server-start.sh /path/to/kafka/config/server.properties
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target

Enable the services :

复制代码
    sudo systemctl daemon-reload
    sudo systemctl start zookeeper
    sudo systemctl start kafka
    sudo systemctl enable zookeeper kafka

9. Stop Services

To stop Kafka and Zookeeper:

复制代码
    bin/kafka-server-stop.sh
    bin/zookeeper-server-stop.sh

Notes

This configuration is specifically designed for development activities. When in a production environment, it's advisable to employ a multi-node cluster setup.
Modify firewall configurations when accessed remotely:

复制代码
    sudo ufw allow 2181/tcp  # Zookeeper

    sudo ufw allow 9092/tcp  # Kafka

You now have a single-node Kafka cluster running on Ubuntu!

全部评论 (0)

还没有任何评论哟~