前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ELK(ElasticSearch, Logstash, Kibana)搭建日志分析平台

ELK(ElasticSearch, Logstash, Kibana)搭建日志分析平台

作者头像
小小科
发布2018-06-20 12:03:08
6820
发布2018-06-20 12:03:08
举报
文章被收录于专栏:北京马哥教育

作者:FenG_Vnc

来源:见文末

环境 :

一台 centos 6.7

IP地址:

192.168.88.250

软件版本 :

ElasticSearch 2.1.0 Logstash 2.1.1 Kibana 4.3.1 JDK 1.8.0.77

JDK 我这里没有地址 就不连接了

下载好JDK 放在路径/usr/local/java

编辑配置文件 /etc/profile

代码语言:javascript
复制
export JAVA_HOME=/usr/local/java/jdk1.8.0_77
export PATH=$JAVA_HOME/bin:$PATH

加入这两句以后 然后 source /etc/profile

确认生效 java -version

代码语言:javascript
复制
[root@master ~]# java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

1、 搭建 ElasticSearch

代码语言:javascript
复制
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.1.0/elasticsearch-2.1.0.tar.gz
tar xf elasticsearch-2.1.0.tar.gz 
cd /usr/local/elasticsearch-2.1.0/bin
./plugin  -install mobz/elasticsearch-head  # web集群管理插件  安装好了以后可以在plugin文件发现多了一个head
./elasticsearch  -Des.insecure.allow.root=true  #加这个参数才可以root启动

curl -X GET 192.168.88.250:9200   #curl 测试
{
  "name" : "Reeva Payge",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.1.0",
    "build_hash" : "72cd1f1a3eee09505e036106146dc1949dc5dc87",
    "build_timestamp" : "2015-11-18T22:40:03Z",
    "build_snapshot" : false,
    "lucene_version" : "5.3.1"
  },
  "tagline" : "You Know, for Search"
}
web地址  http://192.168.88.250:9200/_plugin/head/

2、搭建NGINX

代码语言:javascript
复制
wget   搭建nginx之前需要安装 pcre
tar xf nginx-1.7.8.tar.gz
cd /usr/local/nginx
vim /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
   worker_connections  1024;
}

http {
  
upstream kibana4 {  #对Kibana做代理  
        server 127.0.0.1:5601 fail_timeout=0;
}
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
                     
     log_format json '{"@timestamp":"$time_iso8601",'   #配置NGINX的日志格式 json
                        '"host":"$server_addr",'
                        '"clientip":"$remote_addr",'
                        '"size":$body_bytes_sent,'
                        '"responsetime":$request_time,'
                        '"upstreamtime":"$upstream_response_time",'
                        '"upstreamhost":"$upstream_addr",'
                        '"http_host":"$host",'
                        '"url":"$uri",'
                        '"xff":"$http_x_forwarded_for",'
                        '"referer":"$http_referer",'
                        '"agent":"$http_user_agent",'
                        '"status":"$status"}';
     access_log /var/log/nginx/access.log_json json;   #配置日志路径 json格式
     error_log /var/log/nginx/error.log;  

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }



    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

  
server {
    listen               *:80;
    server_name          kibana_server;
    access_log           /var/log/nginx/kibana.srv-log-dev.log;
    error_log            /var/log/nginx/kibana.srv-log-dev.error.log;


    location / {
        root   /var/www/kibana;
        index  index.html  index.htm;
    }

    location ~ ^/kibana4/.* {
        proxy_pass           http://kibana4;
        rewrite              ^/kibana4/(.*)  /$1 break;
        proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header     Host            $host;
        auth_basic           "Restricted";
        auth_basic_user_file /etc/nginx/conf.d/kibana.myhost.org.htpasswd;
    }

}
}

3、搭建 Logstash

代码语言:javascript
复制
wget https://download.elastic.co/logstash/logstash/logstash-2.1.1.tar.gz
tar xf logstash-2.1.1.tar.gz 
cd /usr/local/logstash-2.1.1/bin
vim stdin.conf #编写配置文件
input{
        file {
                path => "/var/log/nginx/access.log_json"  #NGINX日志地址 json格式
               codec => "json"  json编码
        }
}
filter {
        mutate {
                split => ["upstreamtime", ","]    
        }
        mutate {
                convert => ["upstreamtime", "float"]
        }
}
output{

        elasticsearch {
                hosts => ["192.168.88.250:9200"]   #elasticsearch地址
                index => "logstash-%{type}-%{+YYYY.MM.dd}"   #索引
                document_type => "%{type}"    
                workers => 1
                flush_size => 20000        #传输数量 默认500
                idle_flush_time => 10      #传输秒数  默认1秒
                template_overwrite => true   
        }
}
 ./logstash -f stdin.conf &  #后台启动
启动成功以后 打开刚才搭建的web服务器  es就能看到数据

4、搭建Kibana

代码语言:javascript
复制
wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
tar xf kibana-4.3.1-linux-x64.tar.gz
cd /usr/local/kibana-4.3.1-linux-x64/
vim ./config/kibana.yml

elasticsearch.url: "   只需要修改URL为ElasticSearch的IP地址
./kibana &  后台启动

启动成功以后 会监听 5601端口

全部搭建好了以后就可以用Kibana查看

地址 : 192.168.88.250:5601

如果create灰色的 说明没有创建索引 打开你的nginx服务器 刷新几下 采集一下数据

然后 选择 左上角的 Discover

数据可能会出不来 那是因为 Kibana 是根据时间来匹配的 并且 因为 Logstash的采集时间使用的UTC 永远早8个小时

所以设置时间 要设置晚8个小时以后

设置好了时间以后 。数据基本就会看的到

这里可以设置你想看到的任意 数据 选择 add 就能看到的 不想看 可以remove

还有后面的 Visualize 也可以个性化定制图标

基本就到此结束了,另外如果 Kibana出不来数据 一般都是因为时间设置不正确。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-06-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 马哥Linux运维 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档