How To Install Apache Kafka on Ubuntu 20.04

How To Install Apache Kafka on Ubuntu 20.04

 

Step 1 — Creating a User for Kafka

 We will create a dedicated kafka user in this step.

Logged in as your non-root sudo user, create a user called kafka:

 

sudo adduser kafka

 

Follow the prompts to set a password and create the kafka user.

Next, add the kafka user to the sudo group with the adduser command. You need these privileges to install Kafka’s dependencies:

 

sudo adduser kafka sudo

 

Your kafka user is now ready. Log into the account using su:

 

su -l kafka

 

Step 2 — Downloading and Extracting the Kafka Binaries

 

Let’s download and extract the Kafka binaries into dedicated folders in our kafka user’s home directory.

To start, create a directory in /home/kafka called Downloads to store your downloads:

 

mkdir ~/Downloads

 

Use curl to download the Kafka binaries:

 

curl “https://downloads.apache.org/kafka/2.6.3/kafka_2.13-2.6.3.tgz” -o ~/Downloads/kafka.tgz

 

Create a directory called kafka and change to this directory. This will be the base directory of the Kafka installation:

 

mkdir ~/kafka && cd ~/kafka

 

Extract the archive you downloaded using the tar command:

 

tar -xvzf ~/Downloads/kafka.tgz –strip 1

 

Step 3 — Configuring the Kafka Server

 

Kafka’s configuration options are specified in server.properties. Open this file with nano or your favorite editor:

 

nano ~/kafka/config/server.properties

 

First, add a setting that will allow us to delete Kafka topics. Add the following to the bottom of the file:

~/kafka/config/server.properties

delete.topic.enable = true

Second, change the directory where the Kafka logs are stored by modifying the logs.dir property:

~/kafka/config/server.properties

log.dirs=/home/kafka/logs

 

Step 4 — Creating Systemd Unit Files and Starting the Kafka Server

sudo nano /etc/systemd/system/zookeeper.service

 

Enter the following unit definition into the file:

/etc/systemd/system/zookeeper.service

[Unit]

Requires=network.target remote-fs.target

After=network.target remote-fs.target

[Service]

Type=simple

User=kafka

ExecStart=/home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper.properties

ExecStop=/home/kafka/kafka/bin/zookeeper-server-stop.sh

Restart=on-abnormal

[Install]

WantedBy=multi-user.target

Next, create the systemd service file for kafka:

 

sudo nano /etc/systemd/system/kafka.service

 

Enter the following unit definition into the file:

/etc/systemd/system/kafka.service

[Unit]

Requires=zookeeper.service

After=zookeeper.service

[Service]

Type=simple

User=kafka

ExecStart=/bin/sh -c ‘/home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties > /home/kafka/kafka/kafka.log 2>&1’

ExecStop=/home/kafka/kafka/bin/kafka-server-stop.sh

Restart=on-abnormal

[Install]

WantedBy=multi-user.target

Now that you have defined the units, start Kafka with the following command:

sudo systemctl start kafka

 

sudo systemctl status kafka

 

You will receive output like this:

Output

  • kafka.service

     Loaded: loaded (/etc/systemd/system/kafka.service; disabled; vendor preset: enabled)

     Active: active (running) since Wed 2021-02-10 00:09:38 UTC; 1min 58s ago

   Main PID: 55828 (sh)

      Tasks: 67 (limit: 4683)

     Memory: 315.8M

     CGroup: /system.slice/kafka.service

             ├─55828 /bin/sh -c /home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties > /home/kafka/kafka/kafka.log 2>&1

             └─55829 java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlineLevel=15 -Djava.awt.headless=true -Xlog:gc*:file=>

 

Feb 10 00:09:38 cart-67461-1 systemd[1]: Started kafka.service.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *