首页 > 其他分享 >用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统

时间:2022-12-15 19:33:05浏览次数:49  
标签:plugin root redis 192.168 logstash Elasticsearch Kibana soft Logstash


搭建该平台的目的就是为了运维、研发很方便的进行日志的查询。Kibana一个免费的web壳;Logstash集成各种收集日志插件,还是一个比较优秀的正则切割日志工具;Elasticsearch一个开源的搜索引擎框架(支持群集架构方式)。

1 安装需求

1.1 理论拓扑

 

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统_redis

1.2 安装环境



先看看都需要安装什么软件包
ruby 运行Kibana 必须,
rubygems 安装ruby扩展必须
bundler 功能类似于yum
JDK 运行java程序必须
redis 用来处理日志队列
logstash 收集、过滤日志
ElasticSearch 全文搜索服务(logstash集成了一个)
kibana 页面展示



 



192.168.233.128 logstash index,kibana,JDK
192.168.233.129 logstash agent,JDK
192.168.233.130 redis
192.168.233.131 ElasticSearch,JDK

logstash分为 index和agent ,agent负责监控、过滤日志,index负责收集日志并将日志交给ElasticSearch 做搜索。 此外 logstash 的收集方式分为 standalone 和 centralized。

standalone 是所有功能都在一个服务器上面,自发自收,centralized 就是集中收集,一台服务器接收所有shipper(个人理解就是logstash agent)的日志。其实 logstash本身不分 什么 shipper 和 collector ,只不过就是配置文件不同而已,我们这次按照集中的方式来测试。



在 logstash index上安装基础的软件环境:



 

vi /etc/apt/sources.list



deb http://ftp.debian.org/debian/ squeeze main non-free

deb-src http://ftp.debian.org/debian/ squeeze main non-free

# apt-get update

# apt-cache search sun-java

# apt-get install sun-java6-jdk sun-java6-jre

# java -version

 

# apt-get install ruby rubygems

# gem install bundler

开始安装logstash




  1. 其实logstash 就是一个java脚本,不需要安装... 下载即用
  2. [192.168.233.128 root@nodec:/soft]
  3. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
  4. 现在看看 这个脚本应该怎么去执行
  5. [192.168.233.128 root@nodec:/soft]
  6. # java -jar /soft/logstash-1.1.0-monolithic.jar -h
  7. No such command "-h"
  8. Available commands:
  9. -v
  10. -V
  11. --version
  12. agent
  13. web
  14. test
  15. 显然没有 -h 参数,不过列出了能用的参数,但是logstash的参数可不止这些,
  16. java -jar /soft/logstash-1.1.0-monolithic.jar agent --help
  17. 这些是在agent模式下的命令参数
  18. -f, --config CONFIGFILE
  19. Load the logstash config from a specific file, directory, or a wildcard. If given a directory or wildcard, config files will be readinorder
  20. -e CONFIGSTRING
  21. as the configuration data. Same syntax as the config file. If not input is specified, 'stdin { type => stdin }'isdefault. If nooutputis specified, 'stdout { debug => true }}'isdefault.
  22. -w, --filterworks COUNT
  23. COUNT filter workers (default: 1)
  24. --watchdog-timeout TIMEOUT
  25. Set
  26. -l, --log FILE
  27. to a given path. Defaultisto log to
  28. -v
  29. of verbosity available with'-vv'
  30. --pluginpath PLUGIN_PATH
  31. to find other logstash plugins in
  32. java -jar /soft/logstash-1.1.0-monolithic.jar web --help
  33. 下面的是在web界面的参数
  34. --log FILE
  35. to a given path. Defaultis
  36. --address ADDRESS
  37. on which to start webserver. Defaultis
  38. --port PORT
  39. on which to start webserver. Defaultis
  40. -B, --elasticsearch-bind-host ADDRESS
  41. on which to
  42. -b, --backend URL
  43. to use. Defaultis
