设为首页 收藏本站
查看: 1135|回复: 0

[经验分享] 实战 ELK 日志分析、存储、展示

[复制链接]

尚未签到

发表于 2019-1-28 09:43:43 | 显示全部楼层 |阅读模式
ElasticSearch 简介

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

logstash 简介

简单来说logstash就是一根具备实时数据传输能力的管道,负责将数据信息从管道的输入端传输到管道的输出端;与此同时这根管道还可以让你根据自己的需求在中间加上滤网,Logstash提供里很多功能强大的滤网以满足你的各种应用场景。

kibana 简介

Kibana 是一个开源的分析和可视化平台,旨在与 Elasticsearch 合作。Kibana 提供搜索、查看和与存储在 Elasticsearch 索引中的数据进行交互的功能。开发者或运维人员可以轻松地执行高级数据分析,并在各种图表、表格和地图中可视化数据。

ELK 工作大概流程图 :



ES :索引、存储、分析日志       logstash : 收集日志     kibana :展示日志


本案环境 :


角色
主机名
IP地址
软件




节点1
ElasticSearch
192.168.100.129
jdk、elasticsearch 、logstash 、kibana


节点2
node 2
192.168.100.130
jdk、elasticsearch

搭建 ElasticSearch

1.关闭防火墙 :

systemctl stop firewalld.service
setenforce 0
2.配置 yum 源 :

vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
3.安装服务 :

yum install elasticsearch -y      #安装 elassearch 服务
yum install java -y   #安装 java 环境
4.编辑配置文件 :

vim /etc/elasticsearch/elasticsearch.yml
17行 集群名称
cluster.name: abner
23行 节点名称
node.name: node1   
33行 工作目录
path.data: /data/es-data          #文件不存在
path.logs: /var/log/elasticsearch/
43行 防止交换swap分区
bootstrap.memory_lock: true
54行 监听网络
network.host: 0.0.0.0
58行 端口
http.port: 9200
5.创建工作目录 :

mkdir -p /data/es-data   
chown -R elasticsearch:elasticsearch /data/es-data/    #修改文件属主属组
6.开启服务 :

systemctl start elasticsearch.service
netstat -ntap | grep 9200
7. elasticsearch 内存限制 :

less /var/log/elasticsearch/test.log   #查看日志
......
[2018-08-21 16:45:52,015][WARN ][bootstrap                ] These can be adjusted by modifying /etc/security/limits.conf, for example:
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
......
vim /etc/security/limits.conf       #末尾添加 不限制内存
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
systemctl restart elasticsearch.service    #重启服务
8.文件数量限制 :

ulimit -a         #查看文件限制数量
......
open files                      (-n) 1024
......
vim /etc/security/limits.conf       #末尾添加
* soft nofile 65535        
* hard nofile 65535
#重启服务器生效
测试访问 http://192.168.100.129:9200


两种方法和ES进行交互 :

第一种  JAVA API :

[root@localhost ~]# curl -i -XGET 'http://192.168.100.129:9200/_count?pretty' -d '{
> "query": {
>     "match_all": {}
> }
> }'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 95
{
"count" : 0,
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
}
}
第二种  RESTful API (通过json格式交互)

1.安装插件 :

