ELK Stack Explanation & Configuration
ELK Stack Explanation & Configuration
ELK introduction:
"ELK" is the acronym for three open source projects: Elasticsearch, Logstash,
and Kibana. Elasticsearch is a search and analytics engine. Logstash is a
server-side data processing pipeline that ingests data from multiple sources
simultaneously, transforms it, and then sends it to a "stash" like Elasticsearch.
Kibana lets users visualize data with charts and graphs in Elasticsearch.
The open source, distributed, RESTful, JSON-based search engine. Easy to use, scalable and
flexible, it earned hyper-popularity among users and a company formed around it, you know, for
search.
A search engine at heart, users started using Elasticsearch for logs and wanted to easily ingest
and visualize them. Enter Logstash, the powerful ingest pipeline, and Kibana, the flexible
visualization tool.
1. Fields
2. Documents
An example of a document:
{
"_id": 3,
“_type”: [“user”],
"age": 28,
"name": ["daniel”],
"year":1989,
}
3. Types
An index can have any number of types, and you can store
documents belonging to these types in the same index.
4. Mapping
5. Index
6. Shards
Put simply, shards are a single Lucene index. They are the
building block of Elasticsearch and are what facilitate its
scalability.
Index size is a common cause of Elasticsearch crashes. Since
there is no limit to how many documents you can store on
each index, an index may take up an amount of disk space
that exceeds the limits of the hosting server. As soon as an
index approaches this limit, indexing will begin to fail.
When you create an index, you can define how many shards
you want. Each shard is an independent Lucene index that
can be hosted anywhere in your cluster:
# Example
curl -XPUT localhost:9200/example -d '{
"settings" : {
"index" : {
"number_of_shards" : 2,
"number_of_replicas" : 1
}
}
}'
7. Replicas
8. Analyzers
9. Nodes
10. Cluster
There are a number of useful cluster APIs that can query the
general status of the cluster.
For example, the cluster health API returns health status
reports of either “green” (all shards are allocated), “yellow”
(the primary shard is allocated but replicas are not), or “red”
(the shard is not allocated in the cluster).
# Output Example
{
"cluster_name" : "elasticsearch",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 5,
"active_shards" : 5,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 5,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 50.0
}
Elasticsearch Queries
Elasticsearch is built on top of Apache Lucene and exposes
Lucene’s query syntax. Getting acquainted with the syntax
and its various operators will go a long way in helping you
query Elasticsearch.
Boolean Operators
jack AND jill — Will return events that contain both jack
and jill
name:”Ned Stark”
Ranges
You can search for fields within a specific range, using square
brackets for inclusive range searches and curly braces for
exclusive range searches:
URI Search
curl “localhost:9200/index_name/_search?
q=name:travis”
PUT /index_name/_settings
{"max_ngram_diff" : "50","analysis":{"filter":{"my_custom_stop_words_filter":{"filter":
["lowercase"],"ignore_case":"true","char_filter":["my_char_filter"],"type":"stop"},"my_stopwords":
{"type":"stop","stopwords":["_english_"]},"name_ngrams":
{"min_gram":"1","side":"front","type":"edgeNGram","max_gram":"15"},"autocomplete_filter":
{"type":"nGram","min_gram":"2","max_gram":"20"}},"char_filter":{"my_char_filter":
{"type":"mapping","mappings":["- =>\\u0020"]}},"normalizer":{"lowerCase_normaizer":{"filter":
["lowercase"],"char_filter":["my_char_filter"]}},"analyzer":{"partial_name":{"filter":
["lowercase","asciifolding","name_ngrams"],"type":"custom","tokenizer":"standard"},"custom_ana
lyzer":{"filter":["lowercase"],"type":"custom","tokenizer":"standard"},"default":{"filter":
["my_custom_stop_words_filter"],"tokenizer":"whitespace"},"autocomplete":{"filter":
["lowercase","asciifolding"],"char_filter":
["html_strip"],"type":"custom","tokenizer":"whitespace"},"lowerCase":{"filter":
["lowercase"],"char_filter":
["html_strip"],"type":"custom","tokenizer":"keyword"},"lowercase_analyzer":{"filter":
["lowercase"],"char_filter":
["my_char_filter"],"type":"custom","tokenizer":"standard"},"whitespace_analyzer":{"filter":
["lowercase","my_custom_stop_words_filter"],"char_filter":
["my_char_filter","html_strip"],"tokenizer":"whitespace"},"commaSeparated":{"filter":
["lowercase"],"tokenizer":"commaSeparated"},"startsWithAnalyzer":{"filter":
["lowercase"],"type":"custom","tokenizer":"keyword"}},"tokenizer":{"commaSeparated":
{"pattern":",","type":"pattern"}}}, "max_terms_count":"1000000","number_of_replicas":"1"}
POST /index_name/_open
{"properties":{"fieldname":{"type":"text","fields":{"caseinsensitive":
{"type":"keyword","normalizer":"lowerCase_normaizer"},"keyword":
{"type":"keyword","ignore_above":256},"normalize":
{"type":"keyword","normalizer":"lowerCase_normaizer"},"whitespace":
{"type":"text","analyzer":"whitespace_analyzer"}},"analyzer":"autocomplete","fielddata":true}}}
From index settings, mappings and data moved one server to another
server
elasticdump --input=http://elastic:password@localhost:9200/index1 --
output=http://elastic:password@localhost2:9200/index1 --type=settings
elasticdump --input=http://elastic:password@localhost:9200/index1 --
output=http://elastic:password@localhost2:9200/index1 --type=mapping
elasticdump --input=http://elastic:password@localhost:9200/index1 --
output=http://elastic:password@localhost2:9200/index1 --type=data
Advantages
Advantages of ElasticSearch include the following:
Disadvantages of Elasticsearch:
Elasticsearch also has a problem of Split brain situations but in rare cases.
It is not as good at being a data store as some other options like MongoDB,
Hadoop, etc. For smaller use cases, it will perform fine. If you are streaming
TB’s of data every day, you will find that it either chokes or loses data.
Elasticsearch is way more powerful and flexible, but it’s learning curve is
much steeper.
INSTALLING ELK
The ELK Stack can be installed using a variety of methods
and on a wide array of different operating systems and
environments. ELK can be installed locally, on the cloud,
using Docker and configuration management systems like
Ansible, Puppet, and Chef. The stack can be installed using a
tarball or .zip packages or from repositories.
Elastic search
1.sudo apt update
2.sudo apt install -y openjdk-8-jdk wget apt-transport-https
3.wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
4.echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
5.sudo apt update
6.sudo apt install -y elasticsearch
7.sudo systemctl start elasticsearch
8.sudo systemctl enable elasticsearch
9.curl -X GET http://localhost:9200
Logstash installation
Note : if you want to install logstash make sure your system java version 11
1. wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz
2.tar zxvf logstash-1.4.2.tar.gz
3.Cd logstash-1.4.2
Kibana installation
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.3.1-linux-x86_64.tar.gz
shasum -a 512 kibana-7.3.1-linux-x86_64.tar.gz
tar xvzf kibana-7.3.1-linux-x86_64.tar.gz
cd kibana-7.3.1-linux-x86_64/
if you have public ip setup then changes into kibana.yml file in config folder
./bin/kibana
index Creation
curl -XPUT 'localhost:9200/sample_test'
Note: 1. Format for date should be defined like eg: ""format"":""yyyy/MM/dd||yyy-MM-dd HH:mm:ss||
yyyy-MM-dd||epoch_millis""
2. For string type fields, you dont need to do external mapping, as by default every field is string type
field."
http://localhost:9200/_cat/indices
http://localhost:9200/sample_test/details/_search
Query converter from sql query to elastic search query Use This Link
https://sqltoelasticsearch.azurewebsites.net/
http://localhost:9200/customerdata/details/_search?pretty&size=1000
$json_data = '{"Id":"2","firstname":"nani","lastname":"praveen"}';
$url = "http://localhost:9200/sample_test/details/2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$response = curl_exec($ch);
curl_close($ch);
Dump the data from one index to another index or from one server index to another server index
elasticdump --input=http://localhost:6200/customerdata
--output=http://localhost:9200/customerdata_copy --type=mapping
elasticdump --input=http://localhost:6200/customerdata
--output=http://localhost:9200/customerdata_copy --type=data
For fetchingmore than 10000 values from elasticsearch index, run the below command:
This is a setting for the index, which fetches more than 10000 records.
i) <aggregation field>: this can be any random name which you want in return output.
ii) <required field> : This is the field name for which you require distinct values.
iii) "size":0 denotes that we only want return for required field, nothing else.
iv) "size":1000 denotes that maximum 1000 distinct <required fields> will result in output. By default, it
will be 10.
i) if you get error: max number of threads [1024] for user [elastic] likely too low, increase to at least
[2048]
if elasti search running but not display in browser then run the following command
Run the following command:
iv) service iptables stop
Exit
Run as other user except root user
./bin/elasticsearch -d
Logstash requires Java 8. Before downloading and installing Logstash, install Java 8.
No configuation required for logstash.
Configuring Kibana:
1.To set the number of open file handles or file descriptors (ulimit -n) to 65536. Limits usually need to be
set as root before switching to the user that will run Elasticsearch.
Ex:
sudo su
ulimit -n 65536
exit
./bin/elasticsearch -d