ELK stack是什么?
ELK Stack 是 Elasticsearch、Logstash、Kibana 三个开源软件的组合。在实时数据检索和分析场合,三者通常是配合共用,而且又都先后归于 Elastic.co 公司名下,故有此简称。
ELK Stack 在最近两年迅速崛起,成为机器数据分析,或者说实时日志处理领域,开源界的第一选择。和传统的日志处理方案相比,ELK Stack 具有如下几个优点:
- 处理方式灵活。Elasticsearch 是实时全文索引,不需要像 storm 那样预先编程才能使用;
- 配置简易上手。Elasticsearch 全部采用 JSON 接口,Logstash 是 Ruby DSL 设计,都是目前业界最通用的配置语法设计;
- 检索性能高效。虽然每次查询都是实时计算,但是优秀的设计和实现基本可以达到全天数据查询的秒级响应;
- 集群线性扩展。不管是 Elasticsearch 集群还是 Logstash 集群都是可以线性扩展的;
- 前端操作炫丽。Kibana 界面上,只需要点击鼠标,就可以完成搜索、聚合功能,生成炫丽的仪表板。
Logstash是能够用于收集,处理和转发事件或日志信息的工具.
Logstash : Logstash服务的组件,用于处理日志
Elasticsearch : 用于存储,索引日志
Kibana 4: 搜索和可视化的日志的Web界面.
Elasticsearch和Logstash需要运行Java环境,并且版本最好是1.7以上.
首先必须安装java7及以上的版本
http://www.java.com/zh_CN/download/linux_manual.jsp
[root@192_168_174_68 home]# wget 'http://sdlc-esd.oracle.com/ESD6/JSCDL/jdk/8u73-b02/jre-8u73-linux-x64.rpm?GroupName=JSC&FilePath=/ESD6/JSCDL/jdk/8u73-b02/jre-8u73-linux-x64.rpm&BHost=javadl.sun.com&File=jre-8u73-linux-x64.rpm&AuthParam=1457343423_73675d83c3aa00d667217e09e10d85c2&ext=.rpm' -O jre-8u73-linux-x64.rpm
--2016-03-07 17:18:01-- http://sdlc-esd.oracle.com/ESD6/JSCDL/jdk/8u73-b02/jre-8u73-linux-x64.rpm?GroupName=JSC&FilePath=/ESD6/JSCDL/jdk/8u73-b02/jre-8u73-linux-x64.rpm&BHost=javadl.sun.com&File=jre-8u73-linux-x64.rpm&AuthParam=1457343423_73675d83c3aa00d667217e09e10d85c2&ext=.rpm
Resolving sdlc-esd.oracle.com... 96.7.54.179, 96.7.54.187
Connecting to sdlc-esd.oracle.com|96.7.54.179|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 49210622 (47M) [application/x-sdlc]
Saving to: “jre-8u73-linux-x64.rpm”
100%[=================================================>] 49,210,622 1.58M/s in 31s
2016-03-07 17:18:32 (1.51 MB/s) - “jre-8u73-linux-x64.rpm” saved [49210622/49210622]
安装jdk
[root@192_168_174_68 home]# yum install jre-8u73-linux-x64.rpm -y
[root@192_168_174_68 home]# java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
Elasticsearch 2.2.0安装
YUM / DNF安装
下载并安装公钥:
[root@192_168_174_68 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
创建/etc/yum.repos.d/elasticsearch.repo文件,复制下面的内容到文件中
[root@192_168_174_68 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
安装elasticsearch
[root@192_168_174_68 ~]# yum install elasticsearch #新版本的Fedora使用 dnf install elasticsearch
配置elasticsearch服务自动启动,SysV init 版本使用
chkconfig --add elasticsearch
systemd使用:
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
修改elasticsearch配置
[root@192_168_174_68 ~]# mkdir /opt/elasticsearch.path.data
[root@192_168_174_68 elasticsearch]# vim /etc/elasticsearch/elasticsearch.yml
path.data: /opt/elasticsearch.path.data
network.host: 0.0.0.0
启动服务:
[root@192_168_174_68 opt]# service elasticsearch start
Starting elasticsearch: [ OK ]
[root@192_168_174_68 opt]# ps aux |grep elastic
498 8171 24.3 3.2 6815980 263784 ? Sl 17:24 0:10 /usr/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/share/elasticsearch -cp /usr/share/elasticsearch/lib/elasticsearch-2.2.0.jar:/usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch start -p /var/run/elasticsearch/elasticsearch.pid -d -Des.default.path.home=/usr/share/elasticsearch -Des.default.path.logs=/var/log/elasticsearch -Des.default.path.data=/var/lib/elasticsearch -Des.default.path.conf=/etc/elasticsearch
root 8283 0.0 0.0 103252 844 pts/1 S+ 17:25 0:00 grep elastic
[root@192_168_174_68 opt]# netstat -lntp |grep 8171
tcp 0 0 ::ffff:127.0.0.1:9200 :::* LISTEN 8171/java
tcp 0 0 ::1:9200 :::* LISTEN 8171/java
tcp 0 0 ::ffff:127.0.0.1:9300 :::* LISTEN 8171/java
tcp 0 0 ::1:9300 :::* LISTEN 8171/java
[root@192_168_174_68 opt]#
服务已经启动.
![](https://i-blog.csdnimg.cn/blog_migrate/8ae4493d947834ffcc9f7dbeb764fa3d.png)
kibana4.4.1安装:
在下载主界面,有这么一行字,所以要注意与安装的elasticsearch相匹配.
Note: Kibana 4.4.1 requires Elasticsearch 2.2.
下载并安装公钥:
[root@192_168_174_68 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
创建/etc/yum.repos.d/elasticsearch.repo文件,复制下面的内容到文件中
[root@192_168_174_68 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
安装elasticsearch
[root@192_168_174_68 ~]# yum install elasticsearch #新版本的Fedora使用 dnf install elasticsearch
配置elasticsearch服务自动启动,SysV init 版本使用
chkconfig --add elasticsearch
systemd使用:
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
修改elasticsearch配置
[root@192_168_174_68 ~]# mkdir /opt/elasticsearch.path.data
[root@192_168_174_68 elasticsearch]# vim /etc/elasticsearch/elasticsearch.yml
path.data: /opt/elasticsearch.path.data
network.host: 0.0.0.0
启动服务:
[root@192_168_174_68 opt]# service elasticsearch start
Starting elasticsearch: [ OK ]
[root@192_168_174_68 opt]# ps aux |grep elastic
498 8171 24.3 3.2 6815980 263784 ? Sl 17:24 0:10 /usr/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/share/elasticsearch -cp /usr/share/elasticsearch/lib/elasticsearch-2.2.0.jar:/usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch start -p /var/run/elasticsearch/elasticsearch.pid -d -Des.default.path.home=/usr/share/elasticsearch -Des.default.path.logs=/var/log/elasticsearch -Des.default.path.data=/var/lib/elasticsearch -Des.default.path.conf=/etc/elasticsearch
root 8283 0.0 0.0 103252 844 pts/1 S+ 17:25 0:00 grep elastic
[root@192_168_174_68 opt]# netstat -lntp |grep 8171
tcp 0 0 ::ffff:127.0.0.1:9200 :::* LISTEN 8171/java
tcp 0 0 ::1:9200 :::* LISTEN 8171/java
tcp 0 0 ::ffff:127.0.0.1:9300 :::* LISTEN 8171/java
tcp 0 0 ::1:9300 :::* LISTEN 8171/java
[root@192_168_174_68 opt]#
服务已经启动.
![](https://i-blog.csdnimg.cn/blog_migrate/8ae4493d947834ffcc9f7dbeb764fa3d.png)
kibana4.4.1安装:
在下载主界面,有这么一行字,所以要注意与安装的elasticsearch相匹配.
Note: Kibana 4.4.1 requires Elasticsearch 2.2.
下载安装包.
[root@192_168_174_68 ~]# wget https://download.elastic.co/kibana/kibana/kibana-4.4.1-linux-x64.tar.gz
[root@192_168_174_68 ~]# tar zxvf kibana-4.4.1-linux-x64.tar.gz -C /opt/
[root@192_168_174_68 ~]# cd /opt/kibana-4.4.1-linux-x64/
修改配置文件
[root@192_168_174_68 kibana-4.4.1-linux-x64]# vim config/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.174.68:9200"
启动服务
[root@192_168_174_68 kibana-4.4.1-linux-x64]# nohup ./bin/kibana -l /var/log/kibana4.4.1.log &
[1] 8867
nohup: ignoring input and appending output to `nohup.out'
![](https://i-blog.csdnimg.cn/blog_migrate/6add0ab3743ca0c1cc4d558c7218fa50.png)
logstash安装
![](https://i-blog.csdnimg.cn/blog_migrate/fc8e1734e53c072a78492a2b358706a2.png)
YUM源安装方式
下载安装公钥
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
创建/etc/yum.repo.d/logstash.repo文件
[root@192_168_174_68 ~]# vim /etc/yum.repos.d/logstash.repo
[logstash-2.2]
name=Logstash repository for 2.2.x packages
baseurl=http://packages.elastic.co/logstash/2.2/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
[root@192_168_174_68 ~]# yum install logstash -y
![](https://i-blog.csdnimg.cn/blog_migrate/3a7b5f353805824d13b2cf7821e0ee50.png)
测试:
[root@192_168_174_68 logstash]# JAVA_HOME=/usr/java/jre1.8.0_73 && /opt/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'
Settings: Default pipeline workers: 8
Logstash startup completed
talen
2016-03-07T09:51:08.290Z 192_168_174_68 talen
测试OK
下面使用log4j模块收集日志
从Log4j SocketAppender通过TCP读取事件.只支持log4j 1.x版本.根据配置mode的不同,可以接受客户端连接也可以连接到服务端.根据mode不同,必需在对端配置SocketAppender或SocketHubAppender.
编辑业务的log4j配置,添加如下信息
log4j.rootLogger=logstash
log4j.appender.logstash=org.apache.log4j.net.SocketAppender
log4j.appender.logstash.port=56789
log4j.appender.logstash.remoteHost=192.168.174.68
修改logstash配置信息
[root@192_168_174_68 ~]# cat /etc/logstash/conf.d/3rd.conf
input {
log4j {
mode => "server"
host => "0.0.0.0"
port => 56789
type => "log4j"
}
}
#The geoip plugin configuration requires data that is already defined as separate fields. Make sure that the geoip section is after the grok section of the configuration file.
output {
elasticsearch {
hosts => "192.168.174.68:9200"
}
}
在浏览器中输入http://192.168.174.68:5601,可以看到收集上来的日志
![](https://i-blog.csdnimg.cn/blog_migrate/9fa87c04bd9350399112442980d71e40.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cbc43667963d3c7ddddf4103433f260a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2824619f17430232c10e7dc235002220.png)
问题一:
[root@192_168_174_68 ~]# service elasticsearch start
Starting elasticsearch: Exception in thread "main" java.lang.IllegalStateException: Unable to access 'path.data' (/opt/elasticsearch.path.data/elasticsearch)
Likely root cause: java.nio.file.AccessDeniedException: /opt/elasticsearch.path.data/elasticsearch
at sun.nio.fs.UnixException.translateToIOException(Unknown Source)
at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.UnixFileSystemProvider.createDirectory(Unknown Source)
at java.nio.file.Files.createDirectory(Unknown Source)
at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source)
at java.nio.file.Files.createDirectories(Unknown Source)
at org.elasticsearch.bootstrap.Security.ensureDirectoryExists(Security.java:337)
at org.elasticsearch.bootstrap.Security.addPath(Security.java:314)
at org.elasticsearch.bootstrap.Security.addFilePermissions(Security.java:259)
at org.elasticsearch.bootstrap.Security.createPermissions(Security.java:212)
at org.elasticsearch.bootstrap.Security.configure(Security.java:118)
at org.elasticsearch.bootstrap.Bootstrap.setupSecurity(Bootstrap.java:196)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:167)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.
[FAILED]
解决:
[root@192_168_174_68 opt]# chmod 777 /opt/elasticsearch.path.data/
参考:
https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
https://www.ttlsa.com/elk/howto-install-elasticsearch-logstash-and-kibana-elk-stack/