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版本的不同,有些例子可能过时)