Monitor Kafka logs using Elastic Stack

In this article we will learn How to Monitor Kafka logs using Elastic Stack. Kafka and the Elastic Stack (Elasticsearch, Logstash, Kibana, and Filebeat) form a powerful combination for real-time data processing and log analysis. Kafka acts as a message broker, enabling seamless data flow between different components, while the Elastic Stack collects, processes, stores, and visualizes logs. This guide walks you through installing and configuring these technologies on an Ubuntu system, ensuring smooth data ingestion and visualization.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • At least 2 CPU cores and 4 GB of RAM for smooth performance.
  • Java and Apache installed.

Step #1:Setting Up Ubuntu EC2 Instance

Update the Package List to ensure you have the latest versions.

sudo apt update
Monitor Kafka logs using Elastic Stack 1

Elasticsearch requires Java, so we need to install OpenJDK 17.

sudo apt install -y openjdk-17-jdk
Monitor Kafka logs using Elastic Stack 2

Install the Apache web server.

sudo apt install -y apache2
Monitor Kafka logs using Elastic Stack 3

Step #2:Install and Configure Elasticsearch

Import the Elasticsearch GPG key.

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
Monitor Kafka logs using Elastic Stack 4

Add the Elasticsearch repository.

echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
Monitor Kafka logs using Elastic Stack 5

Now lets update the package list again. The repository is added to the system’s package sources.

sudo apt update
Monitor Kafka logs using Elastic Stack 6

Install Elasticsearch.

sudo apt install -y elasticsearch
Monitor Kafka logs using Elastic Stack 7

Modify Elasticsearch configuration for remote access.

sudo nano /etc/elasticsearch/elasticsearch.yml
Monitor Kafka logs using Elastic Stack 8

Find the network.host setting, uncomment it, and set it to 0.0.0.0 to bind to all available IP addresses and uncomment the discovery section to specify the initial nodes for cluster formation discovery.seed_hosts: []

How to Install Elastic Stack on Ubuntu 24.04 LTS 15

For a basic setup (not recommended for production), disable security features.

xpack.security.enabled: false
How to Install Elastic Stack on Ubuntu 24.04 LTS 16

Save and exit the editor.

Enable and start Elasticsearch.

sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
Monitor Kafka logs using Elastic Stack 9

Check the status of the elasticsearch to ensure it is running.

sudo systemctl status elasticsearch
Monitor Kafka logs using Elastic Stack 10

Send a GET request to check if Elasticsearch is running and responding. If successful, you should see a JSON response with cluster information.

curl -X GET "localhost:9200"
Monitor Kafka logs using Elastic Stack 11

You can access it using browser with your Public IP address:9200 port which is a default port for Elasticsearch.

Monitor Kafka logs using Elastic Stack 12

Step #3:Install and Configure Logstash

Logstash processes logs before sending them to Elasticsearch. Install it using following command.

sudo apt install -y logstash
Monitor Kafka logs using Elastic Stack 13

Create a configuration file.

sudo nano /etc/logstash/conf.d/apache.conf
Monitor Kafka logs using Elastic Stack 14

Add the following configuration to collect logs from Kafka, parse them, and send them to Elasticsearch.

input {
  kafka {
    bootstrap_servers => "localhost:9092"
    topics => "apache"
  }
}
filter {
  grok {
    match => { "message" => "%{COMBINEDAPACHELOG}" }
  }
  date {
    match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
  }
  geoip {
    source => "clientip"
    target => "geoip"
  }
}
output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "logstash-apache-%{+YYYY.MM.dd}"
  }
}
Monitor Kafka logs using Elastic Stack 15

Enable and start Logstash

sudo systemctl enable logstash
sudo systemctl start logstash
Monitor Kafka logs using Elastic Stack 16

Checks the status of Logstash.

sudo systemctl status logstash
Monitor Kafka logs using Elastic Stack 17

Step #4:Install and Configure Kibana

Kibana provides visualization for Elasticsearch data. Install Kibana on the system.

sudo apt install -y kibana
Monitor Kafka logs using Elastic Stack 18

Open the Kibana configuration file for editing.

sudo nano /etc/kibana/kibana.yml
Monitor Kafka logs using Elastic Stack 19

Uncomment and adjust the following lines to bind Kibana to all IP addresses and connect it to Elasticsearch.

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
How to Install Elastic Stack on Ubuntu 24.04 LTS 27

Enable and start Kibana.

sudo systemctl enable kibana
sudo systemctl start kibana
Monitor Kafka logs using Elastic Stack 20

Checks the status of Kibana.

sudo systemctl status kibana
Monitor Kafka logs using Elastic Stack 21

Access the Kibana interface by navigating to http://<your-server-ip>:5601 in your web browser. Click on Explore on my own.

Monitor Kafka logs using Elastic Stack 22

This will open the Kibana dashboard where you can start exploring your data.

Monitor Kafka logs using Elastic Stack 23

Step #5:Install and Configure Filebeat

Filebeat collects and forwards log data to Elasticsearch or Logstash. Install Filebeat on the system.

sudo apt install -y filebeat
Monitor Kafka logs using Elastic Stack 24

Enable the Apache module in Filebeat.

sudo filebeat modules enable apache
Monitor Kafka logs using Elastic Stack 25

