ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台


一、准备工具:(Centos7)

1、Elasticsearch:ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。


2Logstash:是一个完全开源的工具,他可以对你的日志进行收集、分析,并将其存储供以后使用(如,搜索),您可以使用它。说到搜索,logstash带有一个web界面,搜索和展示所有日志。


3、kibana: 是一个为 ElasticSearch 提供日志分析的 Web UI工具,可使用它对日志进行高效的搜索、可视化、分析等各种操作。


二、实验

安装java环境

[root@elk src]# yum install -y java-1.8.0-openjdk*

安装ElasticSearch

[root@elk src]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.3.tar.gz
[root@elk src]# tar xf elasticsearch-5.6.3.tar.gz


创建普通用户,用于启动ElasticSearch

[root@elk src]# useradd elk
[root@elk src]# chown -R elk:elkelasticsearch-5.6.3


修改配置文件

[root@elk ~]# cd/usr/local/src/elasticsearch-5.6.3/config/
[root@elk config]# vim elasticsearch.yml
node.name: elk
http.port: 9200
network.host: 10.0.0.128


启动测试:

[root@elk src]# su – elk
[elk@elk src]$ cd elasticsearch-5.6.3/bin/
[elk@elk bin]$ ./elasticsearch

##出错

[1]: max file descriptors [4096] forelasticsearch process is too low, increase to at least [65536]

[2]: max virtual memory areasvm.max_map_count [65530] is too low, increase to at least [262144]

##解决方法:

[root@elk ~]# vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
[root@elk ~]# vim /etc/sysctl.conf
vm.max_map_count=262144
#重启生效


安装head:方便查看索引,集群等相关状态

创建插件存放目录(目录自定义)

[root@elk ~]# cd /usr/local/src/
[root@elk src]# mkdir head
[root@elk src]# cd head/
[root@elk src]# yum install -y git
[root@elk head]# git clonegit://github.com/mobz/elasticsearch-head.git
正克隆到 'elasticsearch-head'...
remote: Counting objects: 4224, done.
remote: Total 4224 (delta 0), reused 0(delta 0), pack-reused 4224
接收对象中: 100% (4224/4224), 2.15 MiB | 223.00KiB/s, done.
处理 delta 中: 100%(2329/2329), done.
[root@elk head]# cd elasticsearch-head/
[root@elk elasticsearch-head]# yum install-y npm
[root@elk elasticsearch-head]# npm install
[root@elk elasticsearch-head]# npm install-g grunt-cli
[root@elk elasticsearch-head]# grunt server  #运行


修改配置文件监听端口为localhost

[root@elk ~]# vim /usr/local/src/head/elasticsearch-head/Gruntfile.j
 server: {
     options: {
         port: 9100,
         hostname: '0.0.0.0',  #添加这句
         base: '.',
         keepalive: true
       }
   }


修改es配置文件,添加如下:

[root@elk ~]# vim/usr/local/src/elasticsearch-5.6.3/config/elasticsearch.yml
http.cors.enabled:true
http.cors.allow-origin:"*"
#重启es服务


启动head插件,然后访问web的ip即可

[root@elk elasticsearch-head]# grunt server


安装logstash

[root@elk src]# wget https://artifacts.elastic.co/downloads/logstash/logstash-5.6.3.tar.gz
[root@elk src]# tar zx logstash-5.6.3.tar.gz
[root@elk src]# cd logstash-5.6.3/bin/


创建导入到elasticsearch的配置文件:

[root@zxb ~]# vim /logstash/nginx.conf

input {
    file {
        path => "/usr/local/nginx/log/access.log" #输入的数据位置
        type => "test"
        start_position => "beginning" #从文件开始出读
   }
}
filter{
    grok {
        match => {
        "message" => "%{IPORHOST:IP} - %{USER:User} \[%{HTTPDATE:Time}\] \"%{WORD:HTTP_Method} %{NOTSPACE:Request} HTTP/%{NUMBER:HTTP_Version}\" %{NUMBER:Status} (?:%{NUMBER:Bytes}|-) \"(?:%{URI:HTTP_Referer}|-)\" \"%{GREEDYDATA:User_Agent}\""  ##nginx默认格式
    }
    }
}
output {
     elasticsearch {
       action => "index"
       hosts  => "10.0.0.128:9200"
       index  => "log-%{+yyyy.MM.dd}" ##索引名
   }
   stdout {
       codec => rubydebug
  }
}


安装kibana

[root@elk src]# wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.3-x86_64.rpm
[root@elk src]# rpm -ivhkibana-5.6.3-x86_64.rpm

#配置文件位于: /etc/kibana/kibana.yml


默认链接的是elasticsearch,修改配置文件

[root@elksrc]# vim /etc/kibana/kibana.yml
server.host:"0.0.0.0"
elasticsearch.url: http://10.0.0.128:9200


启动kibana服务,默认占用5601端口,可通过http://ip:5601访问

[root@elk ~]# /etc/init.d/kibana start
kibana started
[root@elk ~]# ps -ef |grep 5601
root     10343   9741  0 17:18 pts/1    00:00:00 grep --color=auto 5601


启动e l k, head(可查看状态):

##在启动logstash 可能会报错:                                                                                                                                                                   OpenJDK64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000,986513408, 0) failed; error='Cannot allocate memory' (errno=12)

解决方法:
[root@elk bin]# vim/usr/local/src/elasticsearch-5.6.3/config/jvm.options
#修改如下:
-Xms300m
-Xmx300m

导入数据

[root@elk bin]# cd /usr/local/src/logstash-5.6.3/bin

[root@elk bin]# ./logstash -f /logstash/nginx.conf


可以查看访问10.0.0.128:9100(head)网页,看是否有数据导入

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第1张图片


打开kibana界面,选择定义的索引,并创建

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第2张图片


添加成功后,会发现有自定义的数据导入

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第3张图片

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第4张图片


创建一个关于状态码的图形

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第5张图片


ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第6张图片


可以匹配多个条件

ELK(ElasticSearch+Logstash+ Kibana)搭建实时日志分析平台_第7张图片