ELK日志分析平台

ELK Stack(ElasticSearch, Logstash, Kibana)

复制代码

Elasticsearch(端口9200)
  是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,文档,节点,集群,restful风格接口,多数据源,自动搜索负载等。
Logstash
  是一个完全开源的工具,他可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
Kibana 
  也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

ELK官网:https://www.elastic.co/
ELK官网文档:https://www.elastic.co/guide/index.html
ELK中文指南:https://kibana.logstash.es/content/
Elasticsearch 管理:https://www.elastic.co/guide/en/elasticsearch/guide/current/administration.html

平台实现架构如下

复制代码

 

ElasticSearch简介

复制代码

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

我们建立一个网站或应用程序,并要添加搜索功能,令我们受打击的是:搜索工作是很难的。
我们希望我们的搜索解决方案要快
我们希望有一个零配置和一个完全免费的搜索模式
我们希望能够简单地使用JSON通过HTTP的索引数据
我们希望我们的搜索服务器始终可用
我们希望能够一台开始并扩展到数百
我们要实时搜索,我们要简单的多租户
我们希望建立一个云的解决方案。
Elasticsearch旨在解决所有这些问题和更多的问题。

复制代码

ElasticSearch(ES)概念

复制代码

cluster
  一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。
  ES的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看ES集群,在逻辑上是个整体,你与任何一个节点的通信和与整个ES集群通信是等价的。
shards
  索引分片,ES可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。
replicas
  索引副本,ES可以设置多个索引的副本,副本的作用一是提高系统的容错性,当某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高ES的查询效率,ES会自动对搜索请求进行负载均衡。
recovery
  数据恢复或叫数据重新分布,ES在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。
river
  ES的一个数据源,也是其它存储方式(如:数据库)同步数据到ES的一个方法。它是以插件方式存在的一个ES服务,通过读取river中的数据并把它索引到ES中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。
gateway
  ES索引快照的存储方式,ES默认是先把索引存放到内存中,当内存满了时再持久化到本地硬盘。gateway对索引快照进行存储,当这个ES集群关闭再重新启动时就会从gateway中读取索引备份数据。
  ES支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和amazon的s3云存储服务。
discovery.zen
  ES的自动发现节点机制,ES是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。
Transport
  ES内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。

复制代码

 

ELK日志分析平台搭建

192.168.0.90nginx servernginx/java1.8/logstash-1.5.3
192.168.0.91redis serverredis/java1.8/logstash-1.5.3
192.168.0.92ELKelasticsearch-1.7.2/kibana-4.1.1/logstash-1.5.3
192.168.0.93ELKelasticsearch-1.7.2/kibana-4.1.1/logstash-1.5.3

 

 

 

 

安装ElasticSearch

复制代码

安装配置java环境依赖
#rpm -ivh jdk-8u111-linux-x64.rpm 
#echo "export JAVA_HOME=/usr/java/latest" >> /etc/profile
#echo "export PATH=$JAVA_HOME/bin:$PATH" >>/etc/profile
#source /etc/profile

安装配置ElasticSearch
#tar -zxf elasticsearch-1.7.2.tar.gz -C /usr/local/
#ln -s /usr/local/elasticsearch-1.7.2/ /usr/local/elasticsearch
  /usr/local/elasticsearch/bin/elasticsearch  #启动脚本
  /usr/local/elasticsearch/config/elasticsearch.yml #配置文件 

根据如下内容,修改es配置文件
# grep ^[a-z] /usr/local/elasticsearch-1.7.2/config/elasticsearch.yml 
cluster.name: gtms-cluster    es cluster名称(cluster间通过组播通信)
node.name: "elk92"               es节点名称【另外一台elk93,除了此项,两台es其他配置相同】
node.master: true          是否允许选举master
node.data: true           是否允许存储数据
index.number_of_shards: 5      索引分片数
index.number_of_replicas: 1     索引副本数
path.conf: /usr/local/elasticsearch/config/     es配置文件路径
path.data: /elk/data                        数据目录
path.work: /elk/work                        工作目录
path.logs: /elk/logs                        log目录
path.plugins: /elk/plugins                   plug目录
bootstrap.mlockall: true                    锁住内存
#mkdir -p /elk/{data,work,logs,plugins}

启动es
# /usr/local/elasticsearch/bin/elasticsearch (默认前台执行)
# curl http://192.168.0.92:9200 (验证成功)
{
  "status" : 200,
  "name" : "elk92",
  "cluster_name" : "gtms-es-cluster",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

为方便管理,安装起停命令脚本,及加入chkconfig
#git clone https://github.com/elasticsearch/elasticsearch-servicewrapper.git
#mv elasticsearch-servicewrapper/service/ /usr/local/elasticsearch/bin
#/usr/local/elasticsearch/bin/service/elasticsearch install
#chkconfig --list elasticsearch
elasticsearch   0:off   1:off   2:on    3:on    4:on    5:on    6:off
#/etc/init.d/elasticsearch  
Usage: /etc/init.d/elasticsearch [ console | start | stop | restart | condrestart | status | install | remove | dump ]
Commands:
  console      Launch in the current console.
  start        Start in the background as a daemon process.
  stop         Stop if running as a daemon or in another console.
  restart      Stop if running and then start.
  condrestart  Restart only if already running.
  status       Query the current status.
  install      Install to start automatically when system boots.
  remove       Uninstall.
  dump         Request a Java thread dump if running.

# vi /usr/local/elasticsearch/bin/service/elasticsearch.conf
set.default.ES_HEAP_SIZE=512   默认1024M,根据实际情况调低内存,解决内存容量小导致服务无法启动


复制代码

 

ElasticSearch交互管理

复制代码

ELK API
    Java API
        node client
        Transport client
    RESTful API
        Javascript  .NET PHP Perl Python Ruby curl


通过curl方式
curl -X<VERB> '<PROTOCOL>://<HOST>/<PATH>?<QUERY_STRING>' -d '<BODY>'
  VERB HTTP方法     GET/POST/PUT/HEAD/DELETE
  PROTOCOL    http/https(es前有代理)
  QUERY_STRING    可选的请求参数,例如?pretty参数将使请求返回更加美观易读的JSON数据
  BODY    一个JSON格式的请求主体    
# curl -XGET 'http://192.168.0.92:9200/_count?pretty' -d ' {"query":"match_all":{}}}'
{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}

 # curl -XGET http://192.168.0.92:9200/_cluster/health?pretty     ==>通过此方式可以实现cluster监控

单台时启动另一台ES后
{
  "cluster_name" : "gtms-es-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
}
{
  "cluster_name" : "gtms-es-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
}











elasticsearch集群管理插件head(如果集群大了管理有问题)
head插件安装
# /usr/local/elasticsearch/bin/plugin -i mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip...
Downloading .......................................................
...................................................................
.........DONE
Installed mobz/elasticsearch-head into /elk/plugins/head


浏览器http://192.168.0.92:9200/_plugin/head

 

复制代码

 

marvel插件(开发用免费)

复制代码

marvel插件(开发用免费)
marvel 是 Elastic.co 公司推出的商业监控方案,也是用来监控 Elasticsearch 集群实时、历史状态的有力用具,便于性能优化以及故障诊断。
监控主要分为六个层面,分别是集群层、节点层、索引层、分片层、事件层、Sense。
  集群层:主要对集群健康情况进行汇总,包括集群名称、集群状态、节点数量、索引个数、分片数、总数据量、集群版本等信息。同时,对节点、索引整体情况分别展示。
  节点层:主要对每个节点的 CPU 、内存、负载、索引相关的性能数据等信息进行统计,并进行图形化展示。
  索引层:展示的信息与节点层类似,主要从索引的角度展示。
  分片层:从索引、节点两个角度展示分片的分布情况,并提供 playback 功能演示分片分配的历史过程。
  事件层:展示集群相关事件,如节点脱离、加入,Master 选举、索引创建、Shard 分配等。
  Sense:轻量级的开发界面,主要用于通过 API 查询数据,管理集群

安装marvel插件
# /usr/local/elasticsearch/bin/plugin -i elasticsearch/marvel/latest
-> Installing elasticsearch/marvel/latest...
Trying http://download.elasticsearch.org/elasticsearch/marvel/marvel-latest.zip...
Downloading ...........................................................
........................DONE
Installed elasticsearch/marvel/latest into /elk/plugins/marvel


http://192.168.0.92:9200/_plugin/marvel
右上角dashboards--->sence

复制代码

复制代码

接下来通过marvel对es做一些操作,新建一个索引及一条记录

post /index-demo/test
{
  "user":"gtms",
  "messages":"hello world!"
}
{
   "_index": "index-demo",
   "_type": "test",
   "_id": "AVsq51umX6MYL3f6wZ0-",
   "_version": 1,
   "created": true
}
GET /index-demo/test/AVsq51umX6MYL3f6wZ0-{
   "_index": "index-demo",
   "_type": "test",
   "_id": "AVsq51umX6MYL3f6wZ0-",
   "_version": 1,
   "found": true,
   "_source": {
     

   }
}
GET /index-demo/test/AVsq51umX6MYL3f6wZ0-/_source{
   "user": "gtms",
   "messages": "hello world!"
}
GET /index-demo/test/_search?q=hello{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.15342641,
      "hits": [
         {
            "_index": "index-demo",
            "_type": "test",
            "_id": "AVsq51umX6MYL3f6wZ0-",
            "_score": 0.15342641,
            "_source": {
               "user": "gtms",
               "messages": "hello world!"
            }
         }
      ]
   }
}
































