ELK Stack · Part 3 of 5

Logstash Pipeline

Configure Logstash to consume logs from Kafka, transform the data, and securely output to Elasticsearch with SSL/TLS.

Input: Kafka Filter: Transform Output: Elasticsearch SSL/TLS
01 Configuration File Location

The pipeline config has three sections: input, filter, and output.

bash
cat /etc/logstash/conf.d/no-filter.conf
02 Input Plugin — Kafka Consumer

Configures Logstash to act as a consumer for the Kafka topic.

logstash config
input {
  kafka {
    bootstrap_servers => "192.168.20.208:9092"
    topics => ["application_logs"]
    codec => json { ecs_compatibility => disabled }
    group_id => "logstash-consumer-group"
    auto_offset_reset => "earliest"
  }
}
Configuration Parameters
SettingValueExplanation
bootstrap_servers192.168.20.208:9092Address and port of the Kafka broker
topicsapplication_logsThe Kafka topic Logstash will read from
codecjson { ... }Decodes incoming Kafka messages as JSON, creating structured fields
group_idlogstash-consumer-groupConsumer group ID — allows multiple Logstash instances to scale across partitions
auto_offset_resetearliestStarts reading from oldest available message on first startup
03 Filter Plugin — Data Transformation

Standardizes data before it reaches Elasticsearch.

logstash config
filter {
  date {
    match => ["timestamp", "yyyy-MM-dd HH:mm:ss.SSS"]
    timezone => "Asia/Riyadh"
    target => "@timestamp"
  }
  uuid {
    target => "doc_id"
  }
  mutate {
    remove_field => ["timestamp", "@version", "event"]
  }
}
Filter Configuration Details
PluginConfigExplanation
datematch, timezone, targetParses the app timestamp, adjusts to Asia/Riyadh timezone, sets it as @timestamp
uuidtarget => "doc_id"Generates a unique ID for every log document — ensures document uniqueness in Elasticsearch
mutateremove_fieldRemoves temporary fields: original timestamp, Logstash's @version, and event
04 Output Plugin — Elasticsearch

Sends cleaned, structured logs to your secure Elasticsearch cluster.

logstash config
output {
  elasticsearch {
    hosts => ["https://localhost:9200"]
    user => "elastic"
    password => "7=04elu9ZfBiltoN=KFS"
    ssl_enabled => true
    ssl_certificate_authorities => ["/etc/logstash/elasticsearch-ca.crt"]
    index => "applogs-%{+YYYY.MM.dd}"
    document_id => "%{doc_id}"
  }
  stdout { codec => rubydebug }
}
Output Configuration Details
SettingValueExplanation
hostshttps://localhost:9200Elasticsearch endpoint — https indicates secure connection
user / passwordelastic / passwordCredentials for authentication with the Elasticsearch cluster
ssl_enabledtrueRequired when using https hosts
ssl_certificate_authoritiescert pathCA certificate location to trust the Elasticsearch SSL certificate
indexapplogs-%{+YYYY.MM.dd}Daily index naming — standard practice for time-series log data
document_id%{doc_id}UUID from filter stage used as unique primary key in Elasticsearch
stdoutrubydebugDebug output — prints every processed log to the Logstash console
05 Complete Configuration File
logstash config
input {
  kafka {
    bootstrap_servers => "192.168.20.208:9092"
    topics => ["application_logs"]
    codec => json { ecs_compatibility => disabled }
    group_id => "logstash-consumer-group"
    auto_offset_reset => "earliest"
  }
}

filter {
  date {
    match => ["timestamp", "yyyy-MM-dd HH:mm:ss.SSS"]
    timezone => "Asia/Riyadh"
    target => "@timestamp"
  }
  uuid {
    target => "doc_id"
  }
  mutate {
    remove_field => ["timestamp", "@version", "event"]
  }
}

