elk笔记10--filebeat使用

1 filebeat 介绍

Filebeat 是一个用于转发和集中化日志数据的轻量级工具,它一般作为agent安装在服务器上。它监控日志文件和文件夹,收集日志事件,并转发到logstash、ES或者Kafka。
Filebeat的流程图如下,当filebeat运行后,其根据指定的log日志位置,启动一个或者多个input。 对于每一个定位跟踪的日志,filebeat都为启动一个独特的harvester(收割机), harvester会读取一条新日志内容,并将其发送到libbeat, libbeat收集一些列的事件,并将收集的日志发送到对应的output。
Filebeat

2 filebeat 使用案例

2.1 软件安装

  1. 下载filebeat bin文件,解压即可用
    curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.8.0-linux-x86_64.tar.gz
    tar xzvf filebeat-7.8.0-linux-x86_64.tar.gz
  2. 通过deb包安装
    curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.8.0-amd64.deb
    sudo dpkg -i filebeat-7.8.0-amd64.deb

2.2 采集数据到 kafka

  1. 配置yml
    # https://www.elastic.co/guide/en/beats/filebeat/index.html
    默认向elasticsearch写数据,需要将elasticsearch注释掉。
    - type: log
      enabled: true #此处必须为true否则不生效
      paths:
        - /var/log/syslog
    output.kafka:
      topic: 'syslog001'
      hosts: ["10.120.75.102:9092", "10.120.75.103:9092", "10.120.75.107:9092"]
      enabled: true
      codec.format:
        string: '%{[message]}'
      partition.round_robin:
        reachable_only: false
      required_acks: 1                
      compression: gzip
      max_message_bytes: 1000000 
    
  2. 创建对应topic:
    bin/kafka-topics.sh --create --zookeeper 10.120.75.102:2181 --replication-factor 1 --partitions 1 --topic syslog001
    Created topic syslog001.
  3. 创建对应rollover索引
    PUT /%3Csyslog001-%7Bnow%2Fd%7D-000001%3E
    {
      "aliases": {
      "syslog001_write": {}
      }
    }
    
  4. 启动filebeat:
    ./filebeat [-e] -c filebeat.yml &
    -e 可以省略
  5. kafka查看是否收到数据:
    $ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 [–from-beginning] --topic syslog001
    Jul 5 20:45:01 xg kernel: [5108113.014561] sadc[7456]: segfault at 1 ip 00007f7837f2051f sp 00007ffd072c92e8 error 4 in libc-2.24.so[7f7837e96000+195000]
    [–from-beginning]无该选项则消费最新的数据,有该选项则从头开始消费。
  6. 查看数据是否到es
    若配置了kafka采集数据的话,可以直接在es中查看数据是否到es,具体配置方法见:elk笔记5–logstash使用
    GET syslog001_write/_count 或者_serach 查看具体数据

2.3 采集数据到 es

  1. 配置yml
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /var/log/dmesg
    output.elasticsearch:
     # Array of hosts to connect to.
        hosts: ["http://10.120.75.102:9201","http://10.120.75.103:9201","http://10.120.75.107:9201"]
        indices:
          - index: "dmesg001_write"
    # setup 必有,否则会出错
    setup.template.name: "*" # 这里也可以更换为实际莫个模板
    setup.template.pattern: "dmesg001-*"
    
  2. 创建对应rollover索引
    PUT /%3Cdmesg001-%7Bnow%2Fd%7D-000001%3E
    {
      "aliases": {
      "dmesg001_write": {}
      }
    }
    
  3. 启动filebeat:
    ./filebeat [-e] -c filebeat.yml &
  4. 查看数据是否写到es
    GET dmesg001_write/_count 或者_serach 查看具体数据
    由于数据直接写到es,因此不需要第三方采集,可以直接在es中查看数据

3 使用技巧

3.1 filebeat 将日志按照类别发送到不同 kafka topic

有时候需要使用一个filebeat将不同类别的日志发送到不同的topic中,此时可以在inputs中添加fields: log_topic 字段,字段值设置为不同的topic名称即可,然后在 output中 通过topic: '%{[fields.log_topic]}'设置其对应topic,此时启动filebeat后,日志将发送到不通的topic中,以下分别发送到dmesg001和syslog001两个topic中。

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:
    - /var/log/syslog
    #- c:\programdata\elasticsearch\logs\*
  fields:
    log_topic: syslog001
- type: log
  enabled: true
  paths:
    - /var/log/dmesg
  fields:
    log_topic: dmesg001

output.kafka:
    # topic: 'syslog001'
  topic: '%{[fields.log_topic]}'
  hosts: ["10.120.75.102:9092", "10.120.75.103:9092", "10.120.75.107:9022"]
  enabled: true
  codec.format:
    string: '%{[message]}'
  partition.round_robin:
  reachable_only: false
  required_acks: 1                
  compression: gzip
  max_message_bytes: 1000000   

3.2 filebeat将日志按照类别发送到不同es index

对于多个不同类别的日志,若希望采集到不同的index中,则可以分别为每类日志创建一个- type: log, 并设置对应的fields.type,后面在output中通过fields.type 来判断属于哪个索引。
具体配置如下:

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/dmesg
  fields: 
    type: 'dmesg001'
- type: log
  enabled: true
  paths:
    - /var/log/syslog
  fields:
    type: 'syslog001'

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  indices:
    - index: "syslog001_write"
      when.contains:
        fields:
          type: 'syslog001'
    - index: "dmesg001_write"
      when.contains:
        fields:
          type: 'dmesg001'
          
setup.template.name: "base-settings"
setup.template.pattern: "*"

最终, 写入kibana的字段会多一个fields.type,syslog001-* 的全部为syslog001,dmesg001-* 的全部为dmesg001

4 说明

  1. 测试软件: elasticsearch版本为7.2.1, filebeat为7.8.0
  2. 参考文献: elastic 官网–filebeat/current/index
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,可以看出以下是关于Docker Compose配置ELK(Elasticsearch、Logstash和Kibana)和Filebeat的步骤: 1. 首先,拉取所需的镜像,包括Elasticsearch、Kibana、Logstash和Filebeat。例如,使用以下命令拉取特定版本的镜像: ``` docker pull elasticsearch:7.16.1 docker pull kibana:7.16.1 docker pull logstash:7.16.1 docker pull elastic/filebeat:7.16.1 ``` 2. 创建一个自定义网络,以便ELK容器可以相互通信。例如,使用以下命令创建一个名为"somenetwork"的网络: ``` docker network create somenetwork ``` 3. 配置Elasticsearch。根据提供的引用\[2\],可以在指定的目录(例如`/mydata/elk/elasticsearch/conf`)中创建一个配置文件(例如`elasticsearch.yml`),并根据需要进行配置。例如,设置集群名称、主机IP和允许跨域等。 4. 配置Kibana。根据提供的引用\[2\],可以在指定的目录(例如`/mydata/elk/kibana/conf`)中创建一个配置文件(例如`kibana.yml`),并根据需要进行配置。例如,设置服务器名称、主机IP和与Elasticsearch的连接等。 5. 配置Logstash。根据提供的引用\[3\],可以在指定的目录(例如`/mydata/elk/logstash/conf`)中创建一个配置文件(例如`logstash.yml`),并根据需要进行配置。例如,设置集群名称、主机IP和允许跨域等。 6. 配置Filebeat。根据提供的引用\[1\],可以在指定的目录(例如`/mydata/elk/filebeat/conf`)中创建一个配置文件(例如`filebeat.yml`),并根据需要进行配置。例如,设置Elasticsearch的主机IP和端口等。 请注意,以上步骤仅提供了基本的配置示例,实际配置可能因环境和需求而有所不同。您可以根据自己的需求进行相应的配置。 #### 引用[.reference_title] - *1* *2* *3* [docker-compose配置elk + filebeat(版本:7.16.1)](https://blog.csdn.net/paidaxinga_/article/details/122218054)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值