ELKStack部署

ELKStack简介

 对于日志来说,最常见的需求就是收集、存储、查询、展示,开源社区正好有相对应的开源项目:logstash(收集)、elasticsearch(存储+搜索)、kibana(展示),我们将这三个组合起来的技术称之为ELKStack,所以说ELKStack指的是Elasticsearch、Logstash、Kibana技术栈的结合,一个通用的架构如下图所示:

ELK部署实践

测试环境如下:

linux-node1   192.168.182.170   部署logstash、elasticsearch、kibana、nginx、httpd

linux-node2   192.168.182.171   部署logstash、elasticsearch、kibana、redis

由于这是测试环境,节省宿主机的资源,将logstash、elasticsearch、kibana分别部署在两个设备上。在生产环境中一般将logstash部署在需要收集日志文件的设备上;将elasticsearch部署在存储日志文件的设备上,而且要使elasticsearch至少在2台设备上部署,因为elasticsearch遵循着分布式存储,这样才可以让elasticsearch可在多个部署es设备上存储日志副本;将kibana部署在需要展示es上日志信息的设备上。

这里在linux-node1上部署nginx、httpd是为了下面采集httpd、nginx日志信息测试用。在linux-node2上部署redis是为了实现logstash-->redis-->logstash-->elasticsearch收集日志信息的解耦合,不过在生产环境中收集日志的方式为:logstash-->redis-->python-->elasticsearch

ELK安装部署

在安装elasticsearch和logstash之前,需要java的运行环境,所以需要提前安装好JDK,可以直接使用yum安装。也可以从Oracle官网下载JDK进行安装。开始之前要确保JDK正常安装并且环境变量也配置正确:

 安装JDK

[root@linux-node1 ~]# yum install -y java
[root@linux-node1 ~]# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

 yum安装elaticsearch和logstash

  下载并安装GPG key

