Filebeat收集服务端日志

一、什么是Filebeat?

Filebeat是轻量级本地文件的日志数据采集器,可监控日志目录或特定日志文件,并将它们转发给Elasticsearch或Logstatsh进行索引、kafka等。带有内部模块(auditd,Apache,Nginx,System和MySQL),可通过一个指定命令来简化通用日志格式的收集、解析和可视化,能做到实时收集日志,延迟在秒级,配置非常简单方便,占用资源少。

二、业务背景?

公司后端服务都是使用Golang开发的,服务部署在多台节点上,日志没有统一收集,异常问题排查困难,根据以往经验,首先想到EKL这一套日志系统。整个日志链路是这样的:Filebeat收集日志——> Elasticsearch 存储日志——> Kibana日志可视化查询。

三、安装Filebeat

step1: 官网下载对应版本的安装包

https://elasticsearch.cn/download

采坑记录:一开始下载的是tar.gz的安装包,配置完之后,使用nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 & 方式启动,运行一段时间后进程总是自动停止了,查阅各种资料之后,得到解决方案,下载rpm包安装。

step2.安装

rpm -ivh filebeat-7.6.0-x86_64.rpm

3.修改主配置文件该步骤同tar包,查询配置文件的路径可通过:

rpm -ql filebeat | more

配置文件目录:

/etc/filebea/filebeat.yml

修改配置文件

4.以系统服务方式启动filebeat

systemctl enable filebeat.service  --设置服务开机自启动
systemctl start filebeat.service     --启动服务

5.查看状态:

 service filebeat status 

在这里插入图片描述

三、重点Filebeat配置filebeat.yml

filebeat.inputs:
  # 多个后端服务,每个后端服务日志目录不同,给不同的日志目录设置不同的tag
  enabled: true
  paths:
    - /data/es/aaa.log
  tags: ["aaa-test"]
  fields:
    index: 'aaa-log'

- type: log
  # Change to true to enable this input configuration.
  enabled: true
  paths:
    - /data/es/bbb.log
  tags: ["bbb-test"]
  fields:
    index: 'bbb-log'

- type: log
  # Change to true to enable this input configuration.
  enabled: true
  paths:
    - /data/es/ccc.log
  tags: ["ccc-test"]
  fields:
    index: 'ccc-log'

  # json设置
  json.keys_under_root: true #filebeat会将日志进行json_decode处理
  json.overwrite_keys: true  #把filebeat默认的key值给覆盖了
  json.add_error_key: true #如果启用此设置,则在出现JSON解组错误或配置中定义了message_key但无法使用的情况下,Filebeat将添加“error.message”和“error.type:json”键。

#============================= 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
#==================== Elasticsearch template setting ==========================

setup.template.settings:
  index.number_of_shards: 1
  #index.codec: best_compression
  #_source.enabled: false
#------------add----------
setup.template.name: "log"
setup.template.pattern: "log-*"

#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  # Array of hosts to connect to. index: "btcfile-%{+yyyy.MM.dd}"    //索引
  hosts: ["xxxx:9200"]

  indices:
    # 为每个目录设置Elasticsearch索引,对应上面path的tag
    # {+yyyy.MM.dd} 自动获取当前的年月日
    - index: "aaa-log-%{+yyyy.MM.dd}"
      when.contains:
        fields: 
          index: 'aaa-log'

    - index: "bbb-log-%{+yyyy.MM.dd}"
      when.contains:
        fields: 
          index: 'bbb-log'

    - index: "ccc-log-%{+yyyy.MM.dd}"
      when.contains:
        fields: 
          index: 'ccc-log'
#================================ Processors =====================================

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

processors:
#需要删除的标签
- drop_fields:  
    fields: ["input_type", "log.offset", "host.name", "input.type", "agent.hostname", "agent.type", "ecs.version", "agent.ephemeral_id", "agent.id", "agent.version", "fields.ics", "log.file.path", "log.flags", "host.os.version", "host.os.platform","host.os.family", "host.os.name", "host.os.kernel", "host.os.codename", "host.id", "host.containerized", "host.hostname", "host.architecture"]

  

注意点:(1)服务器上有多个后端服务,每个服务对应的日志目录不同,为了区分项目的日志,在filebeat.yml中应为每个日志目录设置一个tag,对应到Elasticsearch中的索引。
(2)Filebeat 会给每条日志加上很多字段如 “input_type”, “log.offset”, “host.name”, “input.type”, “agent.hostname”, “agent.type”, “ecs.version”, “agent.ephemeral_id”, “agent.id”, “agent.version”, “fields.ics”, “log.file.path”, “log.flags”, “host.os.version”, “host.os.platform”,“host.os.family”, “host.os.name”, “host.os.kernel”, “host.os.codename”, “host.id”, “host.containerized”, “host.hostname”, "host.architecture"等,这些字段是不重要的,会干扰日志阅读,需要去除这些字段。

四、日志展示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38255219/article/details/106943766