Elastic Stack日志采集方案
说明
本文基于elastic stack 相关组件版本号为:6.1.2,linux操作系统,单节点测试部署。
Elastic Stack简介:
ElasticStack 是一系列开源产品的合集,包括 Elasticsearch、Logstash 、Kibana、Beat等等。
Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。Logstash处理数据主要分为三个阶段,inputs、filters、outputs,其中各个阶段,官方提供了丰富的插件,可以对数据进行处理。
Kibana也是一个开源和免费的工具,Kibana可以为Logstash 和ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
Filebeat隶属于Beats。目前Beats包含四种工具:
- Packetbeat(搜集网络流量数据)
- Topbeat(搜集系统、进程和文件系统级别的CPU 和内存使用情况等数据)
- Filebeat(轻量级的日志收集处理工具(Agent),数据占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方推荐)
- Winlogbeat(搜集 Windows 事件日志数据)
官方还提供X-Pack以支持安全验证,服务器监控,报警与通知,报表,以及机器学习相关功能,但是收费,安装时,默认30天试用期。
日志采集方案:
日志采集,可以分为两种:
- 常规应用日志采集,由filebeat中model提供的日志采集模块进行日志采集工作,目前官网提供以下日志采集模块:
- 特定应用日志采集,可以选择自定义beat采集日志,或者由beat采集日志搬运给logstach,由logstach的fileter、output、codec相关插件处理。
采集流程filebeat--> elasticsearch--> kibana 或者 filebeat -->logstach--> elasticsearch--> kibana
elasticsearch和kibana的安装,并整合x-pack
1. 在官网下载elasticsearch-6.1.2.tar.gz;
2. 在官网下载kibana-6.1.2-linux-x86_64.tar.gz;
3. 安装x-pack到elasticsearch,相关命令:
tar –xvf elasticsearch-6.1.2.tar.gz
cd elasticsearch-6.1.2
bin/elasticsearch-plugininstall x-pack
bin/elasticsearch
bin/x-pack/setup-passwordsauto #生成默认的密码,需要记录下相关用户密码
4. 安装x-pack到kibana,相关命令:
tar –xvf kibana-6.1.2-linux-x86_64.tar.gz
cd kibana-6.1.2-linux-x86_64
bin/kibana-plugininstall x-pack
5. 把认证信息填到kibana.yml中
elasticsearch.username:"kibana"
elasticsearch.password: "<pwd>"
#"<pwd>是第三步生成的kibana用户的密码。
6. 启动kibana
bin/kibana
7. 打开kibana
在浏览器中输入http://ip:5601
输入认证信息
Username:elastic Password: <pwd>
整合logstash与filebeat
1. 在官网下载logstash-6.1.2.tar.gz安装包
2. 解压安装包,编写配置文件first-pipeline.conf,配置文件信息如下:
input {
beats {
port => "5044"
}
}
# Thefilter part of this file is commented out to indicate that it is
#optional.
filter {
grok {
match => { "message" =>"%{COMBINEDAPACHELOG}"}
}
geoip {
source => "clientip"
}
}
output {
elasticsearch {
user => "elastic"
password => "pwd"
hosts=> [ "localhost:9200" ]
}
}
3. 启动logstash
bin/logstash -ffirst-pipeline.conf --config.reload.automatic
4. 在官网下载filebeat-6.1.2-linux-x86_64.tar.gz安装包
5. 解压安装包,配置filebeat.yml
setup.kibana:
host: "ip:5601"
username: "elastic"
password: "elastic"
output.logstash:
hosts: ["localhost:5044"]
6. 进入modules.d目录,把nginx.yml.disable重命名为nginx.yml,并配置以下信息
- module:nginx
# Access logs
access:
enabled: true
# Set custom paths for the log files. Ifleft empty,
# Filebeat will choose the paths dependingon your OS.
var.paths:["/usr/local/nginx/logs/access.log*"]
# Error logs
error:
enabled: true
# Set custom paths for the log files. Ifleft empty,
# Filebeat will choose the paths dependingon your OS.
var.paths:["/usr/local/nginx/logs/error.log*"]
7. 启动filebeat
./filebeat -e -c filebeat.yml -d"publish"
8. 查询数据
filebeat配置输出可以直接输出到elasticsearch,效果如下
查看dashboard数据
参考文献:
https://www.elastic.co/cn/start
https://www.elastic.co/guide/index.html
https://www.cnblogs.com/aresxin/p/8035137.html