一、需求及基础:

场景:

1、开发人员不能登录线上服务器查看详细日志

2、各个系统都有日志,日志数据分散难以查找

3、日志数据量大,查询速度慢,或者数据不够实时

4、一个调用会涉及到多个系统,难以在这些协调中快速定位数据

二、ELS的概念:

elasticsearch:搜索引擎,提供索引,搜索功能

Logstash:接收,处理,转发日志

Kibana:独立的、美观的图形数据web界面

三、安装及配置

1、 elasticsearch安装

安装java环境,1.8.20或以上的版本

  1. 1、安装Java环境
  2. tar xf jdk-8u201-linux-x64.tar.gz
  3. vim /etc/profile
  4. export JAVA_HOME=/root/jdk
  5. export PATH=$JAVA_HOME/bin:$PATH
  6. source /etc/profile
  7.  
  8. 2、安装elasticsearch
  9. wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.0.tar.gz
  10. tar xf elasticsearch-1.7.0.tar.gz
  11. ln -s elasticsearch-1.7.0 /usr/local/elasticsearch
  12.  
  13. 3、修改配置文件
  14. grep -n '^[a-z]' /usr/local/elasticsearch/config/elasticsearch.yml
  15. 32:cluster.name: elasticsearch #必须修改
  16. 40:node.name: "stu02" #必须修改
  17. 47:node.master: true
  18. 51:node.data: true
  19. 107:index.number_of_shards: 5
  20. 111:index.number_of_replicas: 1
  21. 145:path.conf: /usr/local/elasticsearch/config
  22. 149:path.data: /usr/local/elasticsearch/data
  23. 159:path.work: /usr/local/elasticsearch/work
  24. 163:path.logs: /usr/local/elasticsearch/logs
  25. 167:path.plugins: /usr/local/elasticsearch/plugins
  26. 184:bootstrap.mlockall: true
  27.  
  28. 4、启动
  29. /usr/local/elasticsearch/bin/elasticsearch -d #(可加入参数,-Xms512m -Xmx512m)
  30. netstat -lnt | egrep '9200|9300'
  31. jps -lvm

测试:

  1. curl 127.0.0.1:9200
  2. curl -i -XGET http://192.168.4.16:9200

安装ELS监控管理插件

  1. 1、离线安装插件
  2. wget http://download.elasticsearch.org/elasticsearch/marvel/marvel-latest.zip
  3. /usr/local/elasticsearch/bin/plugin -i marvel -u file:///tmp/marvel-latest.zip
  4. 2、在线安装插件
  5. /usr/local/elasticsearch/bin/plugin -i elasticsearch/marvel/latest/
  6. /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head
  7.  
  8. 3、卸载插件
  9. /usr/local/elasticsearch/bin/plugin -r marvel

生产可能需要配置已下:

  1. max_file_descriptors: 64000
  2. /etc/sysctl.conf
  3. sysctl -w vm_max_count=262144

2、 logstash安装

  1. 1、安装Java环境
  2. tar xf jdk-8u201-linux-x64.tar.gz
  3. vim /etc/profile
  4. export JAVA_HOME=/root/jdk
  5. export PATH=$JAVA_HOME/bin:$PATH
  6. source /etc/profile
  7.  
  8. 2、安装logstash
  9. wget https://download.elastic.co/logstash/logstash/logstash-1.5.3.tar.gz
  10. tar xf logstash-1.5.3.tar.gz
  11. ln -s /root/logstash-1.5.3/ /usr/local/logstash

标准输入和标准输出

  1. /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'

使用ruby进行更详细的输出

  1. /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{codec => rubydebug}}'

输出到elasticsearch

  1. /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch
    { host => "192.168.4.16" protocol => "http"} }'

读取日志并输出到/tmp下

  1. vim   /etc/logstash/conf.d/logstash.conf
    input {
  2. file {
  3. path => "/tmp/messages"
  4. }
  5. }
  6.  
  7. output {
  8. file {
  9. path => "/tmp/log-%{+YYYY-MM-dd}messages.gz"
  10. gzip => true
  11. }
  12. }

测试配置文件

  1. /usr/local/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf -t

启动服务

  1. /usr/local/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf

