lshboo 发表于 2018-11-2 11:50:27

elkb+redis建立日志收集分析系统

  一、ELKB说明
  elastic提供了一套非常高级的工具ELKB来满足以上这几个需求。ELKB指的是用于日志分析或者说数据分析的四个软件,各自拥有独立的功能又可以组合在一起。先来简单介绍一下这四个软件。
  Elastic Search: 从名称可以看出,Elastic Search 是用来进行搜索的,提供数据以及相应的配置信息(什么字段是什么数据类型,哪些字段可以检索等),然后你就可以自由地使用API搜索你的数据。
  Logstash:。日志文件基本上都是每行一条,每一条里面有各种信息,这个软件的功能是将每条日志解析为各个字段。
  Kibana:提供一套Web界面用来和 Elastic Search 进行交互,这样我们不用使用API来检索数据了,可以直接在 Kibana 中输入关键字,Kibana 会将返回的数据呈现给我们,当然,有很多漂亮的数据可视化图表可供选择。
  Beats:安装在每台需要收集日志的服务器上,将日志发送给Logstash进行处理,所以Beats是一个“搬运工”,将你的日志搬运到日志收集服务器上。 Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh中存放。所以说filebeat就是logstash-agent端的取代产品。且性能优良。
  日志系统架构

开始搭建日志系统
  首先去ELK官网下载相应的压缩包 :https://www.elastic.co/downloads
安装
  这里使用CentOS 7为例来说明怎么装这几个软件。其中ELK只需要安装在进行日志收集分析的服务器(server)上,而Beats是每一台产生日志的机器(client)都需要安装,当然也可能包括日志收集服务器本身。安装之前关闭防火墙,关闭selinux
  1.首先配置 filebeat。
  安装filebeat。只需 yum -y install filebeat
  curl -L -Ohttps://download.elastic.co/beats/filebeat/filebeat-5.0.0-x86_64.rpm
  yum -y installfilebeat-5.0.0-x86_64.rpm
  安装完成后,修改配置文件 /etc/filebeat/filebeat.yml
  内容如下:

[*]  filebeat.prospectors:
[*]  - type: log
[*]  enabled: true       ###启用此配置
[*]  paths:
[*]  - /var/log/nginx/*.log      ###指明读取文件的位置,可指定多个
[*]  - /var/log/tomcat/*.log
[*]  filebeat.config.modules:
[*]  path: ${path.config}/modules.d/*.yml
[*]  reload.enabled: false
[*]  setup.template.settings:
[*]  index.number_of_shards: 3
[*]  setup.kibana:
[*]  output.redis:
[*]  hosts: ["192.168.10.10:6379"]
[*]  key: "nginx-log"
[*]  password: ""   ###如果没有密码可以省略
[*]  db: 0               ###指定redis数据库,默认第一个数据库是0,可以省略
[*]  timeout: 5         ###可以省略
  这样的配置是 从 /var/log/nginx下读取所有以.log结尾的文件。且发送到redis。且redis中以list方式存储。键名为 nginx-log配置完成后。先启动redis,再启动filebeat。
  systemctl enable filebeat
  systemctl start filebeat
  2.安装配置启动redis
  vim/opt/soft/redis-4.0.8/redis.conf
  bind 0.0.0.0
  port 6379
  daemonize yes   ### 开启守护进程模式
  redis采用的是单进程多线程的模式。当redis.conf中选项daemonize设置成yes时,代表开启守护进程模式。在该模式下,redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill该进程。但当daemonize选项设置成no时,当前界面将进入redis的命令行界面,exit强制退出或者关闭连接工具(putty,xshell等)都会导致redis进程退出。
  logfile "/var/log/redis.log"
  redis-server/opt/soft/redis-4.0.8/redis.conf
  3.安装配置启动logstash
  rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  cat   /etc/yum.repos.d/logstash.repo
  
  name=Elastic repository for 6.x packages
  baseurl=https://artifacts.elastic.co/packages/6.x/yum
  gpgcheck=1
  gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
  enabled=1
  autorefresh=1
  type=rpm-md
  EOF
  创建好仓库后使用yum安装
  yum -y install logstash
  systemctl enable logstash
  systemctl start logstash
  chown -R logstash:logstash   /etc/logstash/*
  mkdir/usr/share/logstash/config
  ln -s /etc/logstash/*/usr/share/logstash/config/
  测试
  ./logstash -e "input{stdin{}}output{stdout{codec=>rubydebug}}"
  this is a test !Sending Logstash's logs to /var/log/logstash which is now configured via log4j2.properties
  The stdin plugin is now waiting for input:
  {
  "host" => "test03",
  "@version" => "1",
  "message" => "this is a test !",
  "@timestamp" => 2018-04-10T10:48:34.858Z
  }
  说明正常
  接下来应该让logstash从redis中间键中读取数据。那么配置logstash。配置文件为 /etc/logstash/conf.d/logstash.conf
  vim/etc/logstash/conf.d/logstash.conf
  input {
  redis {
  data_type => "list"
  key => "nginx-log"
  host => "192.168.10.10"
  port => 6379
  }
  }
  output {
  elasticsearch {
  hosts => ["192.168.10.8:9200"]
  manage_template => false
  index => "nginx-log-%{+YYYY.MM.dd}"
  }
  }
  systemctl enable logstash
  systemctl start logstash
  安装elasticsearch
  yum -y install elasticsearch-6.2.3.rpm
  vim/etc/elasticsearch/elasticsearch.yml
  path.data: /var/lib/elasticsearch
  path.logs: /var/log/elasticsearch
  network.host: 192.168.10.8
  http.port: 9200
  systemctl enableelasticsearch
  systemctl startelasticsearch
  安装kibana
  yum -y install kibana-6.2.3-x86_64.rpm
  vim /etc/kibana/kibana.yml
  server.port: 5601
  server.host: "0.0.0.0"
  elasticsearch.url: "http://192.168.10.8:9200"
  systemctl enable kibana
  systemctl start kibana
  kibana汉化方法:https://github.com/anbai-inc/Kibana_Hanization
  git clone https://github.com/anbai-inc/Kibana_Hanization.git
  python main.py /usr/share/kibana/
  汉化之后需要重启服务
  时间久了,日志文件过多占用空间,需要清理,命令如下:
  清理2018年4月12号的日志
  curl -XDELETE 'http://192.168.10.8:9200/nginx-log-2018.04.12'
  清理2018年4月的日志
  curl -XDELETE 'http://192.168.10.8:9200/nginx-log-2018.04.*'
  清除所有索引日志
  curl -XDELETE 'http://192.168.10.8:9200/*'
  如果清理了所有的索引日志,kibana里面没有了索引,无法查看日志,重启logstash服务即可自动生成当天的日志索引。
  可以写到脚本里面并添加计划任务

页: [1]
查看完整版本: elkb+redis建立日志收集分析系统