elastic stack的介紹
在elastic架構下面有:elasticsearch、kibana、logstash(俗稱ELK)和beats。
他們的介紹依次如下:
我對elastic stack的理解
圖片來自網絡
- elasticsearch是一個分佈式搜索引擎,通過指定的
_index
可以快速搜索到需要的內容。通常用於日誌搜索和電商商品搜索等大數據搜索。
它的優點:
- 支持restful,基於JSON,開箱即用
- 支持分佈式部署,處理海量數據
- kibana 是elastic stack的客戶端,主要提供基於
_index
的內容查詢,圖表、Map、性能監控等展示。 - Logstash 用於數據收集,分爲輸入端(input)和輸出端(output),在elastic stack的架構下,輸入端指的是各種數據源各種beat,輸出端是:elasticSearch。在logStash這一層將採集的數據轉換成elasticSearch支持的格式。它支持實時解析和轉換數據。
- beats 是數據源採集器,包括filebeat、Packetbeat等。將採集的數據發送給logStash或者elasticSearch
環境搭建
elastic stack下載版本 7.3.X 基於 MAC OS
其中beats有很多數據源,我按照官方教程下載了一個filebeats
進入相關目錄,依次啓動elasticsearch、kibana等。這裏不再贅述。
蒐集apache.log日誌
demo教程
https://github.com/elastic/examples/tree/master/Common%20Data%20Formats
目的
通過filebeats
數據源將收集本地的apache.log
日誌,logStash
將日誌通過固定格式寫入到elasticsearch
中,並在kibana
的discover
中展示出來。
前置條件
默認elasticSearch和kibana可以正常啓動,本地環境可以通過“localhost:9200”和“localhost:5601”驗證。
logStash配置信息
apache.conf
配置信息
- 指定input爲beats,設置端口號(5044)。
- 指定output爲elasticSearch
- filter配置,包括解析日誌中的IP,user的agent等
- (需要注意)在elasticSearch中設置template
input {
beats {
port => 5044
}
}
filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
}
date {
match => [ "timestamp", "dd/MMM/YYYY:HH:mm:ss Z" ]
locale => en
}
geoip {
source => "clientip"
}
useragent {
source => "agent"
target => "useragent"
}
}
output {
stdout {
codec => dots {}
}
elasticsearch {
index => "apache_elastic_example"
template => "./config/apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
}
apache_template.json
的配置
與github上demo相比,去掉了_default_
類型
{
"template": "apache_elastic_example",
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string",
"match": "message"
}
},
{
"string_fields": {
"mapping": {
"norms": false,
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"geoip": {
"dynamic": true,
"properties": {
"location": {
"type": "geo_point"
},
"ip": {
"type": "ip"
},
"continent_code": {
"type": "keyword"
},
"country_name": {
"type": "keyword"
}
},
"type": "object"
},
"@version": {
"type": "keyword"
}
}
}
}
- 啓動logStash
./bin/logstash -f ./config/apache.conf
elasticSearch中出現以下日誌代表成功
adding template [apache_elastic_example] for index patterns [apache_elastic_example]
filebeat配置信息
創建file.new.yml
文件
filebeat.inputs:
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /Users/awo/project/tool/logstash-7.3.2/example/apache-log.log
output.logstash:
# The Logstash hosts
hosts: ["localhost:5044"]
因爲logStash中指定輸出格式
stdout {
codec => dots {}
}
logStash中出現...
表示日誌正在同步。
3. 最終展示
在kibana中創建索引,在discover可以看到apache_elastic_example
索引
遇到一些問題
github上面的教程是基於elasticSearch6.0的有很多特性在elasticSearch7.X已經被棄用。
1. 無需下載ingest-user-agent和ingest-geoip
7.X中將ingest-user-agent和ingest-geoip加入到modules中,無需下載
2. Rejecting mapping update to [XXX] as the final mapping would have more than 1 type: [_doc, log]
7.X中棄用type類型默認是"_doc"。所以在PUT數據時不用加type。原因可以參考:https://www.cnblogs.com/miracle-luna/p/10998670.html
學習資料分享
- elastic中文社區:https://elasticsearch.cn/
- 官方文檔:https://www.elastic.co/guide/index.html
- elastic example: https://github.com/elastic/examples(因爲es版本的不同,有些例子可能過時)