news 2026/6/11 9:23:38

ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

http://my.oschina.net/itblog/blog/547250
http://baidu.blog.51cto.com/71938/1676798
1、ELK由ElasticSearch、Logstash和Kiabana三个开源工具组成。官方网站:https://www.elastic.co/products
• Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
• Logstash是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
• Kibana 也是一个开源和免费的工具,它Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。




• 画了一个ELK工作的原理图:

• 如图:Logstash收集AppServer产生的Log,并存放到ElasticSearch集群中,而Kibana则从ES集群中查询数据生成图表,再返回给Browser。
2、环境:
Linux:

Elasticsearch:elasticsearch-2.3.4.tar.gz
Logstash:logstash-2.3.4.tar.gz
Kibana:kibana-4.5.2-linux-x64.tar.gz
Jdk:jdk-8u91-linux-x64.tar.gz
3、jdk的安装:
mkdir -p /usr/local/java/
tar -zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/java/
vim /etc/profile
加入:
JAVA_HOME=/usr/local/java/jdk1.8.0_91
PATH=J A V A H O M E / b i n : JAVA_HOME/bin:JAVAHOME/bin:PATH
CLASSPATH=.:J A V A H O M E / l i b / d t . j a r : JAVA_HOME/lib/dt.jar:JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH

执行:
. /etc/profile #生效配置
java -version
4、安装配置Elasticsearch
4.1、安装配置Elasticsearch
tar -zxvf elasticsearch-2.3.4.tar.gz
cd elasticsearch-2.3.4
然后编辑ES的配置文件:
vim config/elasticsearch.yml
修改以下配置项:
cluster.name: wen
node.name: node-1
path.data: /path/to/data
path.logs: /path/to/logs
当前hostname或IP,这里使用ip
network.host: 10.10.20.210
http.port: 9200

4.2、启动elasticsearch
./bin/elasticsearch
报错:

不能使用root用户启动elasticsearch
解决方法: http://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ
将/data/soft/ch/elasticsearch-2.3.4/所属用户和组改为另外一个非root账户:
4.2.1、创建esgp组
groupadd esgp
4.2.2、创建用户es 添加到esgp组设置用户的密码为es123
useradd es -g esgp -p es123
4.2.3、更改elasticsearch-2.3.4文件夹及内部文件的所属用户及组为es:esgp
chown -R es:esgp elasticsearch-2.3.4


4.2.4、切换到es用户
su es
4.2.5、启动elasticsearch
./elasticsearch-2.3.4/bin/elasticsearch

修改/path/to/logs/和/path/to/data/的权限:设置/path的用户为es,组为esgp
su root
chown -R es:esgp /path

再启动elasticsearch

使用ctrl+C停止。当然,也可以使用后台进程的方式启动ES:
./elasticsearch-2.3.4/bin/elasticsearch –d
或者
./elasticsearch-2.3.4/bin/elasticsearch &

4.2.6、可以打开页面10.10.20.210:920,将会看到以下内容


5、安装elasticsearch的插件:elasticsearch-head
https://github.com/mobz/elasticsearch-head
http://blog.csdn.net/july_2/article/details/24481935
5.1、插件安装方法1:
5.1.1.#./elasticsearch-2.3.4/bin/plugin -install mobz/elasticsearch-head
5.1.2.运行es
5.1.3.打开http://10.10.20.210:9200/_plugin/head/
5.2、插件安装方法2:
5.2.1.https://github.com/mobz/elasticsearch-head下载zip 解压

