ELK日志分析平台搭建全过程
一、使用背景
当生产环境有很多服务器、很多业务模块的日志需要每时每刻查看时
二、环境
系统:centos 6.5
JDK:1.8
Elasticsearch-5.0.0
Logstash-5.0.0
kibana-5.0.0
三、安装
1、安装JDK
下载JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
本环境下载的是64位tar.gz包,将安装包拷贝至安装服务器/usr/local目录
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -xzvf jdk-8u111-linux-x64.tar.gz
配置环境变量
[root@localhost local]# vim /etc/profile
将下面的内容添加至文件末尾(假如服务器需要多个JDK版本,为了ELK不影响其它系统,也可以将环境变量的内容稍后添加到ELK的启动脚本中)
JAVA_HOME=/usr/local/jdk1.8.0_111
JRE_HOME=/usr/local/jdk1.8.0_111/jre
CLASSPATH=.:$JAVA_HOME/lib:/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
ulimit -u 4096
[root@localhost local]# source /etc/profile
配置limit相关参数
[root@localhost local]# vim /etc/security/limits.conf
添加以下内容
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
创建运行ELK的用户
[root@localhost local]# groupadd elk
[root@localhost local]# useradd -g elk elk
<iframe id="iframe_0.5598784179658625" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 367px; height: 135px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/105046_pQqO_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5598784179658625',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
创建ELK运行目录
[root@localhost local]# mkdir /elk
[root@localhost local]# chown -R elk:elk /elk
关闭防火墙:
[root@localhost ~]# iptables -F
以上全部是root用户完成
2、安装ELK
以下由elk用户操作
以elk用户登录服务器
下载ELK安装包:https://www.elastic.co/downloads,并上传到服务器且解压,解压命令:tar -xzvf 包名
<iframe id="iframe_0.0815892494236754" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 599px; height: 186px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/105703_Cv7F_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.0815892494236754',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
配置Elasticsearch
<iframe id="iframe_0.10374600164018122" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 462px; height: 72px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/105827_8GZs_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.10374600164018122',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
修改如下内容:
<iframe id="iframe_0.07410717744910933" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 227px; height: 126px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/105906_zUJ2_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.07410717744910933',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
<iframe id="iframe_0.33557386976636194" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 259px; height: 96px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/105923_AuR5_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.33557386976636194',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
保存退出
启动Elasticsearch
<iframe id="iframe_0.40192665314822595" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 570px; height: 24px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/110110_ZgnH_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.40192665314822595',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
查看是否启动成功
<iframe id="iframe_0.9350422771105631" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 629px; height: 519px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/110158_he0l_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9350422771105631',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
用浏览器访问:http://192.168.10.169:9200
<iframe id="iframe_0.9338390194613313" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 602px; height: 284px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/110253_sJnh_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9338390194613313',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
Elasticsearch安装完毕
安装logstash
logstash是ELK中负责收集和过滤日志的
<iframe id="iframe_0.5476563389939548" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 410px; height: 53px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/110555_5ckN_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5476563389939548',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
编写配置文件如下:
<iframe id="iframe_0.3824549073131953" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 458px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/110650_4SLy_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3824549073131953',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
解释:
logstash的配置文件须包含三个内容:
input{}:此模块是负责收集日志,可以从文件读取、从redis读取或者开启端口让产生日志的业务系统直接写入到logstash
filter{}:此模块是负责过滤收集到的日志,并根据过滤后对日志定义显示字段
output{}:此模块是负责将过滤后的日志输出到elasticsearch或者文件、redis等
本环境采用从文件读取日志,业务系统产生日志的格式如下:
[2016-11-05 00:00:03,731 INFO] [http-nio-8094-exec-10] [filter.LogRequestFilter] - /merchant/get-supply-detail.shtml, IP: 121.35.185.117, [device-dpi = 414*736, version = 3.6, device-os = iOS8.4.1, timestamp = 1478275204, bundle = APYQ9WATKK98V2EC, device-network = WiFi, token = 393E38694471483CB3686EC77BABB496, device-model = iPhone, device-cpu = , sequence = 1478275204980, device-uuid = C52FF568-A447-4AFE-8AE8-4C9A54CED10C, sign = 0966a15c090fa6725d8e3a14e9ef98dc, request = {
"supply-id" : 192
}]
[2016-11-05 00:00:03,731 DEBUG] [http-nio-8094-exec-10] [filter.ValidateRequestFilter] - Unsigned: bundle=APYQ9WATKK98V2EC&device-cpu=&device-dpi=414*736&device-model=iPhone&device-network=WiFi&device-os=iOS8.4.1&device-uuid=C52FF568-A447-4AFE-8AE8-4C9A54CED10C&request={
"supply-id" : 192
output直接输出到Elasticsearch
本环境需处理两套业务系统的日志
<iframe id="iframe_0.943884629889175" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 472px; height: 354px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/111658_5Uuu_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.943884629889175',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
type:代表类型,其实就是将这个类型推送到Elasticsearch,方便后面的kibana进行分类搜索,一般直接命名业务系统的项目名
path:读取文件的路径
<iframe id="iframe_0.6482273387038273" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 224px; height: 85px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/111953_4YUe_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6482273387038273',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
这个是代表日志报错时,将报错的换行归属于上一条message内容
start_position => "beginning"是代表从文件头部开始读取
<iframe id="iframe_0.051983671744315174" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 112px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/112220_hFBY_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.051983671744315174',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
filter{}中的grok是采用正则表达式来过滤日志,其中%{TIMESTAMP_ISO8601}代表一个内置获取2016-11-05 00:00:03,731时间的正则表达式的函数,%{TIMESTAMP_ISO8601:date1}代表将获取的值赋给date1,在kibana中可以体现出来
本环境有两条grok是代表,第一条不符合将执行第二条
<iframe id="iframe_0.2657880905321013" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 391px; height: 149px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/112742_n814_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2657880905321013',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
其中index是定义将过滤后的日志推送到Elasticsearch后存储的名字
%{type}是调用input中的type变量(函数)
启动logstash
<iframe id="iframe_0.640560640535079" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 16px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/113125_RDU9_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.640560640535079',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
<iframe id="iframe_0.7989725293287735" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 94px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/113232_XDky_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7989725293287735',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
代表启动成功
安装kibana
<iframe id="iframe_0.7789425109757211" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 474px; height: 58px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/113318_OzBV_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7789425109757211',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
<iframe id="iframe_0.653312015653114" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 295px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/113341_w95f_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.653312015653114',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
保存退出
启动kibana
<iframe id="iframe_0.49168528510009146" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 107px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/113457_At8S_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.49168528510009146',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
<iframe id="iframe_0.14360340804598515" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 352px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/113614_4JLN_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.14360340804598515',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
<iframe id="iframe_0.5319714407383598" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 362px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://static.oschina.net/uploads/space/2016/1110/113653_vanl_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5319714407383598',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
其中api-app-*和api-cxb-*从<iframe id="iframe_0.03820734213560706" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 0px; height: 0px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/112742_n814_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.03820734213560706',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>来的,*代表所有
<iframe id="iframe_0.09740154046682292" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 686px; height: 294px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/114021_cGJH_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09740154046682292',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
代表实时收集的日志条数
<iframe id="iframe_0.31039741402983934" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 0px; height: 0px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://static.oschina.net/uploads/space/2016/1110/114329_AWy9_3030681.png?_=6059231%22%20style=%22border:none;max-width:686px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.31039741402983934',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
红色框内的就是在刚才filter过滤规则中定义的