How do I Install and Configure ELK (Elasticsearch, Logstash, Kibana) for Log Analysis

Install Java

Java is a prerequisite for installing and running the ELK stack. To install Java, you need to download the latest version of the Java Development Kit (JDK) from the Oracle website. Once you have downloaded the JDK, you can install it by running the following command:

sudo apt-get install openjdk-8-jdk

Once the installation is complete, you can verify the installation by running the following command:

java -version

This will output the version of Java that is installed on your system. You can now proceed to install Elasticsearch.

Install Elasticsearch

Elasticsearch is an open source search engine that is used for log analysis. To install Elasticsearch, you need to have Java installed on your system. You can download the latest version of Java from Oracle's website. Once you have Java installed, you can download the latest version of Elasticsearch from Elastic's website. After downloading the file, you can extract it to a directory of your choice. To configure Elasticsearch, you need to edit the elasticsearch.yml file located in the config directory. You can set the cluster.name and node.name parameters in the file. Once you have configured Elasticsearch, you can start it by running the following command:

$ ./bin/elasticsearch

You can verify that Elasticsearch is running by visiting http://localhost:9200 in your browser. You should see a JSON response with the version of Elasticsearch that you have installed.

Configure Elasticsearch

Elasticsearch is a distributed, open source search and analytics engine for all types of data, including textual, numerical, geospatial, structured, and unstructured. To configure Elasticsearch, you need to edit the configuration file located at /etc/elasticsearch/elasticsearch.yml. This file contains settings for cluster and node configuration, such as cluster name, node name, and network settings. You can also configure the number of shards and replicas, as well as the memory and disk settings. To make changes to the configuration file, use the following command:

$ sudo nano /etc/elasticsearch/elasticsearch.yml

Once you have made the necessary changes, save the file and restart Elasticsearch. To restart Elasticsearch, use the following command:

$ sudo service elasticsearch restart

For more information on configuring Elasticsearch, please refer to the official Elasticsearch documentation.

Start Elasticsearch

In order to start Elasticsearch, you need to open a terminal window and navigate to the directory where Elasticsearch is installed. Then, type the following command: bin/elasticsearch. This will start the Elasticsearch server. You can also use the -d flag to run the server in the background. Once the server is running, you can access the Elasticsearch API by sending HTTP requests to the server's port. To check if the server is running, you can use the curl command to send a request to the server's port. For example, you can use the following command: curl http://localhost:9200. This will return a JSON response with the server's status.

To ensure that Elasticsearch is running properly, you can also use the elasticsearch-plugin command to install and configure plugins. This will allow you to monitor the server's performance and ensure that it is running optimally.

Install Logstash

Logstash is an open source data collection engine with real-time pipelining capabilities. It is used to collect, parse, and store logs for future use. In this tutorial, we will learn how to install and configure Logstash for log analysis.

To install Logstash, we need to download the Logstash package from the Elastic website. Once the package is downloaded, we can install it using the following command:

$ sudo dpkg -i logstash_2.4.0-1_all.deb

Once the installation is complete, we can configure Logstash by creating a configuration file. This file will contain the input, filter, and output sections. The input section will define the source of the log data, the filter section will define the rules for processing the log data, and the output section will define the destination of the processed log data.

We can create the configuration file using the following command:

$ sudo nano /etc/logstash/conf.d/logstash.conf

Once the configuration file is created, we can start Logstash using the following command:

$ sudo service logstash start

Logstash is now installed and configured for log analysis. We can now start collecting and analyzing log data using Logstash.

Configure Logstash

Logstash is a powerful tool for collecting, parsing, and storing logs for future use. To configure Logstash, you need to create a configuration file that tells Logstash how to process the data. The configuration file is written in the JSON format and located in the /etc/logstash/conf.d directory. To create the configuration file, open a text editor and add the following code:

input {
  file {
    path => "/var/log/messages"
    type => "syslog"
  }
}

filter {
  grok {
    match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:host} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:syslog_message}" }
  }
  date {
    match => [ "timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
  }
  stdout { codec => rubydebug }
}

The configuration file consists of three sections: input, filter, and output. The input section defines the source of the data. In this example, the source is a file located in the /var/log/messages directory. The filter section is used to parse and transform the data. In this example, the grok filter is used to parse the syslog messages. The date filter is used to parse the timestamp field. The output section defines where the data should be sent. In this example, the data is sent to Elasticsearch and the console.

Once the configuration file is created, you can start Logstash with the following command:

sudo systemctl start logstash

You can check the status of Logstash with the following command:

sudo systemctl status logstash

If Logstash is running, you should see an output similar to the following:

● logstash.service - logstash
   Loaded: loaded (/etc/systemd/system/logstash.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-09-14 11:45:02 UTC; 1min 10s ago
 Main PID: 888 (java)
    Tasks: 11 (limit: 4704)
   CGroup: /system.slice/logstash.service
           └─888 /usr/bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.compile.invokedynamic=false -XX:+HeapDumpOnOutOfMemoryError -Xloggc:/var/log/logstash/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Djruby.logger.class=com.amazon.logstash.log.LogstashLogger -Djruby.logger.name=logstash -Djruby.logger.level=INFO -Djruby.compat.version=RUBY1_9 -Djruby.jit.threshold=0 -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -Xss2048k -XX:+UseStringDeduplication -Des.path.home=/usr/share/logstash -Des.path.conf=/etc/logstash -Des.distribution.flavor=default -Des.distribution.type=tar -cp /usr/share/logstash/logstash-core/lib/jars/* org.logstash.Logstash --path.settings /etc/logstash

Logstash is now running and ready to process your data. You can check the logs to make sure that Logstash is processing the data correctly. To do this, open the /var/log/logstash/logstash-plain.log file and check the output.

Useful Links