Configure the Apache module.

sudo nano /etc/filebeat/modules.d/apache.yml
Monitor Kafka logs using Elastic Stack 26

Ensure the following configuration is enabled to send Apache logs.

- module: apache
  access:
    enabled: true
    var.paths: ["/var/log/apache2/access.log*"]
  error:
    enabled: true
    var.paths: ["/var/log/apache2/error.log*"]
Monitor Kafka logs using Elastic Stack 27

Save and exit the file.

Edit the Filebeat configuration file to ship logs to Kafka.

sudo nano /etc/filebeat/filebeat.yml
Monitor Kafka logs using Elastic Stack 28

Change the type to log and change enable to true from false also comment out the id: my-filestream-id and give the path to our custome log file.

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /var/log/apache2/access.log
Monitor Kafka logs using Elastic Stack 29

Comment out the Elasticsearch output section. and write the kafka output section as shown below.

output.kafka:
  codec.format:
    string: '%{[@timestamp]} %{[message]}'
  hosts: ["localhost:9092"]
  topic: apache
  partition.round_robin:
    reachable_only: false
  required_acks: 1
  compression: gzip
  max_message_bytes: 1000000
Monitor Kafka logs using Elastic Stack 30

Test the configuration.

sudo filebeat test config
Monitor Kafka logs using Elastic Stack 31

Start and enable the Filebeat service.

sudo systemctl enable filebeat
sudo systemctl start filebeat
Monitor Kafka logs using Elastic Stack 32

Checks the status of filebeat.

sudo systemctl status filebeat
Monitor Kafka logs using Elastic Stack 33

Step #6:Install and Configure Kafka

Kafka is responsible for streaming log data from Filebeat to Logstash. Install Zookeeper first.

sudo apt install -y zookeeperd
Monitor Kafka logs using Elastic Stack 34

Download Kafka from Apache’s official servers.

wget https://downloads.apache.org/kafka/3.7.2/kafka_2.13-3.7.2.tgz
Monitor Kafka logs using Elastic Stack 35

Extract the downloaded Kafka archive.

tar -xvzf kafka_2.13-3.7.2.tgz
Monitor Kafka logs using Elastic Stack 36

Copy the extracted Kafka files to a standard installation location.

sudo cp -r kafka_2.13-3.7.2 /opt/kafka
Monitor Kafka logs using Elastic Stack 37

Start the Kafka server.

sudo /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
Monitor Kafka logs using Elastic Stack 38

Verify the topic.

sudo /opt/kafka/bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Monitor Kafka logs using Elastic Stack 39

First open your browser and navigate to http://<your-server-ip>. You should see the default Apache welcome page.

Monitor Kafka logs using Elastic Stack 40

Lets consume messages from Kafka.

/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic apache --from-beginning
Monitor Kafka logs using Elastic Stack 41

Ensure Elasticsearch is receiving data from Filebeat by checking the indices.

curl -XGET "localhost:9200/_cat/indices?v"

You should see output indicating the presence of indices created by logstash.

Monitor Kafka logs using Elastic Stack 42

Step #7:Visualizing Data in Kibana

Go to Menu bar from top-left corner and select Stack Management under the management section.

Monitor Kafka logs using Elastic Stack 43

Under “Kibana” section, click on “Data views”.

Monitor Kafka logs using Elastic Stack 44

Click on “Create data view”.

Monitor Kafka logs using Elastic Stack 45

Enter logstash-* (the index name used in Logstash output) in the Index pattern name field and click on Save data view to Kibana.

Monitor Kafka logs using Elastic Stack 46

Scroll down and click on the Logs option in Obeservability in the left-hand navigation menu. If the menu is collapsed, click the Expand icon to reveal the options.

Monitor Kafka logs using Elastic Stack 47

Go to All logs as shown below.

Monitor Kafka logs using Elastic Stack 48

Next go to the Data Views.

Monitor Kafka logs using Elastic Stack 49

Select logstash-* as a data view.

Monitor Kafka logs using Elastic Stack 50

Kibana displays logs data from the last 15 minutes, visualized as a histogram along with individual log messages below. (You may need to adjust the time range.)

Monitor Kafka logs using Elastic Stack 51
Monitor Kafka logs using Elastic Stack 52

To produce a custom test messages run the following command.

/opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic apache

Type a message like shown below.

Welcome to devopshint
We are generating a Test message to Kafka
Monitor Kafka logs using Elastic Stack 53

Now go back to kibana and refresh the logs, you will see the test message logs their.

Monitor Kafka logs using Elastic Stack 54
Monitor Kafka logs using Elastic Stack 55

Conclusion:

By following these steps, you can successfully Monitor Kafka logs with the Elastic Stack on an Ubuntu system. This setup enables real-time log processing and visualization using Elasticsearch, Logstash, Kibana, and Filebeat. Kafka acts as a central message broker, ensuring efficient log streaming. With this infrastructure in place, you can monitor and analyze logs in real time, making it easier to detect and resolve issues.

Related Articles:

How to Install Elastic Stack on Ubuntu 24.04 LTS

Send Java Gradle App Logs to Elastic Stack

Send Java Maven App Logs to Elastic Stack

Reference:

Elastic Stack official page

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap