ElasticSearch+Logstash+Kibana+redis+filebeat搭建日志收集分析平台

一、背景

  • 公司随着业务的增多,服务器也慢慢增多,并且也搭建了集群环境。带来的一个不便之处就是,要查看日志排除线上故障时,经常要打开多个tomcat日历,逐个查看,非常麻烦。所以就有了要搭建一个能通过WEB,实时查看所有Tomcat服务器日志平台的想法。在技术选型的过程中,发现ElasticSearch+Logstash+Kibana+Redis+filebeat的技术栈算是目前比较成熟,比较合适的方案。
    • ElasticSearch :分布式搜索和分析引擎,具有高可伸缩、高可靠和易管理等特点。基于 Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作。通常被用作某些应用的基础搜索引擎,使其具有复杂的搜索功能;
    • Logstash:数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置;
    • Kibana:数据分析和可视化平台。通常与 Elasticsearch 配合使用,对其中数据进行搜索、分析和以统计图表的方式展示;
    • Redis:基于内存亦可持久化的日志型、Key-Value超高性能 key-value 数据库
    • filebeat:ELK 协议栈的新成员,一个轻量级开源日志文件数据搜集器,基于 Logstash-Forwarder 源代码开发,是对它的替代。在需要采集日志数据的 server 上安装 Filebeat,并指定日志目录或日志文件后,Filebeat 就能读取数据,迅速发送到 Logstash 进行解析,亦或直接发送到 Elasticsearch 进行集中式存储和分析。
  • 下面引用一张图来说明各个组件在此系统中的角色作用。总结来说:filebeat负责采集日志数据,并且经消息队列输出插件输出到消息队列中,它安装在各个需要采集日志的服务器中;Redis充当消息队列的角色,均衡了网络传输,降低了Logstash的压力,解决了传统模式下filebeat与Logstash日志直接传输丢失数据的可能性;Logstash负责将Redis中的日志数据过滤、丰富、处理;ElasticSearch负责存储并索引Logstash转发过来的数据,提供高效的日志数据的查询效率;Kibana:负责提供一个WEB可视化界面查询分析日志。
    image.png

二、环境软件准备

  • 此处我的系统是Centos7。采用单机版安装。即Elasticsearch 、Redis、Logstash、kibana都装在同一台机器上。据网上的参考资料,单机版ES在内存8G的前提下,可以支持50GB的日志数据查询,并且可以设置定期删除老旧日志,所以完全够我们使用。
  1. 安装jdk1.8
  2. 关闭防火墙
  3. 禁用selinux
    vi /etc/selinux/config, 修改成SELINUX=disabled即可。重启后生效。
  4. 服务器IP


    image.png
  5. 此处我演示的是收集nginx日志。

三、安装配置软件

一. 安装配置Redis

参考这个教程

二. 安装配置Filebeat.
  1. 下载Filebeat,此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动。注意Filebeat是安装在需要采集日志的服务器上,比如你的Nginx服务器,或者Tomcat服务器等。
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.7.0-x86_64.rpm
  1. 安装
rpm -ivh filebeat-6.7.0-x86_64.rpm 

3.配置Filebeat

  • 此处我先修改nginx的日志格式,方便以后过滤
vi /etc/nginx/ngxin.conf
  • 加入以下内容
log_format json '{"@timestamp": "$time_iso8601", '
                              '"time": "$time_iso8601", '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"host": "$host", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"uri": "$uri", '
                               '"http_referrer": "$http_referer", '
                               '"body_bytes_sent":"$body_bytes_sent", '
                               '"http_x_forwarded_for": "$http_x_forwarded_for", '
                               '"http_user_agent": "$http_user_agent" '
               '}';
access_log  /var/log/nginx/access-json.log  json;
  • 最后的nginx.conf为

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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", '
                              '"time": "$time_iso8601", '
                               '"remote_addr": "$remote_addr", '
                               '"remote_user": "$remote_user", '
                               '"body_bytes_sent": "$body_bytes_sent", '
                               '"request_time": "$request_time", '
                               '"status": "$status", '
                               '"host": "$host", '
                               '"request": "$request", '
                               '"request_method": "$request_method", '
                               '"uri": "$uri", '
                               '"http_referrer": "$http_referer", '
                               '"body_bytes_sent":"$body_bytes_sent", '
                               '"http_x_forwarded_for": "$http_x_forwarded_for", '
                               '"http_user_agent": "$http_user_agent" '
               '}';

    access_log  /var/log/nginx/access.log  main;
    access_log  /var/log/nginx/access-json.log  json;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
  • 接着再配置filebeat