如果上面的这些命令都能执行正常的话就表示 logstash可以使用了,但要让他启动还需要一个配置文件
input {
redis {
host => '192.168.233.130'
data_type => 'list'
port => "6379"
key => 'logstash:redis'
type => 'redis-input'
}
}
output {
elasticsearch {
host => '192.168.233.131'
port => "9300"
}
}



解释一下 logstash的配置文件由 input filter output 等几个基本的部分组成,顾名思义 input 就是在那收集数据,output就是输出到哪,filter代表一个过滤规则意思是什么内容 会被收集。 上面这段是让 logstash 去192.168.233.130 这个redis服务器上去收集日志 redis端口为6379,key是 logstash:redis 类型为 redis-input ,(注意:这几个值必须跟logstash agent的 output 所对应),收集完成后输出到 elasticsearch ,embedded => true 的意思是使用logstash 内嵌的 elasticsearch。如果有独立的elasticsearch服务器,需要将 这条改为 host => 'elasticsearch的ip' port => 端口



安装redis



下载安装就比较简单了




下载安装就比较简单了




配置文件里的那几个路径要提前建好 mkdir -p /data/redis  cd /data/redis/  mkdir {db,log,etc}
  1. [192.168.233.130 root@nodea:/soft]
  2. # wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz
  3. [192.168.233.130 root@nodea:/data/redis/etc]
  4. # make –j24
  5. [192.168.233.130 root@nodea:/data/redis/etc]
  6. # make install








  1. 我用的是最简单的 配置,


  2. [192.168.233.130 root@nodea:/data/redis/etc]


  3. # vim redis.conf


  4. #this is the config file for


  5. pidfile /var/run/redis.pid


  6. port 6379


  7. timeout 0


  8. loglevel verbose


  9. logfile /data/redis/log/redis.log


  10. dbfilename dump.rdb


  11. dir /data/redis/db/


  12. vm-swap-file /tmp/redis.swap


  13. activerehashing yes


  14. 启动命令如下


  15. [192.168.233.130 root@nodea:/data/redis/etc]


  16. # redis-server /data/redis/etc/redis.conf &
看看 redis服务的状态




  1. [192.168.233.130 root@nodea:/data/redis/etc]
  2. # lsof -i:6379
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. redis-ser 2732 root 4u IPv4 7946 TCP *:6379 (LISTEN)
  5. redis-ser 2732 root 5u IPv4 7963 TCP localhost.localdomain:6379->localhost.localdomain:19214 (ESTABLISHED)
  6. java 2733 root 9u IPv4 7959 TCP localhost.localdomain:19214->localhost.localdomain:6379 (ESTABLISHED)
安装elasticsearch
elasticsearch会依赖于JAVA_HOME环境变量,所以需要设置JAVA_HOME环境变量。首先查看你的Java的安装路径



wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.18.7.zip



unzip elasticsearch-0.18.7.zip
mv elasticsearch-0.18.7 /usr/local/share/elasticsearch




cd /usr/local/share/elasticsearch/bin/



./elasticsearch -f



 



好了,可以让logstash index 开始启动了





好了,看到9292 端口启动就代表 启动成功了,检查一下




现在可以通过浏览器访问一下 http://192.168.233.128:9292 看看logstash是的页面是个什么样子






