ELK之Filbeat轻量日志采集模块

ELK之Filbeat轻量日志采集模块

当你有N台服务器,然后想实时采集日志,那么你需要Filebeat采集日志,统一上传到日志服务器。

注意这里是实时采集,如果是非实时,也可以直接使用Logstash去采集过滤上传日志服务器。

那么Filebeat模块采集后传给谁?

  • 直接输送给Elasticsearch
  • 输送给Logstash,由Logstash继续过滤处理日志,最后传输给Elasticsearch。

本文仅提供 部署安装的说明,详细的配置后续再进行额外教程说明。

1.下载

点我跳转到官网下载页面

1
$ wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.1-linux-x86_64.tar.gz

2.解压部署

1
2
$ tar zxf filebeat-7.14.1-linux-x86_64.tar.gz
$ cd filebeat-7.14.1-linux-x86_64

3.配置文件配置

由于技术日新月异,下面的配置仅供参考。

$ vim filebeat.yml

filebeat.inputs:

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /opt/testerzhang/biglog/normallog/ApiServer/ApiServer.log

  fields:
     app_id: testerzhang-api-26
     log_type: testerzhang-api-26

  # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
  #multiline.pattern: ^\[
  multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'

  # Defines if the pattern set under pattern should be negated or not. Default is false.
  #multiline.negate: false
  multiline.negate: true


  # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
  # that was (not) matched before or after or as long as a pattern is not matched based on negate.
  # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
  multiline.match: after
  #没有新日志采集后多长时间关闭文件句柄,默认5分钟,设置成1分钟,加快文件句柄关闭;
  close_inactive: 1m
  #传输了3h后如果没有传输完成的话就强行关闭文件句柄,这个配置项是解决以上案例问题的key point;
  close_timeout: 3h
  #这个配置项也应该配置上,默认值是0表示不清理,不清理的意思是采集过的文件描述在registry文件里永不清理,在运行一段时间后,registry会变大,可能会带来问题。
  clean_inactive: 72h
  #设置了clean_inactive后就需要设置ignore_older,且要保证ignore_older < clean_inactive
  ignore_older: 70h


setup.template.name: "filebeat"
setup.template.pattern: "filebeat-*"
setup.template.settings:
  index.number_of_shards: 3


# filebeat 配置关闭 ILM 即可解决Index Pattern不生效的问题
setup.ilm.enabled: false



#如果要直接写入搜索引擎,需要配置下面的配置项,否则注释掉。
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["10.10.10.10:9200"]
  index: "%{[fields.log_type]}-%{+yyyy.MM.dd}"


#如果要直接写入Logstash,需要配置下面的配置项,否则注释掉。
output.logstash:
  # The Logstash hosts
 hosts: ["10.10.10.10:5045","10.10.10.11:5045","10.10.10.12:5045"]

主要配置项说明:

  • enabled:true 代表开启这个配置节
  • paths: 监控指定目录下的文件,支持模糊搜索
  • fields: 增加fields额外字段,本例在fields下面增加了app_id、log_type字段
  • multiline 多行日志监控,下面配置的意思是:不以时间格式开头的行都合并到上一行的末尾(正则写的不好,忽略忽略)
    • pattern:正则表达式
    • negate:true 或 & false;默认是false,匹配pattern的行合并到上一行;true,不匹配pattern的行合并到上一行
    • match:after 或 before,合并到上一行的末尾或开头
  • output.elasticsearch: 配置host指定elasticsearch搜索引擎地址
  • output.logstash: 配置host指定logstash地址

4.启动

$ nohup ./filebeat -e -c filebeat.yml > filebeat.log &

如果你是在微信打开此文章,点我跳转到我的公众号


本文没有授权给任何组织、企业和个人转载,未经作者允许禁止转载!

欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

updatedupdated2021-09-162021-09-16