[root@localhost ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head   #安装 head 插件
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ....................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/share/elasticsearch/plugins/head

2.插件安装位置 :

[root@localhost ~]# ls /usr/share/elasticsearch/plugins/  #插件安装位置 不需要直接删除即可
head
3.测试访问 http://192.168.100.129:9200/_plugin/head/


搭建 ElasticSearch 群集

1.在 ES 节点1服务器修改配置文件 :

vim /etc/elasticsearch/elasticsearch.yml
69行 自动发现机制
discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.100.130"]
2.安装步骤同上,配置 ES 节点2服务器 :

vim /etc/elasticsearch/elasticsearch.yml
17行 集群名称
cluster.name: test     #相同
23行 节点名称
node.name: node2  #不同
33行 工作目录
path.data: /data/es-data          #文件不存在
path.logs: /var/log/elasticsearch/
43行 防止交换swap分区
bootstrap.memory_lock: true
54行 监听网络
network.host: 0.0.0.0
58行 端口
http.port: 9200
69行 自动发现机制
discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.100.129"]
3.开启服务 :

systemctl restart elasticsearch.service   #节点1服务器重启服务、节点2服务器开启服务
4.测试访问 http://192.168.100.129:9200/_plugin/head/ :


搭建 logstash

1.配置 yum 源 :

vim /etc/yum.repos.d/logstash.repo
[logstash-2.1]
name=Logstash repository for 2.1.x packages
baseurl=http://packages.elastic.co/logstash/2.1/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
2.安装 logstash 服务 :

yum install logstash -y   #默认安装在opt下
3.logstash 收集系统日志 :

ln -s /opt/logstash/bin/logstash /usr/bin/   #建立软连接
[root@localhost ~]# vim file.conf     #编写配置文件
input {                                  #输入信息
file {
path => "/var/log/messages"     #系统日志位置
type => "system"                #类型
start_position => "beginning"   #友好显示
}
}
output {                                     #输出信息
elasticsearch {                         #输出到 ES
hosts => ["192.168.100.129:9200"]  #ES 服务器
index => "system-%{+YYYY.MM.dd}"   #system 自定义 后面格式固定
}
}
[root@localhost ~]# logstash -f file.conf    #收集信息  
# -f: 指定加载后缀为conf文件的 logstash 配置模板。
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default filter workers: 1
Logstash startup completed
^CSIGINT received. Shutting down the pipeline. {:level=>:warn}   #Ctrl+c
^CSIGINT received. Terminating immediately.. {:level=>:fatal}    #Ctrl+c
4.访问 http://192.168.100.129:9200/_plugin/head/ 查看信息收集 :


5.收集多服务日志,系统日志和 java 日志 :

vim  file.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}     
file {
path => "/var/log/elasticsearch/test.log"    #java日志位置
type => "es-error"
start_position => "beginning"
}  
}
output {
if [type] == "system" {      # if 判断语句
elasticsearch {
hosts => ["192.168.100.129:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.100.129:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
[root@localhost ~]# logstash -f file.conf    #收集信息
6.访问 http://192.168.100.129:9200/_plugin/head/ 查看信息收集 :


7.codec 插件处理堆栈信息 :

vim multiline.conf
input {
stdin {
codec => multiline {  #codec 插件
pattern => "^\["    #引用正则表达式   
negate => true
what => "previous"
}
}
}
output {
stdout {
codec => "rubydebug"
}
}
[root@localhost ~]# logstash -f multiline.conf
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default filter workers: 1
Logstash startup completed
[1]
[2]   #一个完整的[]算一个事件
{
"@timestamp" => "2018-08-21T11:56:30.674Z",
"message" => "[1]",
"@version" => "1",
"host" => "localhost.localdomain"
}
[abc
{
"@timestamp" => "2018-08-21T11:56:37.894Z",
"message" => "[2]",
"@version" => "1",
"host" => "localhost.localdomain"
}
de
fg]
[abc123]
{
"@timestamp" => "2018-08-21T11:56:58.147Z",
"message" => "[abc\nde\nfg]",
"@version" => "1",
"tags" => [
[0] "multiline"
],
"host" => "localhost.localdomain"
}

8.重新编辑 file.conf 文件 :

vim file.conf     #编辑
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/elasticsearch/test.log"
type => "es-error"
start_position => "beginning"
codec => multiline {           #添加codec插件
pattern => "^\["
negate => true
what => "previous"
}
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.100.129:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.100.129:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}

搭建 kibana

1.下载软件包 :

wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
2.修改配置文件 :

tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
mv kibana-4.3.1-linux-x64/ kibana    #重命名
vim /usr/local/kibana/config/kibana.yml
//2行
server.port: 5601
//5行
server.host: "0.0.0.0"
//12行 ES地址
elasticsearch.url: "http://192.168.175.132:9200"   #从 ES 中拿数据
//20行
kibana.index: ".kibana"
3.启动监控 :

[root@localhost local]# /usr/local/kibana/bin/kibana
log   [20:17:53.006] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
log   [20:17:53.028] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
......
4.访问 http://192.168.100.129:5601








运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668546-1-1.html 上篇帖子: 大型架构ELK 下篇帖子: 从零开始搭建ELK+GPE监控预警系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表