现在还不能搜索因为现在还没有数据,其实这个时候 http://192.168.233.128:9200 也是可以访问的,
很多开发自己写代码来调用elasticsearch 来实现他们自己的需要,这里就不多说了。
  1. [192.168.233.128 root@nodec:/soft]
  2. # lsof -i:9292
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. java 5205 root 465u IPv4 130805 TCP *:armtechdaemon (LISTEN)
  5. 其实logstash还启动了一个端口9200,因为启动了内嵌的 elasticsearch,这个9200是 elasticsearch在监听
  6. [192.168.233.128 root@nodec:/soft]
  7. # lsof -i:9200
  8. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  9. java 5205 root 160u IPv4 130682 TCP *:wap-wsp (LISTEN)
  10. [192.168.233.128 root@nodec:/soft]
  11. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf -- web &
  12. [1] 5205
  13. ...这里要等待约5秒钟... 为什么?去问开发者吧
  14. [192.168.233.128 root@nodec:/soft]
  15. # I, [2013-03-19T03:23:10.749000 #5205] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T03:23:10.732000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  16. file:/soft/logstash-1.1.0-monolithic.jar!/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53 warning: already initialized constant WFKV_
  17. Mizuno 0.5.0 (Jetty 8.0.y.z-SNAPSHOT) listening on
  18. 解释一下 上面的命令 agent 代理模式 -f 指定配置文件 --web 其实是个分隔符等于又启动了一个命令,后面的参数就是开启一个web页面默认端口是9292,这个命令如果拆成两个就是这个样子
  19. java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
  20. java -jar /soft/logstash-1.1.0-monolithic.jar web &



配置logstash的agent
登录到 服务器 192.168.233.129 安装基本软件包和logstash



安装sun-java6-jre sun-java6-jdk




  1. [192.168.233.129 root@noded:/soft]
  2. # atp-get install ruby
  3. 192.168.233.129 root@noded:/soft]
  4. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
  5. [192.168.233.129 root@noded:/soft]
  6. # vim redis.conf
  7. input {
  8. file {
  9. "producer"
  10. "/soft/apache.log"
  11. }
  12. file {
  13. "php-log"
  14. "/soft/php.log"
  15. }
  16. }
  17. filter {
  18. grep {
  19. "@message", "mysql|GET|error"
  20. }
  21. }
  22. output
  23. redis {
  24. '192.168.233.130'
  25. 'list'
  26. key => 'logstash:redis'
  27. }
  28. }

大概说一下这个配置文件 input 里的file就是要监视的文件了 这里我监视了两个文件,如果这两个文件有追加的内容就会通过下面的output设置发给 redis服务器
filter 里的grep 意思就是 grep... 后面这段就是日志内容里面只要有匹配 mysql或GET或error的内容就会被过滤出来,发送到 logstash index
以上就是一个比较简单的配置文件了,让我们启动他




  1. [192.168.233.129 root@noded:/soft]
  2. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
  3. I, [2013-03-19T19:45:35.762000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.752000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  4. I, [2013-03-19T19:45:35.778000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.778000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  5. I, [2013-03-19T19:45:35.804000 #2721] INFO -- : Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.803000 -0700","message":"Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  6. I, [2013-03-19T19:45:35.854000 #2721] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.853000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}

只要没有 warning 和 error就算是正常启动了
启动之前请确定 192.168.233.130的 redis服务器已经启动,不然会报错。

 

最后我们回到 logstash agent 上面测试一下




  1. [192.168.233.129 root@noded:/soft]
  2. # echo GET12313 >> apache.log
  3. [192.168.233.129 root@noded:/soft]
  4. # echo errorabcd >> apache.log

ok 到 http://192.168.233.128:9292 去搜索一下 刚才的两个内容

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统_elasticsearch_02

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统_elasticsearch_03

​​嗯,就是这样了,我现在找个php的错误日志给他追加到php.log文件里
[192.168.233.129 root@noded:/soft]
# cat php-error.log >> php.log
在看看 logstash的页面 搜索一下 error

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统_redis_04

OK,最后就是 Kibana了 ,我把Kibana装在了 logstash index上面
下载地址为 http://kibana.org/intro.html




  1. [192.168.233.128 root@nodec:/soft]
  2. # tar xf Kibana-0.2.0.tar.gz
  3. [192.168.233.128 root@nodec:/soft]
  4. # cd Kibana-0.2.0
  5. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  6. # bundle install
  7. ( /var/lib/gems/1.8/bin/bundle install )
  8. 直接安装就好了,非常简单,因为之前咱们已经安装好了 bundle
  9. 编辑配置文件,指定 elasticsearch 的位置
  10. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  11. # vim KibanaConfig.rb
  12. .....
  13. "192.168.18.131:9200"
  14. KibanaPort = 5601
  15. '0.0.0.0'
  16. .....
  17. 主要是这几个参数
  18. 启动的话需要ruby
  19. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  20. # /usr/bin/ruby kibana.rb &
  21. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  22. # == Sinatra/1.3.5 has taken the stage on 5601 for development with backup from
  23. >> Thin web server (v1.5.0 codename Knife)
  24. >> Maximum connections setto
  25. >> Listening on 0.0.0.0:5601, CTRL+C to
  26. 如果ruby的东西都不缺的话,启动会很顺利,ok 现在看看5601端口的状态
  27. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  28. # lsof -i:5601
  29. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  30. ruby 3116 root 5u IPv4 28947 TCP *:esmagent (LISTEN)

访问一下 试试看 http://192.168.233.128:5601 尝试搜索一下php的错误日志,比如mysql

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统_jar_05

呵呵,要的就是这个效果,日志会实时的汇总到 logstash index 上供我们查询,当然这只是开始使用logstash的第一步而


标签:plugin,root,redis,192.168,logstash,Elasticsearch,Kibana,soft,Logstash
From: https://blog.51cto.com/u_15785444/5945783

相关文章

  • Linux 安装 Elasticsearch+kibana
    参考:Elasticsearch详解及部署https://www.cnblogs.com/cjzzz/p/16127324.html下载:https://www.elastic.co/cn/downloads/elasticsearch   或 https://www.elastic......
  • ElasticSearch的基本用法与集群搭建 good
    一、简介ElasticSearch和Solr都是基于Lucene的搜索引擎,不过ElasticSearch天生支持分布式,而Solr是4.0版本后的SolrCloud才是分布式版本,Solr的分布式支持需要ZooKeeper的支持......
  • 安装Elasticsearch和插件
    说明:es7版本以后,软件包的有带和不带jdk版本,都需要配置环境变量,有jdk版本路径:/usr/share/elasticsearch/jdk无jdk版本:需要配置环境变量。1.安装JDKopenjdk安......
  • Elasticsearch 入门实战(7)--Data Stream
    数据量(DataStream) 是在 Elasticsearch 7.9版推出的一项功能,它可以很方便的处理时间序列数据。1、简介1.1、什么是TimeSeriesDataTSD始终与时间戳关联,该时间戳标......
  • 【Elasticsearch】聚合分析
    聚合分析什么是聚合分析聚合分析,英文为Aggregation,是es除搜索功能外提供的针对es数据做统计分析的功能ES提供多种分析方式:Bucket、Metric、Pipeline、Matrix等 Bucke......
  • Elasticsearch+Logstash+Kiabana 日志管理
       日志是分析线上问题的重要手段,通常我们会把日志输出到控制台或者本地文件中,排查问题时通过根据关键字搜索本地日志,但越来越多的公司,项目开发中采用分布式的架构,日......
  • Elasticsearch调优
    Elasticsearch调优在线刷题小程序微信搜索:傲浮刷题,或者扫描文章底部二维码即可食用前语随着数据量的增长,MySQL在很多场景下,似乎已经无能为力了,所以有不少应用的数据从......
  • ElasticSearch原理篇
    一、开篇几个问题 1、大规模数据如何检索?当系统数据量上了10亿、100亿条的时候,我们在做系统架构的时候通常会从以下角度去考虑:1)用什么数据库好?(MySQL、sybase、Oracle、达......
  • 【安装】Linux安装Elasticsearch教程
    Elastic官网​​开源搜索:Elasticsearch、ELKStack和Kibana的开发者|Elastic​​Elasticsearch(官网:​​https://www.elastic.co/cn/products/elasticsearch​​ )需要......
  • 谷歌浏览器安装elasticsearch-head插件
    之前在使用es的时候有插入索引和数据的需求但是对于es操作不适太熟悉,然后就用docker安装了一个es-head进行操作,但是用docker安装的es-head有时候不是太好用(问题后续标注)。......