5.2.2.建立elasticsearch-2.3.4/plugins/head/_site文件
mkdir -p ./elasticsearch-2.3.4/plugins/head/_site
5.2.3.将解压后的elasticsearch-head-master文件夹下的文件copy到_site
su root #切换到root用户
unzip elasticsearch-head-master.zip #解压
cp -r /data/soft/ch/elasticsearch-head-master/* /data/soft/ch/elasticsearch-2.3.4/plugins/head/_site/

5.2.4.运行es
su es #切换到es用户
/data/soft/ch/elasticsearch-2.3.4/bin/elasticsearch


会报错,如上,拷贝plugin-descriptor.properties文件
cp /data/soft/ch/elasticsearch-2.3.4/plugins/head/_site/plugin-descriptor.properties /data/soft/ch/elasticsearch-2.3.4/plugins/head/
再启动elasticsearch
5.2.5.打开http://10.10.20.210:9200/_plugin/head/

6、安装logstash-2.3.4.tar.gz
tar -zxvf logstash-2.3.4.tar.gz
cd logstash-2.3.4
mkdir config
touch file_to_es.conf #创建file_to_es.conf文件

vim file_to_es.conf
输入内容:
#For detail structure of this file
#Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
input {

For detail config for file as input,

See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html

file{
path => “/data/java/apache-tomcat-ware/logs/catalina.out”
}

For detail config for log4j as input,

See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html

#log4j {

mode => “server”

host => “10.10.20.210”

port => 4567

#}
}
filter {
#Only matched data are send to output.
}
output {

For detail config for elasticsearch as output,

See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html

elasticsearch {
action => “index” #The operation on ES
hosts => “10.10.20.210:9200” #ElasticSearch host, can be array.
index => “ware-manager-dev” #The index to write data to.
}
}

启动logstash
./logstash-2.3.4/bin/logstash agent -f ./logstash-2.3.4/config/file_to_es.conf

后台启动(加上&):
./logstash-2.3.4/bin/logstash agent -f ./logstash-2.3.4/config/file_to_es.conf &

查看进程

可以在http://10.10.20.210:9200/_plugin/head/中看到自动创建了ware-manager-dev这个索引,里面保存的是ware-manager这个项目的日志


注意:如果在同一个服务器对不同的文件,设置不同的索引,那么可以增加配置文件然后制定配置文件启动即可

./logstash-2.3.4/bin/logstash agent -f ./logstash-2.3.4/config/order_to_es.conf &

#./logstash-2.3.4/bin/logstash agent -f ./logstash-2.3.4/config/ware_to_es.conf &

order_to_es.conf的内容: # For detail structure of this file # Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html input { file{ path => "/data/applogs/**order**/wrapper.*.log" } # For detail config for log4j as input, # See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html #log4j { # mode => "server" # host => "10.10.20.206" # port => 4567 #} } filter { #Only matched data are send to output. } output { # For detail config for elasticsearch as output, # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html elasticsearch { action => "index" #The operation on ES hosts => "10.10.20.206:9200" #ElasticSearch host, can be array. index => "**order-server-test**" #The index to write data to. } } ware_to_es.conf的内容: # For detail structure of this file # Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html input { file{ path => "/data/applogs/**ware**/wrapper.*.log" } # For detail config for log4j as input, # See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html #log4j { # mode => "server" # host => "10.10.20.206" # port => 4567 #} } filter { #Only matched data are send to output. } output { # For detail config for elasticsearch as output, # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html elasticsearch { action => "index" #The operation on ES hosts => "10.10.20.206:9200" #ElasticSearch host, can be array. index => "**ware-server-test**" #The index to write data to. } }

7、安装kibana
tar -zxvf kibana-4.5.2-linux-x64.tar.gz
cd kibana-4.5.2-linux-x64
vim config/kibana.yml
修改内容如下:
server.port: 5601
server.host: “10.10.20.210”
elasticsearch.url: “http://10.10.20.210:9200”
kibana.index: “.kibana”


注意:其中server.host是安装kibana服务的地址
elasticsearch.url为elasticsearch安装的地址
启动kibana
/data/soft/ch/kibana-4.5.2-linux-x64/bin/kibana

可以通过http://10.10.20.206:5601访问到。

也可以后台启动kibana(加上&)
/data/soft/ch/kibana-4.5.2-linux-x64/bin/kibana&

注意:这时加上了&虽然执行了后台启动,但是还是有日志打印出来,使用ctrl+c可以退出。
但是如果直接关闭了Xshell,这时服务也会停止,访问http://10.10.20.206:5601就失败了。


解决方法:
执行了/data/soft/ch/kibana-4.5.2-linux-x64/bin/kibana &命令后,不使用ctrl+c去退出日志,
而是使用exit;这样即使关闭了Xshell窗口服务也不会挂了。

配置kibana:

这时在elasticsearch中多了一个kibana的索引

为了后续使用Kibana,需要配置至少一个Index名字或者Pattern,它用于在分析时确定ES中的Index。这里我输入之前配置的Index 名字ware-manager-dev,Kibana会自动加载该Index下doc的field,并自动选择合适的field用于图标中的时间字段:

点击Discover查看日志

设置只查看message信息:


设置自动刷新:



更多配置请查看:
http://my.oschina.net/itblog/blog/547250

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/11 9:23:28

httpcode返回码和含义、504、502等、请求方式等

文章目录请求方式请求方式列表请求方式-HEAD请求方式-GET请求方式-POST错误码错误 502:Bad Gateway (错误网关)错误 504:Gateway Timeout (网关超时)错误码(状态码)大全请求方式 请求方式列表 GET POST PUT DELETE PATCH HEAD OPTIONS TRACE 请求方式…

作者头像 李华
网站建设 2026/6/11 9:23:23

【车辆】自动驾驶路径规划 DDPG 强化学习matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

作者头像 李华
网站建设 2026/6/11 9:23:21

js、ts保留两位或n位小数,包括四舍五入 以及 不进位

javascript、typescript 保留两位或n位小数,包括四舍五入 和 不四舍五入进位四舍五入 1. toFixed(n) 一般保留小数基本会先想到用toFixed(x),x区间[0,20],但会四舍五入,返回的是字符串 let num 3.141592653; let n num.toFixed(…

作者头像 李华
网站建设 2026/6/11 9:23:19

基于平方根比例控制的压电微定位平台 XY 双轴步进驱动Matlab仿真

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、程序设计科研仿真。🍎完整代码获取 定制创新 论文复现点击:Matlab科研工作室👇 关注我领取海量matlab电子书和数学建模资料 &#x1f3…

作者头像 李华
网站建设 2026/6/11 9:23:11

PROPER框架:个性化大语言模型的渐进式学习技术解析

1. PROPER框架技术解析:个性化大语言模型的渐进式学习革命在自然语言处理领域,个性化大语言模型(Personalized LLM)正经历从"千人一面"到"千人千面"的范式转变。传统微调方法往往面临两个困境:要么…

作者头像 李华