测试

  1. cat /var/log/messages >> /tmp/messages
    ll /tmp/log-2019-02-11messages.gz

把输出直接传输到elasticsearch

  1. vim /etc/logstash/conf.d/logstash.conf
  2. input {
  3. file {
  4. path => "/tmp/messages"
  5. }
  6. }
  7.  
  8. output {
  9. file {
  10. path => "/tmp/log-%{+YYYY-MM-dd}.messages.gz"
  11. gzip => true
  12. }
  13.  
  14. elasticsearch {
  15. host => ["192.168.4.16"]
  16. protocol => "http"
  17. index => "system-message-%{+YYYY.MM.dd}"
  18. }
  19. }

在集群管理平台查看结果

打开浏览器输入:http://192.168.4.16:9200/_plugin/head/

将logstash输出给redis

一台logstash的配置文件

  1. input {
  2. file {
  3. path =>"/tmp/messages"
  4. }
  5. }
  6.  
  7. output {
  8. redis {
  9. data_type => "list"
  10. key => "system-message"
  11. host => "192.168.4.17"
  12. port => "6379"
  13. db => "0"
  14. }
  15. }

另一台logstash的配置文件

  1. input {
  2. redis {
  3. data_type => "list"
  4. key => "system-message"
  5. host => "192.168.4.17"
  6. port => "6379"
  7. db => "0"
  8. }
  9. }
  10.  
  11. output {
  12. elasticsearch {
  13. host => ["192.168.4.16"]
  14. protocol => "http"
  15. index => "redis-message-%{+YYYY.MM.dd}"
  16. }
  17. }

logstash日志json格式

安装nginx,将nginx的日志设置为json模式

  1. log_format logstash_json '{"@timestamp":"$time_iso8601",' #定义日志格式logstash_json
  2. '"host":"$server_addr",'
  3. '"clientip":"$remote_addr",'
  4. '"size":$body_bytes_sent,'
  5. '"responsetime":$request_time,'
  6. '"upstreamtime":"$upstream_response_time",'
  7. '"upstreamhost":"$upstream_addr",'
  8. '"http_host":"$host",'
  9. '"url":"$uri",'
  10. '"domain":"$host",'
  11. '"xff":"$http_x_forwarded_for",'
  12. '"referer":"$http_referer",'
  13. '"agent":"$http_user_agent",'
  14. '"status":"$status"}';
  15.  
  16. access_log /var/log/nginx/json.access.log logstash_json; #日志文件保存路径及使用上面定义的日志格式logstash_json

配置logstash将nginx的json日志并写入到redis

一台配置

  1. input {
  2. file {
  3. path => "/var/log/nginx/json.access.log" #读取指定的json格式的日志
  4. codec => "json" #指定json格式
  5. }
  6. }

  7. output {
  8. redis {
  9. data_type => "list"
  10. key => "nginx-json-log" #nginx的json格式日志的key名称
  11. host => "192.168.4.17"
  12. port => "6379"
  13. db => "1"
  14. }
  15. }

另一台

  1. input {
  2. redis {
  3. data_type => "list"
  4. key => "nginx-json-log"
  5. host => "192.168.4.17"
  6. port => "6379"
  7. db => "1"
  8. }
  9. }
  10.  
  11. output {
  12. elasticsearch {
  13. host => ["192.168.4.16"]
  14. protocol => "http"
  15. index => "nginx-json-log-%{+YYYY.MM.dd}"
  16. }
  17. }

使用ab访问nginx测试日志

  1. ab -n1000 -c10 http://192.168.4.16:81 #一共1000个请求,每次并发10个,即100次请求完成

打开浏览器输入:http://192.168.4.16:9200/_plugin/head/

3、kibana安装

  1. 1、安装
  2. wget https://download.elastic.co/kibana/kibana/kibana-4.1.1-linux-x64.tar.gz
  3. tar xf kibana-4.1.1-linux-x64.tar.gz
  4. ln -s /root/kibana-4.1.1-linux-x64/ /usr/local/kibana
  5.  
  6. 2、修改配置
  7. vim /usr/local/kibana/config/kibana.yml
  8. elasticsearch_url: "http://192.168.4.16:9200"
  9. pid_file: /var/run/kibana.pid
  10. log_file: /usr/local/kibana/kibana.log
  11.  
  12. 3、启动
  13. /usr/local/kibana/bin/kibana #直接启动
  14. nohup /usr/local/kibana/bin/kibana & #后台启动