[root@linux-node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

  添加es和logstash的yum仓库

#es的yum仓库
[root@hadoop-node1 ~]# 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
enabled=1

#logstash的yum仓库
[root@linux-node1 ~]# vim /etc/yum.repos.d/logstash.repo
[logstash-2.3]
name=Logstash repository for 2.3.x packages
baseurl=https://packages.elastic.co/logstash/2.3/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1

  安装elasticsearch和logstash

[root@hadoop-node1 ~]# yum install -y elasticsearch
[root@linux-node2 ~]# yum install -y logstash

 安装kibana

  Kibana 是为 Elasticsearch 设计的开源分析和可视化平台。你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互。你可以很容易实现高级的数据分析和可视化,以图表的形式展现出来。

  下载并安装GPG key

[root@linux-node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

  添加kibana的yum仓库

[root@linux-node1 ~]# vim /etc/yum.repos.d/kibana.repo 
[kibana-4.5]
name=Kibana repository for 4.5.x packages
baseurl=http://packages.elastic.co/kibana/4.5/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1

  安装kibana

[root@linux-node1 ~]# yum install -y kibana 

  当然也可以使用Cobbler创建ELKStack仓库来部署ELK环境

[root@log-node1 ~]# cobbler repo add --name=logstash-2.3 --mirror=http://packages.elastic.co/logstash/2.3/centos --arch=x86_64 --breed=yum
[root@log-node1 ~]# cobbler repo add --name=elasticsearch2 --mirror=http://packages.elastic.co/elasticsearch/2.x/centos --arch=x86_64 --breed=yum
[root@log-node1 ~]# cobbler repo add --name=kibana4.5 --mirror=http://packages.elastic.co/kibana/4.5/centos --arch=x86_64 --breed=yum
[root@log-node1 ~]# cobbler reposync

ELK配置实践  

 安装完elasticsearch后修改其配置文件如下:

[root@linux-node1 ~]# cd  /etc/elasticsearch/
[root@linux-node1 elasticsearch]# grep '^[a-Z]' elasticsearch.yml 
cluster.name: myes
node.name: linux-node1
path.data: /data/es-data
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 192.168.182.170
http.port: 9200

 创建目录/data/es-data并配置这个目录的权限为elasticsearch用户

midir  -p /data/es-data
chown  -R   elasticsearch.elasticsearch   /data/es-data

 启动elasticsearch成功后,用浏览器打开:http://http://192.168.1.180:9200/

systemctl   start  elasticsearch
systemctl   enable  elasticsearch

 也可以通过命令来访问:curl -i -XGET 'http://192.168.1.160:9200/_count?pretty'  返回的结果如下:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 101

{
  "count" : 11242,
  "_shards" : {
    "total" : 26,
    "successful" : 26,
    "failed" : 0
  }
}

 这说明es安装配置没问题了,但是这种现实看着很不爽,所以我们要加载一些插件来管理es,这里我们添加了head和kopf插件的方式来管理es。这些插件在github上可以找到,找到后下载并安装这些插件

[root@linux-node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
[root@linux-node1 ~]# /usr/share/elasticsearch/bin/plugin install lukas-vlcek/bigdesk
[root@linux-node1 ~]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

 下载完这些插件后,会在下面的目录中生成这些插件

[root@linux-node1 ~]# ls /usr/share/elasticsearch/plugins/
head  kopf

 安装好这些插件后就可以在浏览器上访问这些插件

  http://192.168.182.170:9200/_plugin/kopf 

  http://192.168.182.170:9200/_plugin/head

 在linux-node2上部署完elasticsearch后,修改其配置文件将linux-node2加入到es集群myes中,他们通过选举产生一个主es,这个主es负责管理es集群中es的状态,但对用户来说在那台es上都一样,因为es集群是分布式的。但是在linux-node2上部署es后,启动不起来,这时候将es的配置文件中添加单播模式,es集群默认是组播模式,不过在生产环境中没有出现这种情况。修改linux-node2的配置文件中的组播模式改为单播模式,linux-node2的配置文件修改如下:

[root@linux-node2 ~]# grep  "^[a-Z]"  /etc/elasticsearch/elasticsearch.yml 
cluster.name: myes
node.name: linux-node2
path.data: /data/es-data
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 192.168.182.171
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.182.170", "192.168.182.171"]

 接下来创建/data/es-data目录,并授予elasticsearch用户的权限。启动linux-node2上的es,这样在一个集群myes中的linux-node1和linux-node2就可以互相发现,主几点的es主机前面会有图标星,非主es主机前面图标为实心圆,主es存储的是收集来的原始信息,非主es收集的信息为主es上的信息的副本

 用logstash收集日志信息

  部署完logstash后,根据/etc/init.d/logstash脚本可以看出,logstash加载的配置文件放在/etc/logstash/conf.d/目录下。

  所以在/etc/logstash/conf.d/目录下创建收集日志的配置文件,让logstash收集的信息放到es中。这样在/opt/logstash/bin/logstash 启动logstash后,这个配置文件目录下的所有收集日志的配置文件都要被加载

[root@linux-node1 ~]# vim   /etc/init.d/logstash 
LS_CONF_DIR=/etc/logstash/conf.d
program=/opt/logstash/bin/logstash
args="agent -f ${LS_CONF_DIR} -l ${LS_LOG_FILE} ${LS_OPTS}"

  下面在/etc/logstash/conf.d/目录下创建一个测试用的日志收集配置文件

  关于如何配置logstash的配置文件请参考elastic官方的说明文档,这个文档说明很全:https://www.elastic.co/guide/en/logstash/2.3/index.html

[root@linux-node1 ~]# cd  /etc/logstash/conf.d/
input{
        stdin{}
}

filter{
}

output{
        elasticsearch{
                hosts => ["192.168.182.170"]
                index => "logstash-%{+YYYY.MM.dd}"
}
        stdout{
                codec => rubydebug
        }
}

  使用命令来测试:[root@linux-node1 conf.d]# /opt/logstash/bin/logstash  -f  /etc/logstash/conf.d/demo.conf。这样打开http://192.168.182.170:9200/_plugin/head/页面中就可以看到收集的信息。测试收集日志信息的步骤为:1.首先用标准输出来测试  2.在标准输出测试成功后,再把数据写入到es中

  当数据通过logstash写入到es后,master收集到日志后,会把一部分数据碎片到salve上(随机的一部分数据),master和slave又都会各自做副本,并把副本放到对方机器上,这样就保证了数据不会丢失。

  下面的例子为读取不同日志文件通过判断方式写入到es的不同的索引中,执行命令:[root@linux-node1 conf.d]# /opt/logstash/bin/logstash  -f  /etc/logstash/conf.d/file.conf

[root@linux-node1 conf.d]# cat  file.conf 
input{
  file{
    path => ["/var/log/messages","/var/log/secure"]
    start_position => "beginning"
    type => "system-log"
  }
  file{
    path => ["/var/log/elasticsearch/myes.log"]
    start_position => "beginning"
    type => "es-log"
  }
}
  
filter{
}

output{
  if [type] == "system-log" {
     elasticsearch{
        hosts => ["192.168.182.170"]
        index => "system-log-%{+YYYY.MM}"
     }
  }
  if [type] == "es-log" {
     elasticsearch{
        hosts => ["192.168.182.170"]
        index => "es-log-%{+YYYY.MM}"
     }
  }
}

  但是上面收集java的日志信息的时候,每个报错都给收集成一行了,不是按照一个报错,一个事件模块收集的。所以要使收集的java信息一个事件一个报错信息的话,就要对file.conf的配置文件做下修改,在收集java日志的输入插件中添加codec插件,修改如下:

[root@linux-node1 conf.d]# vim  file.conf 
input{
  file{
    path => ["/var/log/messages","/var/log/secure"]
    start_position => "beginning"
    type => "system-log"
  }
  file{
    path => ["/var/log/elasticsearch/myes.log"]
    start_position => "beginning"
    type => "es-log"
    codec => multiline {
        pattern => "^\["
        negate => true
        what => "previous"
    }
  }
}
  
filter{
}

output{
  if [type] == "system-log" {
     elasticsearch{
        hosts => ["192.168.182.170"]
        index => "system-log-%{+YYYY.MM}"
     }
  }
  if [type] == "es-log" {
     elasticsearch{
        hosts => ["192.168.182.170"]
        index => "es-log-%{+YYYY.MM}"
     }
  }
}

 kibana展示收集的日志信息

  安装kibana后,修改配置文件如下:

[root@linux-node1 config]# grep '^[a-Z]' /opt/kibana/config/kibana.yml 
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.182.170:9200"
kibana.index: ".kibana"

  启动kibana: /etc/init.d/kibana  start

  访问kibana的url为:http://192.168.182.170:5601

  在kibana界面中可以设置创建一些图形和dashboard页面,这样利用kibana就可以将日志收集展现的布局很到位

 rsyslog系统日志收集:

  比如一台机械上的rsyslog系统日志发送到另一台机器上的logstash中,让另一台机器的logstash来收集其他机器的系统日志

  首先在要发送的机器上开启发送系统日志配置,比如在linux-node1上开启发送rsyslog日志到logstash机器上linux-node2

[root@linux-node1 conf.d]# vim  /etc/rsyslog.conf 

*.* @@192.168.1.190:514

  然后在192.168.182.171机器上配置logstash如下:

[root@hadoop-node2 ~]# vim  rsyslog.conf
input{
    syslog {

      type => "system-syslog"

      port => 514

    }

}

filter{
}

output{
    elasticsearch{
        hosts => ["192.168.182.171"]
        index => "system-syslog-%{+YYYY.MM}"
    }
} 

  重启rsyslog:[root@linux-node1 ~]# systemctl restart rsyslog   

  检查配置文件:[root@elk-node1 ~]# /opt/logstash/bin/logstash -f syslog.conf --configtest

  执行logstash收集rsyslog日志:[root@elk-node1 ~]# /opt/logstash/bin/logstash -f syslog.conf  然后在linux-node1上执行logger  "this is  rsyslog  info  for  test"   这样设备产生的系统日志就会发送到192.168.182.171设备上,让其上的logstash进行收集到es中。

 收集nginx访问日志

  为了测试用,这里就用yum来安装nginx了:yum  install  -y  nginx

  修改nginx的配置文件,分别在nginx.conf的http和server配置区域添加下面内容

##### http 标签中
          log_format json '{"@timestamp":"$time_iso8601",'
                           '"@version":"1",'
                           '"client":"$remote_addr",'
                           '"url":"$uri",'
                           '"status":"$status",'
                           '"domain":"$host",'
                           '"host":"$server_addr",'
                           '"size":$body_bytes_sent,'
                           '"responsetime":$request_time,'
                           '"referer": "$http_referer",'
                           '"ua": "$http_user_agent"'
'}';
##### server标签中
            access_log /var/log/nginx/access_json.log json;

  启动nginx:[root@linux-node1 nginx]# systemctl  start  nginx

  使用json方式收集nginx的访问日志文件

[root@linux-node1 nginx]# cd  /etc/logstash/conf.d/
[root@linux-node1 conf.d]# vim  nginx-json.conf

input {
   file {
      path => "/var/log/nginx/access_json.log"
      codec => "json"
   }
}

output {
   stdout {
      codec => "rubydebug"
   }
}

  启动收集:[root@linux-node1 conf.d]# /opt/logstash/bin/logstash -f nginx-json.conf  这时候访问http://192.168.182.170 就可以看到收集到nginx的日志格式如下:

{
      "@timestamp" => "2018-03-08T11:58:29.000Z",
        "@version" => "1",
          "client" => "192.168.1.3",
             "url" => "/index.html",
          "status" => "304",
          "domain" => "192.168.1.180",
            "host" => "192.168.1.180",
            "size" => 0,
    "responsetime" => 0.0,
         "referer" => "-",
              "ua" => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36",
            "path" => "/var/log/nginx/access_json.log"
}

  测试nginx日志收集成功后,就可以写入es了,对主配置文件file.conf内容添加收集nginx日志部分:

#在input和output中分别添加如下内容
file {
       path => "/var/log/nginx/access_json.log"
       codec => json
       start_position => "beginning"
       type => "nginx-log"
    }


 if [type] == "nginx-log"{
        elasticsearch {
           hosts => ["192.168.182.170:9200"]
           index => "nignx-log-%{+YYYY.MM.dd}"
        }
    }

  执行:[root@linux-node1 conf.d]# /opt/logstash/bin/logstash -f file.conf 然后在kibana中添加nginx-log日志的索引,多访问几次http://192.168.182.170就可以在kibana中展示这个索引

 TCP日志的收集

  当es中收集的日志不全的时候,而且这个缺失的日志内容很重要,这时用tcp的方式就方便很多了

[root@linux-node2 conf.d]# vim tcp.conf

input {
  tcp {
    type => "tcp"
    mode => "server"
    port => "6666"
  }
}
output {
   stdout {
        codec => "rubydebug"
   }
}

  安装nc命令:yum install -y nc

[root@linux-node1 conf.d]# yum  install  nc  -y

  启动tcp收集:[root@linux-node2 conf.d]# /opt/logstash/bin/logstash -f tcp.conf

  测试tcp收集日志:[root@linux-node1 conf.d]# echo "hehe" | nc 192.168.1.190 6666   那么在linux-node2的tcp server端就会收集到信息如下:

{
       "message" => "hehe",
      "@version" => "1",
    "@timestamp" => "2018-03-08T12:30:18.757Z",
          "host" => "192.168.1.180",
          "port" => 34566,
          "type" => "tcp"
}

  测试成功后,就可以将tcp收集的方式放到file.conf的主配置文件中,让tcp收集的日志放到es中,并在kibana中展示出来。

 filter的使用

  filter的使用,主要用的是grok插件,这样就可以使用grok插件来对apahe的访问日志做成json格式,因为apache的访问日志默认不支持json格式。通过grok插件中定义的正则表达式来定义apache访问日志的json格式。一些logstash工具定义的默认正则表达式在这个路径中可以找到:/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/,这个目录中的grok-patterns这个文件就是定义apache日志的默认logstash  filter正则表达式

  因为测试环境中nginx的端口是80,这里apache的端口改为81。然后启动apache:systemctl  start  httpd

[root@linux-node1 conf.d]# vim  httpd-log.conf

        stdin{}
input{
     file {
       path => "/var/log/httpd/access_log"
       start_position => "beginning"
       type => "httpd-log"
     }  
}    

filter{
     grok {
        match => { "message" => "%{COMBINEDAPACHELOG}" }
     }
}

output{
        elasticsearch{
                hosts => ["192.168.182.170:9200"]
                index => "httpd-log-%{+YYYY.MM.dd}"
}               
        stdout{
                codec => rubydebug
        }       
}

  收集的apache的日志信息如下,可见为json格式的

[root@linux-node1 conf.d]# /opt/logstash/bin/logstash  -f  httpd-log.conf
{
        "message" => "192.168.1.3 - - [08/Mar/2018:21:23:06 +0800] \"GET /noindex/css/bootstrap.min.css HTTP/1.1\" 200 19341 \"http://192.168.1.180:81/\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36\"",
       "@version" => "1",
     "@timestamp" => "2018-03-08T13:37:51.576Z",
           "path" => "/var/log/httpd/access_log",
           "host" => "linux-node1.oldboyedu.com",
           "type" => "httpd-log",
       "clientip" => "192.168.1.3",
          "ident" => "-",
           "auth" => "-",
      "timestamp" => "08/Mar/2018:21:23:06 +0800",
           "verb" => "GET",
        "request" => "/noindex/css/bootstrap.min.css",
    "httpversion" => "1.1",
       "response" => "200",
          "bytes" => "19341",
       "referrer" => "\"http://192.168.1.180:81/\"",
          "agent" => "\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36\""
}

  测试成功后,就可以写入es文件了,然后再kibana中展示出来

生产环境中ELK的架构实践

 在生产环境中,在需要收集日志文件的设备上部署logstash,通过logstash收集完的日志信息放到redis消息队列中,然后在其他设备上使用python脚本启动多线程来过滤redis消息队列中的消息,将有用的消息发送es中,将没用的消息丢弃掉,这样方式收集的信息比较有用且高效,不像在logstash端使用grok插件来过滤消息而影响设备性能。

 还有一种情况,一旦我们的elasticsearch出现问题,就不能进行日志采集处理了。解决的办法也是在logstash 和es中加上消息中间件redis,通过logstash收集来的消息放到redis中,就算es突然挂掉了,消息依然保存在redis中,当es正常了就继续从redis中取数据。同时使用这种方式也体现了解耦合的效果。

 在本案例中收集日志的方式为:logstash-->redis-->logstash-->es ,这种方式只是测试使用,生产环境中最好用logstash-->redis-->python-->es的方式

 下面在linux-node2上部署redis,生产上最好另起一台设备部署redis  在linux-node2上取redis里面的消息到es上

 redis的配置和启动

[root@linux-node2 conf.d]# yum  install  redis  -y

[root@linux-node2 conf.d]# vim  /etc/redis.conf
bind 192.168.182.171   #绑定ip地址
daemonize yes    #后台启动

[root@linux-node2 conf.d]# systemctl start  redis
[root@linux-node2 conf.d]# systemctl  enable  redis

 在linux-node1上测试用logstash收集日志放到redis,输入几行数据

[root@linux-node1 conf.d]# /opt/logstash/bin/logstash -f  redis-out.conf 
Settings: Default pipeline workers: 1
Pipeline main started
sdfsfdafasdf
this  is gook  elk ssytem
hahaa
gosererf wfsaldjfasf
214rsalfjasldf

 然后在linux-node2上连接redis查看key的长度是否变化,如果在linux-node1上不停的输入内容,redis的key的长度在增加说明logstash收集的日志信息写入到redis了

[root@linux-node2 conf.d]# redis-cli -h  192.168.182.171
192.168.182.171:6379> select  6
192.168.182.171:6379[6]> keys *
1) "demo"
192.168.182.171:6379[6]> llen demo
(integer) 6
192.168.182.171:6379[6]> llen demo
(integer) 8

 最后将redis消息写到es中

[root@linux-node2 conf.d]# vim redis-in.conf
input {
    redis {
      host => "192.168.182.171"
      port => "6379"
      db => "6"
      data_type => "list"
      key => "demo"
   }
}
 
output {
    elasticsearch {
      hosts => ["192.168.182.170:9200"]
      index => "redis-in-%{+YYYY.MM.dd}"
    }
}

 启动收集redis消息到es:[root@linux-node2 conf.d]# /opt/logstash/bin/logstash  -f  redis-in.conf 这时候再查看redis的key的长度,发现key的长度为空,说明redis里面的数据被es消费掉了

[root@linux-node2 conf.d]# redis-cli -h  192.168.182.171
192.168.182.171:6379> select  6
192.168.182.171:6379[6]> keys *
(empty list or set)

 上面测试使用logstash-->redis-->logstash-es测试通过后,将收集到的所有日志写入到redis中。这了重新定义一个添加redis缓存后的总文件shipper.conf。(可以将之前执行的总文件file.conf停掉)

input {
    file {
      path => "/var/log/messages"
      type => "system"
      start_position => "beginning"
    }
  
    file {
       path => "/var/log/elasticsearch/myes.log"
       type => "es-error"
       start_position => "beginning"
       codec => multiline {
           pattern => "^\["
           negate => true
           what => "previous"
       }
    }
    file {
       path => "/var/log/nginx/access_json.log"
       codec => json
       start_position => "beginning"
       type => "nginx-log"
    }
    syslog {
        type => "system-syslog"
        port => "514"
    }
  
}
  
  
output {
   if [type] == "system"{
     redis {
        host => "192.168.182.171"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system"
     }
   }
  
    if [type] == "es-error"{
      redis {
        host => "192.168.182.171"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "demo"
        }
     }
    if [type] == "nginx-log"{   
       redis {
         host => "192.168.182.171"
          port => "6379"
          db => "6"
          data_type => "list"
          key => "nginx-log"
       }
    }
    if [type] == "system-syslog"{
       redis {
          host => "192.168.182.171"
          port => "6379"
          db => "6"
          data_type => "list"
          key => "system-syslog"
       }   
     }
}

 在linux-node1上启动logstash,让其直接在后台运行,[root@linux-node1 conf.d]# systemctl start logstash.service  这样logstash就会直接在/etc/logstash/conf.d/目录下找收集日志的配置文件

 在linux-node2上收集redis里面的数据写到es中,当然file.conf文件中也可放入收集apache的内容

[root@linux-node2 ~]# vim file.conf
input {
     redis {
        type => "system"
        host => "192.168.182.171"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "system"
     }
 
      redis {
        type => "es-error"
        host => "192.168.182.171"
        port => "6379"
        db => "6"
        data_type => "list"
        key => "es-error"
        }
       redis {
          type => "nginx-log"
          host => "192.168.182.171"
          port => "6379"
          db => "6"
          data_type => "list"
          key => "nginx-log"
       }
       redis {
          type => "system-syslog"
          host => "192.168.182.171"
          port => "6379"
          db => "6"
          data_type => "list"
          key => "system-syslog"
       }   
 
}
 
 
output {
 
    if [type] == "system"{
        elasticsearch {
           hosts => ["192.168.182.170:9200"]
           index => "system-%{+YYYY.MM.dd}"
        }
    }
 
    if [type] == "es-error"{
        elasticsearch {
           hosts => ["192.168.182.170:9200"]
           index => "es-error-%{+YYYY.MM.dd}"
        }
    }
    if [type] == "nginx-log"{
        elasticsearch {
           hosts => ["192.168.182.170:9200"]
           index => "nignx-log-%{+YYYY.MM.dd}"
        }
    }
    if [type] == "system-syslog"{
        elasticsearch {
           hosts => ["192.168.182.170:9200"]
           index => "system-syslog-%{+YYYY.MM.dd}"
        }
    }
}

 在linux-node2上启动logstash,让其直接在后台运行,[root@linux-node2 conf.d]# systemctl start logstash.service  这样logstash就会直接在/etc/logstash/conf.d/目录下找收集日志的配置文件

 最后通过kibana在web进行展示即可

可以执行这个 去查看nginx日志或apache日志 
[root@linux-node1 ~]# ab -n10000 -c1 http://192.168.182.170/

[root@linux-node1 ~]# ab -n10000 -c1 http://192.168.182.170:81

也可以启动多个redis写到ES中,具体根据自己的实际情况而定。
posted @ 2018-03-05 22:12  goser  阅读(180)  评论(0)    收藏  举报