output {
  elasticsearch {
    hosts => ["https://localhost:9200"]
    user => "elastic"
    password => "7=04elu9ZfBiltoN=KFS"
    ssl_enabled => true
    ssl_certificate_authorities => ["/etc/logstash/elasticsearch-ca.crt"]
    index => "applogs-%{+YYYY.MM.dd}"
    document_id => "%{doc_id}"
  }
  stdout { codec => rubydebug }
}
06 JVM Heap Size Configuration

Insufficient memory is the most common reason for Logstash instability. Set heap size in /etc/logstash/jvm.options.

Recommendation
  • -Xms and -Xmx must be set to the same value
  • For an 8GB RAM server: use -Xms4g and -Xmx4g
bash
sudo sed -i 's/^-Xms.*/-Xms4g/' /etc/logstash/jvm.options
sudo sed -i 's/^-Xmx.*/-Xmx4g/' /etc/logstash/jvm.options
07 SSL/TLS Certificate Setup
Method 1 — Extract CA Certificate
bash
cd /etc/elasticsearch/certs/
openssl s_client -showcerts -connect ELK-SERVER:9200 </dev/null 2>/dev/null \
| openssl x509 > /etc/logstash/elasticsearch-ca.crt
What This Does:
  • openssl s_client -connect ELK-SERVER:9200 — connects to Elasticsearch over TLS and prints the certificate chain
  • openssl x509 — extracts the X.509 certificate only
  • Saves the CA certificate to Logstash's directory so it can trust Elasticsearch
Method 2 — Generate Fingerprint
bash
cd /etc/elasticsearch/certs/
openssl x509 -in http_ca.crt -noout -fingerprint -sha256 \
| cut -d '=' -f2 | tr -d ':' | tr 'A-F' 'a-f' >> /etc/filebeat/fingerprint.txt
cat /etc/filebeat/fingerprint.txt
output — fingerprint
7a212d974cd16b78bdd5e3e3541d5579558914ae6ef52bc6c0a9d3811a46dd6b
Use Fingerprint in Configuration
config
output.elasticsearch:
  hosts: ["ELK-SERVER:9200"]
  preset: balanced
  protocol: "https"
  ssl:
    enabled: true
    ca_trusted_fingerprint: "7a212d974cd16b78bdd5e3e3541d5579558914ae6ef52bc6c0a9d3811a46dd6b"
  username: "elastic"
  password: "abcd@1234"
08 Test Configuration
bash
sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash -t
Purpose
  • Runs Logstash in validate mode (-t) — does not start the service
  • Checks pipelines, config files, and certificate settings
  • Ensures Logstash will not fail on restart
  Expected output: Configuration OK
09 Set Permissions
bash
chmod 644 /etc/logstash/conf.d/filebeat-client-ubuntu.conf
chown -R logstash:logstash /usr/share/logstash/data
Note: Use 644 not 777 — it's more secure. The chown prevents permission errors when pipelines write to disk.
10 Service Management
bash
systemctl daemon-reload
systemctl enable --now logstash
systemctl start logstash
systemctl restart logstash
systemctl status logstash

# Verify Logstash is listening on port 5044
ss -tulnp | grep 5044
Summary — All Commands
bash — full sequence
cd /etc/elasticsearch/certs/
openssl s_client -showcerts -connect ELK-SERVER:9200 </dev/null 2>/dev/null \
| openssl x509 > /etc/logstash/elasticsearch-ca.crt

sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash -t

chmod 644 /etc/logstash/conf.d/filebeat-client-ubuntu.conf
chown -R logstash:logstash /usr/share/logstash/data

sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash -t
sudo -u logstash /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/filebeat-client-ubuntu.conf

systemctl daemon-reload
systemctl enable --now logstash
systemctl start logstash
systemctl restart logstash
systemctl status logstash

ss -tulnp | grep 5044
Related Documentation
Logstash Pipeline Configuration  ·  Part of the ELK Stack deployment series