访问测试:默认监听端口5601

打开浏览器输入:http://192.168.4.16:5601

配置索引:索引的名称要和logstash的output生成的索引能进行匹配才可以

查看数据:默认显示最新的500个文档

数据精确搜索:

搜索高级语法:

  1. status:404 OR status:500 #搜索状态是404或者是500之一的
  2. status:301 AND status:200 #搜索即是301和200同时匹配的
  3. status:[200 TO 300] #搜索指定范围的

其他的常用模块:

1、系统日志收集---> syslog:配置syslog结果写入到elasticsearch,指定端口514,主机就是要收集日志的服务器IP地址,即可使用

2、访问日志:nginx转换成json格式

3、错误日志:使用codec插件:

  1. https://www.elastic.co/guide/en/logstash/1.5/codec-plugins.html
  2.  
  3. input {
  4. stdin {
  5. codec => multiline { #多行日志,比如java的日志
  6. pattern => "^\s" #pattern => ".*\t.*" #找到换行符,会把多行认为是一行,即会把当前行和上一行合成一行,直到有换行符结束
  7. what => "previous"
  8. }
  9. }
  10. }

4、运行日志 codec => json,如果不是json要使用grok进行匹配,相对比较麻烦,如果丢日志就看logstash.log,另外检查日志是否有效的json格式:

json效验地址:http://www.bejson.com/

5、kibana的时区和时间问题:kibana会自动根据浏览器将时间加8小时,通过logstash写入会自动解决,如果通过python脚本等写入会产生时间问题

6、在地图显示IP具体来源地址:

  1. https://www.elastic.co/guide/en/logstash/1.5/filter-plugins.html

