- ELK(ElasticSearch,Logstash,Kibana)用于日志系统收集和搜索功能,其中Logstash负责收集、格式化、处理、过滤等操作,ES负责存储收集数据。Kibana是用node编写的可视化界面搜索工具。
-
ELK工作流程图大致如下:
-
搭建过程
下载好的ELK6安装包放入同一目录下,这里是/opt/soft
启动ES(见ElasticSearch初探之配置和启动(一))
启动Logstash。
- 先配置config下JVM参数信息。由于这里暂时没有日志收集的服务器,Logstash可以配置本地文件,Redis服务器,TCP等方式收集
本例配置成本地文件收集,新建logstash.conf文件在config目录下,配置内容如下:
input {
file {
path => ["/path/es/logs/test.log"] //读取目录
type => "test"
start_position => "beginning" //开始位置读取,end从结尾读取
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"] //ES服务器地址
index => "logstash-%{type}-%{+YYYY.MM.dd}" //存储在ES内的索引名称
}
stdout{
codec => rubydebug //加载成功之后Logstash日志输出类型,也可以json,plain等类型
}
}
其中文件内容如下:
[root@Eden666 config]# cat /path/es/logs/test.log
beginning
test
eden666
zhangsan
lisi
wangwu
启动Logstash成功后,返回ruby语言格式如下内容表示成功将本地文件的日志信息读取到Logstash中,并存储在了ES中
[2018-11-24T13:40:35,694][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
{
"message" => "beginning",
"@timestamp" => 2018-11-24T05:40:36.603Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
{
"message" => "test",
"@timestamp" => 2018-11-24T05:40:36.683Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
{
"message" => "eden666",
"@timestamp" => 2018-11-24T05:40:36.684Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
{
"message" => "zhangsan",
"@timestamp" => 2018-11-24T05:40:36.685Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
{
"message" => "lisi",
"@timestamp" => 2018-11-24T05:40:36.685Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
{
"message" => "wangwu",
"@timestamp" => 2018-11-24T05:40:36.685Z,
"path" => "/path/es/logs/test.log",
"host" => "Eden666",
"@version" => "1",
"type" => "test"
}
查看ES中是否存在 索引为logstash-test-2018.11.24的日志数据
[root@Eden666 config]# curl localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open users TsYd0ZeWTaKB7uakuBRHZw 5 1 4 0 14.9kb 14.9kb
yellow open logstash-test-2018.11.24 MYBcKD_7SmC2y2gYwGMBOQ 5 1 6
启动Kibana。
先配置config/kibana.yml文件内容如下
server.port: 5601 //访问端口,可修改其他的,若外网访问阿里云服务器需添加安全组规则。
server.host: "0.0.0.0" //kibana访问地址
elasticsearch.url: "http://localhost:9200" //ES访问地址
启动成功后,外网就可以访问Kibana了。如下
- 综上是ELK6搭建的简单过程,当然Logstash还有很多配置收集数据方式以及过滤数据方式,这里只是简单配置搭建ELK系统。