ELFK搭建教程

279 阅读2分钟

前文介绍了 基于 Docker 的 ELK 搭建教程 。本文在此基础上,介绍ELFK的搭建教程。

搭建 Filebeat

普通安装:

  1. 下载 Filebeat
  2. 上传并解压 filebeat-6.4.0-linux-x86_64.tar.gz
  3. 修改 filebeat.yml
  4. 界面启动: ./filebeat -e -c filebeat.yml
    后台启动: ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &
    解决长时间不运行而自动退出问题:nohup ./filebeat -e -c filebeat.yml -d "Publish" &
    停止运行(filebeat没有监听端口,主要看日志和进程):ps -ef | grep filebeat ,then kill -9  [进程号]

Docker 安装:

docker pull elastic/filebeat:6.4.0

配置 filebeat.yml 文件

1.配置监听日志文件的位置,进行inputs

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

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

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

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
     - /data/resource/resource.log
  fields:
    log_type: "info"

 # json.key_under_root: true
 #  json.overwirte_keys: true

- type: log
  enabled: true
  paths:
    - /data/resource/resourceError.log
  fields:
    log_type: "error"

  #合并多行显示
  multiline.pattern: '^\d{4}-\d{1,2}-\d{1,2}\s\d{2}\:\d{2}\:\d{2}\.\d{3}'     #匹配值 2022-03-14 21:13:35.112
  multiline.negate: true  # true,不匹配 pattern 的行合并到上一行;false,匹配 pattern 的行合并到上一行。默认为 false
  multiline.match: after  # after,为匹配合并到上一行的末尾;before,为匹配合并到上一行的开头
  multiline.timeout: 30s  # 多行匹配超时时间,超过超时时间后的当前多行匹配事件将停止并发送,然后开始一个新的多行匹配事件,默认5秒
  #tail_files: true        # 从文件尾开始监控文件新增内容。默认为false。
  #fields_under_root: true
  ……

2.配置 output 到logstash地址

image.png

修改 logstash.conf 文件

cd /usr/local/docker/logstash

input {
  beats {
    port => 4560
    client_inactivity_timeout => 36000
#    codec => multiline {          # 合并多行,此处行不通
#       pattern => "^%{YEAR}%{MONTHNUM}%{MONTHDAY}[ ]%{TIME}"
#       negate => true
#       what => "previous"
#    }
  }
}
output {
  if [fields][log_type] == "info" {
    elasticsearch {
      hosts => "es:9200"
      index => "elfk-info-%{+YYYY.MM.dd}"
    }
  }
  if [fields][log_type] == "error" {
    elasticsearch {
      hosts => "es:9200"
      index => "elfk-error-%{+YYYY.MM.dd}"
    }
  }
}

ELK 收集 springboot log4j2 日志

  1. 引入log4j2 jar包
  2. 编写log4j2.xml文件(日志写入到日志文件中,filebeat会读取日志文件内容)

参考文章

www.cnblogs.com/vipsoft/p/1…
juejin.cn/post/699393…