elk是一个开源的日志分析系统。由Elasticsearch、Logstash、Kibana三部分组成
Elasticsearch:是一个基于Lucene的搜索服务器, 特点就是分布式,开源,实时搜索,稳定快速,安装使用方便 java
Logstash:将对日志进行收集,分析并存储的工具 ruby
kibana:为logstash和ElasticSearch提供日志搜索、分析的web界面。更好的展示出来。
安装:: (根据官网提示安装)
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
https://www.elastic.co/guide/en/logstash/current/index.html
https://www.elastic.co/guide/en/kibana/current/index.html
首先安装java环境:
[[email protected] ~]# yum -y install java-1.8.0-openjdk-devel java-1.8.0-openjdk
先下载Elasticsearch工具yum源的key:
[[email protected] ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
配置yum源:
[[email protected] ~]# tee /etc/yum.repos.d/elasticsearch.repo << EOF [logstash-5.x] name=Elastic repository for 5.x packages baseurl= gpgkey= enabled=1 autorefresh=1 type=rpm-md EOF
安装elk:
[[email protected] ~]# yum install logstash elasticsearch kibana
yum安装路径为:/usr/share/下
三个插件一个一个配置:
1、elasticsearch:修改elasticsearch配置文件:
启动elasticsearch:
[[email protected] ~]# systemctl daemon-reload [[email protected] ~]# systemctl enable elasticsearch.service [[email protected] ~]# systemctl start elasticsearch.service [[email protected] ~]# systemctl status elasticsearch ● elasticsearch.service - Elasticsearch Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2017-06-26 20:56:24 CST; 7min ago Docs: http://www.elastic.co Main PID: 1156 (java) CGroup: /system.slice/elasticsearch.service └─1156 /bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupa... Jun 26 20:56:24 node1 systemd[1]: Starting Elasticsearch... Jun 26 20:56:24 node1 systemd[1]: Started Elasticsearch. Jun 26 21:03:17 node1 systemd[1]: Started Elasticsearch.
使用浏览器访问验证:
2、kibana:配置文件:
启动kibana:
3、logstash配置:
logstash中最麻烦也是重要的配置文件是conf结尾的文件。主要有input(数据来源)、filter(过滤数据)、output(输出数据)三个字段组成。
在logstash的家目录创建三个文件夹。分别用来存放日志,conf配置文件和logstash服务主配置
[[email protected] ~]# mkdir /usr/share/logstash/{etc,config,logs} [[email protected] ~]# cp /etc/logstash/log4j2.properties /usr/share/logstash/config/ [[email protected] ~]# cp /etc/logstash/logstash.yml /usr/share/logstash/config/ [[email protected] ~]# cat /usr/share/logstash/etc/message.conf input { file { path => "/var/log/messages" } } output { elasticsearch { host => ["192.168.199.17:9200"] index => "message-%{+YYYY.MM.dd}" } }
启动logstash
启动没有报错,就在web界面配置索引。并查看获取到的数据
日志数据出来就算是配置完了。
感言:
刚刚接触elk,简单搭建了一下,搭建挺简单的。只要思路有了,工作原理弄明白了。也就没有那么难了。在elk中最难的还是logstash中的过滤。要想深入elk,必须要掌握logstash中的语法,ruby语法。