在elastic架构下面有:elasticsearch、kibana、logstash(俗称ELK)和beats。
他们的介绍依次如下:
图片来自网络
_index
可以快速搜索到需要的内容。通常用于日志搜索和电商商品搜索等大数据搜索。_index
的内容查询,图表、Map、性能监控等展示。elastic stack下载版本 7.3.X 基于 MAC OS
https://www.elastic.co/cn/downloads/
其中beats有很多数据源,我按照官方教程下载了一个filebeats
https://www.elastic.co/cn/downloads/beats/filebeat
进入相关目录,依次启动elasticsearch、kibana等。这里不再赘述。
https://github.com/elastic/examples/tree/master/Common%20Data%20Formats
通过filebeats
数据源将收集本地的apache.log
日志,logStash
将日志通过固定格式写入到elasticsearch
中,并在kibana
的discover
中展示出来。
默认elasticSearch和kibana可以正常启动,本地环境可以通过“localhost:9200”和“localhost:5601”验证。
apache.conf
配置信息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
的配置_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"
}
}
}
}
./bin/logstash -f ./config/apache.conf
elasticSearch中出现以下日志代表成功
adding template [apache_elastic_example] for index patterns [apache_elastic_example]
创建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
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