vi /etc/filebeat/filebeat.yml
  • 修改如下。大家照着对比一下修改。另外有几个注意点:
    • 这里我增加了写入redis的output,那么其他的out都要删掉比如Elasticsearch output、Logstash output
    • 如果配置配置文件里面有中文,那么必须修改yml文件的格式为utf-8,否则也会启动不了。大家用EditPlus修改即可。否则会报错Exiting: error loading config file: yaml: invalid trailing UTF-8 octet
###################### Filebeat Configuration Example #########################

# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html

# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.

#=========================== Filebeat inputs =============================

filebeat.inputs:

- type: log

  enabled: true

  paths:
    - /var/log/nginx/access-json.log   #指明读取文件的位置
#============================= Filebeat modules ===============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #reload.period: 10s

#==================== Elasticsearch template setting ==========================

setup.template.settings:
  index.number_of_shards: 3
  #index.codec: best_compression
  #_source.enabled: false

#================================ Outputs =====================================

# Configure what output to use when sending the data collected by the beat.

#-------------------------- Redis output ------------------------------
output.redis:
   hosts: ["192.168.1.110:6379"]   #输出到redis的机器
   password: "123456"
   key: "filebeat:test16"   #redis中日志数据的key值ֵ
   db: 0
   timeout: 5


#================================ Processors =====================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~



  1. 启动Filebeat
systemctl start filebeat   #启动
systemctl enable filebeat     # 设置开机启动
systemctl status filebeat  
  1. 正常情况下这时应该能在redis 中看到日志。如果看不到,可以到/var/log/filebeat文件夹中,查看日志,排除故障。
三. 安装配置Elasticsearch
  1. 下载Elasticsearch-6.7.0。此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.rpm
  1. 安装
rpm -ivh elasticsearch-6.7.0.rpm
  1. 配置ES
  • 修改配置文件
vi /etc/elasticsearch/elasticsearch.yml
path.data: /mnt/ELK/data  #数据存储目录,可以不修改,看个人情况,此处修改是因为我这个目录挂在的磁盘空间大
path.logs: /mnt/ELK/logs  #日志存储目录,可以不修改,看个人情况
network.host: 192.168.1.110 #监听本地ip,防止外网恶意访问9200端口
http.port: 9200    #http监听端口
  • 由于启动ES不能用root用户,所以需要创建一个用户来启动ES
groupadd elk          # 添加用户组
useradd -g elk elk    # 添加用户到指定用户组
passwd elk            # 为elk用户设置密码
  • 修改权限
chown -R elk /etc/elasticsearch/  #将安装的ES文件夹赋权限给elk用户
chown -R elk /usr/share/elasticsearch #将安装的ES文件夹赋权限给elk用户
chown -R elk /usr/lib/systemd/system/elasticsearch.service  # 后台开机命令赋权限
chown -R elk /var/log/elasticsearch/
chown -R elk /var/run/elasticsearch/
chown -R elk /etc/sysconfig/elasticsearch
chown -R elk /mnt/ELK/data  #将数据存储目录赋权限给elk用户
chown -R elk /mnt/ELK/logs  #将日志存储目录赋权限给elk用户
  • 修改启动文件的启动用户
vi /usr/lib/systemd/system/elasticsearch.service
# 修改如下
User=elk
Group=elk
  • 设置开机启动
systemctl daemon-reload
systemctl enable elasticsearch.service   # 设置开机启动
systemctl start elasticsearch.service #启动
systemctl status elasticsearch.service  #查看运行状态
  • 如果发现报错


    image.png
  • 第一个是,每个进程最大同时打开文件数太小
su root # 切换到root 用户
vi /etc/security/limits.conf   # 修改
# limits.conf文件中,增加如下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
  • 第二个是,打开虚拟内存的个数太少需要增加
vi /etc/sysctl.conf 
# sysctl.conf 文件中,增加如下内容
vm.max_map_count = 655360
sysctl -p   #执行命令立即生效
  • 修改后,再次启动ES。启动成功后,访问IP:9200出现一串json,就代表成功了。
