Filebeat 是 Elastic Stack(ELK/EFK)中的轻量级日志采集器,通常部署在各个节点上用于采集日志并转发到 Elasticsearch、Logstash 或 Kafka。
一、Filebeat 使用
将应用日志、系统日志、容器日志采集后发送到:
Elasticsearch(推荐用于小规模部署,直接可视化)
Logstash(适合需要复杂解析的场景)
Kafka(用于消息队列异步处理)
二、安装 Filebeat
(以 CentOS 7 为例)
# 1. 导入 Elastic 公钥 rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch ##导入 Elastic 官方的软件签名公钥,用于验证安装包的完整性和可信性 # 2. 添加 Filebeat 源 cat >/etc/yum.repos.d/elastic.repo <<EOF [filebeat-7.x] name=Elastic repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF # 3. 安装 yum install -y filebeat # 4. 设置开机启动 systemctl enable filebeat###Filebeat 二进制安装 7.17.20
# 下载 wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.20-linux-x86_64.tar.gz # 解压 tar -xzf filebeat-7.17.20-linux-x86_64.tar.gz # 进入目录 cd filebeat-7.17.20-linux-x86_64 ###################### 可以直接执行: ./filebeat -e 也可以后台运行: nohup ./filebeat -e > filebeat.log 2>&1 &###创建filebeat的配置文件
#(1)创建工作目录 cd /xiaop/softwares/filebeat-7.17.20-linux-x86_64 mkdir config #(2)编写配置文件 [root@elk1 filebeat-7.17.20-linux-x86_64]# cat config/01-stdin-to-console.yaml # 配置filebeat的输入端 filebeat.inputs: # 指定输入端的类型为标准输入 - type: stdin # 指定filebeat的输出端为console output.console: # 表示输出的内容以漂亮的格式显示 pretty: true #(3)启动filebeat的实例 /etc/filebeat/config/01-stdin-to-console.yaml filebeat -e -c config/01-stdin-to-console.yaml #如果是自定义目录 filebeat -e -c /xiao/softwares/filebeat-7.17.20-linux-x86_64/config/01-stdin-to-console.yaml #然后输入点什么就可以了
三、配置filebeat连接ES集群
#(1)修改配置文件 [root@elk103.com filebeat-7.17.20-linux-x86_64]# cat config/24-log-to-es_tls.yaml filebeat.inputs: - type: log paths: - /tmp/xiaop-linux85/test.log output.elasticsearch: hosts: ["http://192.168.200.201:9200","http://192.168.200.202:9200","http://192.168.200.203:9200"] username: "elastic" password: "yinzhengjie" index: "xiaop-jiaoshi07-test" setup.ilm.enabled: false setup.template.name: "xiaop" setup.template.pattern: "xiaop-*" setup.template.overwrite: true setup.template.settings: index.number_of_shards: 3 index.number_of_replicas: 0 #(2)启动filebeat实例 [root@elk103.com filebeat-7.17.20-linux-x86_64]# filebeat -e -c config/24-log-to-es_tls.yaml
四、filbeat的input插件之tcp案例
#(1)编写配置文件 [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/02-tcp-to-console.yaml filebeat.inputs: # 指定类型为tcp - type: tcp # 定义tcp监听的主机和端口 host: 0.0.0.0:8888 # 指定filebeat的输出端为console output.console: # 表示输出的内容以漂亮的格式显示 pretty: true #(2)启动filebeat实例 [root@elk1 config]# filebeat -e -c /xiao/softwares/filebeat-7.17.5-linux-x86_64/config/02-tcp-to-console.yaml #3)客户端测试 [root@elk101.xiaop.com ~]# yum -y install nc telnet [root@elk101.xiaop.com ~]# telnet 192.168.200.203 8888 [root@elk101.xiaop.com ~]# echo "AAAAAAAAAAA" | nc 192.168.200.203 8888
五、filebeat的input插件之log案例
#(1)编写配置文件 [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/03-log-to-console.yaml filebeat.inputs: # 指定输入类型是log - type: log # 指定文件路径 paths: - /tmp/xiaop-linux85/*.log - /tmp/xiaop-linux85/*/*.json # 注意,两个*可以递归匹配 - /tmp/xiaop-linux85/**/*.exe # 指定filebeat的输出端为console output.console: # 表示输出的内容以漂亮的格式显示 pretty: true #(2)启动filebeat实例 [root@elk1 config]# filebeat -e -c /xiao/softwares/filebeat-7.17.5-linux-x86_64/config/03-log-to-console.yaml
六、input的通用字段案例
#filebeat input插件的通用字段(common options): - enabled: 是否启用该组件,有true和false,默认值为true。当设置为false时,表示该input组件不会被加载执行! - tags: 给每条数据添加一个tags标签列表。 - fields 给数据添加字段。 - fields_under_root 该值默认值为false,将自定义的字段放在一个"fields"的字段中。若设置为true,则将fields的KEY放在顶级字段中。 - processors: 定义处理器,对源数据进行简单的处理。
七、综合案例
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/04-input_common_options-to-console.yaml filebeat.inputs: - type: log paths: - /tmp/xiaop/*.log - /tmp/xiaop/*/*.json - /tmp/xiaop/**/*.exe # 是否启用该类型,默认值为true。 enabled: false - type: tcp enabled: true host: "0.0.0.0:8888" # 给数据打标签,会在顶级字段多出来多个标签 tags: ["peng","2025","xiaop"] # 给数据添加KEY-VALUE类型的字段,默认是放在"fields"中的 fields: school: xiaop class: 2025 classroom: peng ip: 219.141.136.10 port: 13306 # 若设置为true时,则将fields添加的自定义字段放在顶级字段中,默认值为false。 fields_under_root: true # 定义处理器,过滤指定的数据 processors: # 删除消息是以linux开头的事件(event) - drop_event: when: regexp: message: "^linux" # 消息包含error内容事件(event)就可以删除自定义字段或者tags。无法删除内置的字段. - drop_fields: when: contains: message: "error" fields: ["class","tags"] ignore_missing: false # 修改字段的名称 - rename: fields: # 源字段 - from: "school" # 目标字段 to: "学校" - from: "log" to: "日志" # 转换数据,将字段的类型转换对应的数据类型,并存放在指定的字段中,本案例将其放在"xiaop"字段中 - convert: fields: - {from: "ip", to: "xiaop._ip", type: "ip"} - {from: "port", to: "xiaop._port", type: "integer"} # 指定filebeat的输出端为console output.console: # 表示输出的内容以漂亮的格式显示 pretty: true [root@elk1 filebeat-7.17.5-linux-x86_64]#
八、使用filebeat采集nginx日志
(1)搭建nginx环境
1.1 添加yum源
cat > /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
#1.2 安装nginx
yum -y install nginx
systemctl start nginx
#(2)使用filebeat采集nginx日志
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/06-log_nginx-to-console.yaml
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
[root@elk1 config]# filebeat -e -c /xiao/softwares/filebeat-7.17.5-linux-x86_64/config/06-log_nginx-to-console.yaml
#浏览器测试
九、使用filebeat采集docker日志
#(1)安装docker 略 #(2)配置docker的镜像加速 略 [root@elk1 ~]# systemctl enable --now docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. #(3)下载nginx镜像 docker run -dp 88:80 --name mynginx --restart always nginx:1.22.1-alpine docker run -dp 89:8080 --name mytomcat --restart always tomcat:jre8-alpine #(3)使用filebeat采集容器日志 [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/11-docker-to-console.yaml filebeat.inputs: # 指定输入类型为docker类型 - type: docker # 指定容器的ID containers.ids: - '*' output.console: pretty: true [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/12-container-to-console.yaml filebeat.inputs: - type: container paths: - '/var/lib/docker/containers/*/*.log' # output.console: # pretty: true output.elasticsearch: hosts: ["http://192.168.200.201:9200","http://192.168.200.202:9200","http://192.168.200.203:9200"]
十、filebeat的input类型之filestream实战案例
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/13-filestream-to-console.yaml filebeat.inputs: # 指定类型为filestream,在7.16版本中已经弃用log类型 - type: filestream enabled: false paths: - /tmp/xiaop/jie.log - type: filestream enabled: false paths: - /tmp/xiaop/docker.json # 配置解析 parsers: # 配置json格式解析 - ndjson: # 将错误消息记录到error字段中 add_error_key: true # 如果解析的json格式字段和filebeat内置的顶级字段冲突,则覆盖,默认是不覆盖的。 overwrite_keys: true # 将message解析的字段放入一个自定义的字段下。若不指定该字段,则默认解析的键值对会在顶级字段. #target: xiaop - type: filestream enabled: false paths: - /tmp/xiaop/jie.log # 配置解析 parsers: - multiline: type: count count_lines: 3 - type: filestream enabled: true paths: - /tmp/xiaop/demo.log parsers: - multiline: type: count count_lines: 4 - ndjson: add_error_key: true overwrite_keys: true target: xiaop-demo output.console: pretty: true把解析后的日志直接输出到终端(stdout),不发送到 Elasticsearch,非常适合做调试
./filebeat -e -c config/13-filestream-to-console.yaml
####创建一个 test 文件###创建 /tmp/xiaop/demo.log {"timestamp":"2025-08-01T10:00:00Z", "level":"info", "message":"user login", "user":"admin"} {"timestamp":"2025-08-01T10:00:01Z", "level":"error", "message":"login failed", "user":"guest"}
十一、练习案例
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/14-ketanglianxi.yaml filebeat.inputs: - type: filestream enabled: true paths: - /tmp/xiaop/shopping.json parsers: - multiline: type: count count_lines: 7 - ndjson: add_error_key: true overwrite_keys: true output.elasticsearch: hosts: ["http://192.168.200.201:9200","http://192.168.200.202:9200","http://192.168.200.203:9200"] [root@elk1 filebeat-7.17.5-linux-x86_64]# #将数据写入到本地文件案例 [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/15-stdin-to-file.yaml filebeat.inputs: - type: stdin # 指定输出的类型为本地文件 output.file: # 指定文件存储的路径 path: "/tmp/xiaop" # 指定文件的名称 filename: stdin.log [root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/15-stdin-to-file.yaml #写入数据到ES集群 [root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/16-log-to-es.yaml filebeat.inputs: - type: filestream enabled: true paths: - /tmp/xiaop/shopping.json parsers: - multiline: type: count count_lines: 7 - ndjson: add_error_key: true overwrite_keys: true # 将日志输出到ES集群 output.elasticsearch: # 指定ES集群地址 hosts: - "http://192.168.200.201:9200" - "http://192.168.200.202:9200" - "http://192.168.200.203:9200" # 指定索引 index: "xiaop-shopping-%{+yyyy.MM.dd}" # 禁用索引声明管理周期,若不禁用则自动忽略自定义索引名称 setup.ilm.enabled: false # 设置索引模板的名称 setup.template.name: "xiaop-shopping" # 指定索引模板的匹配模式 setup.template.pattern: "xiaop-shopping-*" # 是否覆盖原有的索引模板 setup.template.overwrite: true # 设置索引模板 setup.template.settings: # 指定分片数量为8 index.number_of_shards: 8 # 指定副本数量为0 index.number_of_replicas: 0
十二、将多个数据源写入到ES集群不同索引
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat config/17-filestream-to-es.yaml filebeat.inputs: - type: filestream enabled: true tags: "docker" paths: - /tmp/xiaop/docker.json parsers: - ndjson: add_error_key: true #overwrite_keys: true - type: filestream enabled: true tags: "2025" paths: - /tmp/xiaop/2025.log parsers: - multiline: type: count count_lines: 3 ##每 3 行日志合并为 1 条事件 - type: filestream enabled: true tags: "demo" paths: - /tmp/xiaop/demo.log parsers: - multiline: type: count count_lines: 4 ##每 4 行日志合并为 1 条 - ndjson: add_error_key: true overwrite_keys: true target: xiaop-demo output.elasticsearch: hosts: - "http://192.168.200.201:9200" - "http://192.168.200.201:9200" - "http://192.168.200.201:9200" # index: "xiaop-shopping-%{+yyyy.MM.dd}" indices: - index: "xiaop--docker-%{+yyyy.MM.dd}" when.contains: tags: "docker" - index: "xiaop-7-%{+yyyy.MM.dd}" when.contains: tags: "2025" - index: "xiaop-demo-%{+yyyy.MM.dd}" when.contains: tags: "demo" setup.ilm.enabled: false setup.template.name: "xiaop" setup.template.pattern: "xiaop-*" setup.template.overwrite: true setup.template.settings: index.number_of_shards: 3 index.number_of_replicas: 0 [root@elk1 filebeat-7.17.5-linux-x86_64]# filebeat -e -c config/17-filestream-to-es.yaml
####有待补充。。。。



2743

被折叠的 条评论
为什么被折叠?