经过上述操作后,再来看下,es上的索引及分片情况

 

复制代码

 

 

logstash(需java环境)

复制代码

需要在每台需要收集节点上安装(即此测试在nginx server、redis server(yum 安装logstash)上安装)

logstash yum安装(生产yum就可以了)
#rpm --import https://packages.elasticsearch.org/GPG-KEY-elasticsearch
#vi /etc/yum.repos.d/logstash.repo
[logstash-1.5]
name=Elastic repository for 1.5.x packages
baseurl= https://packages.elasticsearch.org/logstash/1.5/centos
gpgcheck=1
gpgkey= https://packages.elasticsearch.org/GPG-KEY-elasticsearch 
enabled=1
autorefresh=1
type=rpm-md

logstash 源码解包安装(本次测试使用此种)
#tar -zxvf logstash-1.5.3.tar.gz -C /usr/local/
#ln -s /usr/local/logstash-1.5.3/ /usr/local/logstash
#/usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'  等待输入
[# /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{codec => rubydebug} }']
test hello
2017-02-26T19:33:01.024Z node93 test hello
Logstash startup completed
test again hello
2017-02-26T19:33:28.237Z node93 test again hello

# /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { host=> "192.168.0.92" protocol =>"http" } }'
'[DEPRECATED] use `require 'concurrent'` instead of `require 'concurrent_ruby'`
Logstash startup completed

==>输入hello world后浏览,生成了,logstash-2017.04.02的索引

复制代码

 

 接下来
a、在nginx sevrer上安装nginx服务并配置 log(略)
b、在redis sevrer上安装redis并开启服务(略)

 

配置logstash收集nginx,发送给ES

复制代码

The settings you can configure vary according to the plugin type. For information about each plugin, see Input Plugins, Output Plugins, Filter Plugins, and Codec Plugins.
    https://www.elastic.co/guide/en/logstash/1.5/input-plugins.html
    https://www.elastic.co/guide/en/logstash/1.5/output-plugins.html
    https://www.elastic.co/guide/en/logstash/1.5/filter-plugins.html
    https://www.elastic.co/guide/en/logstash/1.5/codec-plugins.html

创建/etc/logstash目录并创建3个logstash文件logstash_www.conf/logstash_bbs.conf/logstash_blog.conf
# cat logstash_www.conf (其他2个相应修改)
input{
  file {
        path => "/usr/local/nginx/logs/www_access.log" 
        }
}
output{
  file {
      path => "/tmp/www_access.log"
        }

elasticsearch {
  host => "192.168.0.92"
  protocol => "http"
  index => "node90_www_accesslog-%{+YYYY.MM.dd}"
}
}


# /usr/local/logstash/bin/logstash agent -f /etc/logstash/logstash_www.conf -l /var/log/logstash.log &
# /usr/local/logstash/bin/logstash agent -f /etc/logstash/logstash_blog.conf -l /var/log/logstash.log &
# /usr/local/logstash/bin/logstash agent -f /etc/logstash/logstash_bbs.conf -l /var/log/logstash.log &

分别进行访问10次,使产生accesslog,然后查看es平台(3个索引只有当产生相应日志后才产生)



删除es分片,停止logstash进程后
测试使用包含配置文件方式启动1个logstash进程
# /usr/local/logstash/bin/logstash agent -f /etc/logstash/ -l /etc/logstash/logstash.log &
==>只访问了其中一个站点,es上便产生了node90_www_accesslog-2017.04.02 node90_bolg_accesslog-2017.04.02 node90_bbs_accesslog-2017.04.02 三个索引文件
==>curl 其他站点后,发现3个索引都在进行变动
==>由此推断,此种方式输出方式使用了同一个管道

复制代码

 

配置logstash收集nginx,发送给redis server,并配置es上的logstash到redis server上取日志

复制代码

nginx server及redis配置

# cat logstash_www.conf
input{
  file {
        path => "/usr/local/nginx/logs/www_access.log"
        }
}
output{
  file {
      path => "/tmp/www_access.log"
        }

  redis{
data_type => "list"
key => "www_access"
host =>"192.168.0.91"
port => "6379"
db => "1"
}
}

 

# /usr/local/logstash/bin/logstash agent -f /etc/logstash/logstash_www.conf -l /var/log/logstash.log &

# cat logstash.conf
input{
      redis{
data_type => "list"
key => "www_access"
host =>"192.168.0.91"
port => "6379"
db => "1"
}
}

output{
  file {
      path => "/tmp/messages"
        }
elasticsearch {
  host => "192.168.0.92"
  protocol => "http"
  index => "access_log-%{+YYYY.MM.dd}"
}
}
# service logstash restart

yum安装方式使用单一配置文件启动方式

#/opt/logstash/bin/logstash -f /etc/logstash/conf.d/www.conf -l /var/log/logstash.log &

其他结论

=>同上一个测试原理相同,对于www blog bbs日志,如果需要在es上建立单独的索引

1、nginx server上需建立不同的logstash配置文件并各自起logstash进程

2、在redis server端也需要对应nginx server上不同的logstash配置文件建立对应的logstash配置文件,并各自起进程

#如果在工作中建立不同的配置文件并起多个进程似乎不太合理(所以,需要使用filter,但是日志不能用type字段)

#当然如果对同类型的日志建立一个es索引问题也不大,即www bbs blog配置为一个key,redis上根据此文件建立一个es索引

即按如下配置

 

# cat /etc/logstash/logstash.conf
input{
  file {
path => [ "/usr/local/nginx/logs/www_access.log","/usr/local/nginx/logs/bbs_access.log","/usr/local/nginx/logs/blog_access.log"]
        }
}
output{


  redis
{
data_type => "list"
key => "access"
host =>"192.168.0.91"
port => "6379"
db => "1"
}
}

# cat /etc/logstash/conf.d/logstash.conf
input{
        redis{
data_type => "list"
key => "access"
host =>"192.168.0.91"
port => "6379"
db => "1"
}
}

output{
  file {
      path => "/tmp/messages"
        }
elasticsearch {
  host => "192.168.0.92"
  protocol => "http"
  index => "access-%{+YYYY.MM.dd}"
}
}
 

复制代码

 

 上述配置,传输到es上日志以整条作为message字段,不利于检索,接下来配置nginx www日志json输出格式,将日志记录根据字段分割

复制代码

# vi /usr/local/nginx/conf/nginx.conf   增加如下日志log_format
log_format logstash_json '{ "@timestamp": "$time_iso8601", '  
'"remote_addr": "$remote_addr", '  
'"referer": "$http_referer", '  
'"request": "$request", '  
'"status": $status, '  
'"bytes":$body_bytes_sent, '  
'"agent": "$http_user_agent", '  
'"x_forwarded": "$http_x_forwarded_for", '  
'"upstr_addr": "$upstream_addr",'  
'"upstr_host": "$upstream_http_host",'  
'"ups_resp_time": "$upstream_response_time" }';

# vi /usr/local/nginx/conf/extra/www.conf  将www的access log记录为新建的logstash_json格式
server
{
        listen 80;
        server_name www.gtms.org;
        location / {
                root html/www;
                index index.html;
                }
access_log logs/www_access.log logstash_json;
}

# cat logstash.conf  修改logstash.conf 以json的codec方式输入日志,后重启logstash
input{
  file {
        path => [ "/usr/local/nginx/logs/www_access.log","/usr/local/nginx/logs/bbs_access.log","/usr/local/nginx/logs/blog_access.log"]
codec => "json"  
      }
}
output{


  redis
{
data_type => "list"
key => "access"
host =>"192.168.0.91"
port => "6379"
db => "1"
}
}

接下来进行测试,先删除之前关于nginx的访问日志
先curl www.gtms.org
再curl bbs.gtms.org

可以看到,3条message是未进行json code,而message为空的,在message后面的字段中有根据json code分割的字段
对于进行json codec的我们可以方便的进行检索了,但是es的时间有问题,如果使用kibana就没问题了

复制代码

 

 kibana安装配置

复制代码

#tar -zxf kibana-4.1.1-linux-x64.tar.gz -C /usr/local/
#ln -s /usr/local/kibana-4.1.1-linux-x64/ /usr/local/kibana
# vi /usr/local/kibana/config/kibana.yml
elasticsearch_url: "http://192.168.0.93:9200"
# /usr/local/kibana/bin/kibana  尝试前台登陆,如果没有报错加&后台运行

http://kibanaIP:5601
根据es上的index创建一个index patten


保存后可以对此索引进行检索操作 (筛选条件写法可以用AND OR)
  status:[300 TO 400]
  status:200 AND remote_addr:192.168.0.102

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值