7、条件判断:

  1. input {
  2.   file {
  3.     type => "apache"
  4.     path => "/var/log/apache.log"
  5.   }
  6.   file {
  7.     type => "tomcat"
  8.     path => "/var/log/tomcat.log"
  9.   }
  10. }
  11.  
  12. filter {
  13. if [type] == "apache" { #假如索引为apache,就执行以下操作
  14. redis {
  15. data_type => "list"
  16. key => "system-message-jack"
  17. host => "192.168.10.205"
  18. port => "6379"
  19. db => "0"
  20. }
  21. if [type] == "tomcat" { #假如索引为tomcat,就执行一次操作
  22. redis {
  23. data_type => "list"
  24. key => "system-message-tomcat"
  25. host => "192.168.10.205"
  26. port => "6379"
  27. db => "1" #写不同的数据库
  28. }
  29. }

测试logstash配置文件语法是否正确

  1. /etc/init.d/logstash configtest

8、将tomcat日志的格式定义为json的格式:

  1. directory="logs" prefix="localhost_access_log." suffix=".log"
  2. pattern="{"client":"%h", "client user":"%l",
    "authenticated":"%u", "access time":"%t",
    "method":"%r", "status":"%s",
    "send bytes":"%b", "Query?string":"%q",
    "partner":"%{Referer}i", "Agent version":"%{User-Agent}i"}"/>

取到的日志结果为:

  1. {"client":"180.95.129.206", "client user":"-", "authenticated":"-", "access time":"[20/Apr/2016:03:47:40 +0000]", "method":"GET /image/android_logo.png HTTP/1.1", "status":"200", "send bytes":"1915", "Query string":"", "partner":"http://mobile.weathercn.com/index.do?id=101160101&partner=1000001003", "Agent version":"Mozilla/5.0 (Linux; U; Android 5.1.1; zh-cn; NX510J Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/6.6 Mobile Safari/537.36"}

学习ELK日志平台(一)的更多相关文章

  1. 学习ELK日志平台(二)

      一.ELK介绍 1.1 elasticsearch 1.1.1 elasticsearch介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索 ...

  2. 学习ELK日志平台(四)

    一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...

  3. 学习ELK日志平台(三)

    ELK(elasticsearch.logstash.kibana) Elastic Stack是原ELK Stack在5.0版本加入Beats套件后的新称呼 解决痛点: 开发人员不能登录线上serv ...

  4. 学习ELK日志平台(五)

    ELK Stack 通常情况下: 1,开发人员是不能登录线上服务器查看日志信息 2,各个系统的日志繁多,日志数据分散难以查找 3,日志数据量较大,查询速度慢,数据不够实时性 4,一个调用会涉及到多个系 ...

  5. ELK 日志平台构建

    elastic中文社区 https://elasticsearch.cn/ 完整参考 ELK实时日志分析平台环境部署--完整记录 https://www.cnblogs.com/kevingrace/ ...

  6. elk日志平台搭建小记

    最近抽出点时间,搭建了新版本的elk日志平台 elastaicsearch 和logstash,kibana和filebeat都是5.6版本的 中间使用redis做缓存,版本为3.2 使用的系统为ce ...

  7. Springboot项目使用aop切面保存详细日志到ELK日志平台

    上一篇讲过了将Springboot项目中logback日志插入到ELK日志平台,它只是个示例.这一篇来看一下实际使用中,我们应该怎样通过aop切面,拦截所有请求日志插入到ELK日志系统.同时,由于往往 ...

  8. Springboot项目搭配ELK日志平台

    上一篇讲过了elasticsearch和kibana的可视化组合查询,这一篇就来看看大名鼎鼎的ELK日志平台是如何搞定的. elasticsearch负责数据的存储和检索,kibana提供图形界面便于 ...

  9. 亿级 ELK 日志平台构建部署实践

    本篇主要讲工作中的真实经历,我们怎么打造亿级日志平台,同时手把手教大家建立起这样一套亿级 ELK 系统.日志平台具体发展历程可以参考上篇 「从 ELK 到 EFK 演进」 废话不多说,老司机们座好了, ...

随机推荐

  1. 2021年国内外五大BI厂商_商业智能工具推荐

    ​每家公司对于BI工具的使用都有自己的特点和期望,所以当面对国内外那么多的BI厂商时,大家可能会觉得难以选择.今天我将会为大家介绍国内外五大BI厂商,对于它们做出一些分析,让大家更了解这些BI厂商. ...

  2. Hadoop2.7.2源码编译过程

    目录 准备工作 jar包安装 源码编译 准备工作 CentOS可以联网,验证:ping www.baidu.com 是畅通的 jar 包准备(hadoop 源码.JDK8.maven.ant .pro ...

  3. docker安装以及常用命令

    yum-config-manager 对/etc/yum.repos.d/下的.repo文件进行增删改查 yum install -y yum-utils 添加docker源,由于yum源的docke ...

  4. 浏览器无插件播放rtsp流解决方案

    1. 安装 FFmpeg 参考 CentOS下安装FFmpeg,特别详细. 我遇到的错误和解决办法: 缺少lame ffmpeg+libmp3lame库源码安装教程(CentOS) make ffmp ...

  5. selenium+python安装

    整理了下selenium+python环境搭建,搭建了很多次但每次都还是手忙脚乱,今天用心整理下 selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架,并且 Selenium ...

  6. Caffe2源码解析

    写在前面 上一篇文章对Caffe2中的core模块进行了简单拆解Caffe2源码解析之core,本篇给出其它模块的拆解,目的是大致了解每个模块的内容和目标,进一步理解Caffe2的整体框架.内容不多, ...

  7. 初探 Elasticsearch,学习笔记第一讲

          1. ES 基础   1.1 ES定义   ES=elaticsearch简写, Elasticsearch是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储.检索数据:本身扩展 ...

  8. Spring和MyBatis框架整合

    Spring集成MyBatis 使用MyBatis,需要创建MyBatis框架中的某些对象,使用这些对象,就能使用mybatis提供的功能了. 需要有Dao接口的代理对象,例如StudentDao接口 ...

  9. ActiveMQ 笔记—01

  10. 一致性 hash 环

    一致性 hash 环 最近做项目 做了一个分发器 ,需要 根据请求携带的参数 把请求分发到 不同的服务器上面,最终我选择使用 一致性hash 环 来实现 ,本篇 就主要讲解一下 一致性hash环 它的 ...