curl -l http://localhost:9200
image.png
四. 安装配置Logstash
  1. 下载Logstash。此处我统一下载到/opt目录下,下载网址可以到官网上查找。我下载的是rpm包,这个安装包方便设置开机启动
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.7.0.rpm
  1. 安装
rpm -ivh logstash-6.7.0.rpm
  1. 安装GeoLite2,用来分析访问客户端IP归属地
  • 下载
cd /opt/
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
  • 解压
tar -zxvf GeoLite2-City.tar.gz
  • 重命名一下解压后的文件夹,我这里解压出来的文件名是GeoLite2-City_20190402,重命名为GeoLite2-City。GeoLite2-City文件夹下的GeoLite2-City.mmdb数据库为我们需要的数据库,后续配置中会用到。
mv GeoLite2-City_20190402/ GeoLite2-City/
  1. 配置Logstash
  • 直接增加一个配置文件,这里命名随意,我这里取名nginx16-access。需注意如果配置文件里面有中文,那么必须修改yml文件的格式为utf-8,否则也会启动不了。大家用EditPlus修改即可。
vi /etc/logstash/conf.d/nginx16-access.conf
  • 填入以下内容
input {
    redis {
        data_type =>"list"
        key =>"filebeat:test16"
        host =>"192.168.1.110"
        port => 6379
        password => "123456"
        threads => "8"
        db => 0
        #codec => json
        }
}

filter {
    #在json化之前,使用mutte对\\x字符串进行替换,防止以下错误:ParserError: Unrecognized character escape 'x' (code 120)
    mutate {
        gsub => ["message", "\\x", "\\\x"]
    }
    json {
        source => "message"
        remove_field => ["beat","message"]
    }
    #使用geoip库定位ip
    geoip {
        source => "remote_addr" #nginx日志中外部访问ip对应字段
        database => "/opt/GeoLite2-City/GeoLite2-City.mmdb"
        #去掉显示geoip显示的多余信息
        remove_field => ["[geoip][latitude]", "[geoip][longitude]", "[geoip][country_code]", "[geoip][country_code2]", "[geoip][country_code3]", "[geoip][timezone]", "[geoip][continent_code]", "[geoip][region_code]", "[geoip][ip]"]
        target => "geoip"
     }
    mutate { 
        convert => [ "[geoip][coordinates]", "float" ] 
    }
}


output {
    elasticsearch {
        hosts => ["192.168.1.110:9200"]      
        index => "logstash-test16-nginx-access-%{+yyyy.MM.dd}"         #注意此处索引名称,一定要以logstash开头命名,后者地图功能不可用(mapping)
    }
}

  1. 启动
systemctl enable logstash   # 设置开机启动
systemctl start logstash    #启动
systemctl status logstash     #查看运行状态
  • 正常启动后,Logstash应该是能从redis中获取日志数据,写入es。可以查看Logstash的运行日志。在/var/log/logstash/ 目录中。另外需要注意一点,Logstash抓取日志后,会将redis中的额日志删除掉。
  • 另外安装完后,可以访问这个链接,看看Logstash有没有把索引以及数据写入ES。
    http://192.168.1.110:9200/_cat/indices?v
五. 安装配置Kibana
  1. 下载Kibana
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.7.0-x86_64.rpm
  1. 安装
rpm -ivh kibana-6.7.0-x86_64.rpm
  1. 配置
vi /etc/kibana/kibana.yml 
  • 修改这三个配置即可
# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: "0.0.0.0"

# Enables you to specify a path to mount Kibana at if you are running behind a proxy.
# Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
# from requests it receives, and to prevent a deprecation warning at startup.
# This setting cannot end in a slash.
#server.basePath: ""

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0.
#server.rewriteBasePath: false

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URLs of the Elasticsearch instances to use for all your queries.
elasticsearch.hosts: ["http://192.168.1.110:9200"]
  1. 启动
systemctl enable kibana   # 设置开机启动
systemctl start kibana #启动
systemctl status kibana  #查看运行状态
  • 启动成功后即可访问
    http://192.168.1.110:5601


    image.png
  1. 配置kibana界面
  • 选择


    image.png
  • 建立索引


    image.png
  • 可以看到nginx16-access.conf建立的索引


    image.png

    image.png

    image.png
  • 看见此画面代表成功了。


    image.png

    image.png
  • 最后设置一下自动刷新


    image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容