写这篇文章时,filebeat官方当前版本已经到了7.8,所以以下关于filebeat的使用和介绍都是在这个版本下进行的。操作系统是CentOS 6/7。
最开始使用filebeat大概是2015年,当时filebeat简单的用来读取日志,写入缓存,再通过logstash来进行解析,输出到elasticsearch。最近开始重新使用filebeat,功能已经大大的丰富了,集成封装的功能可以直接跟elasticsearch和kibana进行交互,同时集成了很多应用的日志模块,做到了开箱即用,更适合这个云和容器的时代。
但对我来说,目前的需求跟几年前并没有多大变化,只是为了读取日志,日志字段越简洁越好,不需要有复杂的信息来进行更为复杂的分析计算,所以这篇文章只介绍filebeat比较“原始”的一些使用方式。
filebeat 安装
安装很简单,直接使用官方仓库安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
cat > /etc/yum.repos.d/filebeat.repo <<EOF [elastic-7.x] name=Elastic repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF
yum install filebeat
|
如果使用官方yum仓库下载太慢,华为云有镜像,速度不错,地址如下:
https://mirrors.huaweicloud.com/filebeat/
Filebeat 7.8下载地址:
https://mirrors.huaweicloud.com/filebeat/7.8.0/filebeat-7.8.0-x86_64.rpm
filebeat 配置
因为整个日志分析系统中加了一层kafka缓存,filebeat是写入kafka,而不是直接写入elasticsearch,所以关于elasticsearch template的配置(setup.template.settings)和kibana dashboard的配置(setup.kibana)都不需要更改(去掉也可以)。也没有使用集成的module,所以也不需要通过 filebeat
命令去启用其他module。/etc/filebeat/filebeat.yml 配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| filebeat.inputs: - type: log enabled: true paths: - /var/log/nginx/access.log tags: ["nginx"]
filebeat.config.modules: path: ${path.config}/modules.d/*.yml reload.enabled: false
output.kafka: hosts: ["kafka-broker-list"] version: 1.1.1 topic: filebeat partition.round_robin: reachable_only: false required_acks: 1 compression: lz4 max_message_bytes: 1000000
processors:
|
日志会增加 tags 字段,用来区分不同应用日志,同时去掉 processors 的配置,以免产生过多的无用字段。
配置完成后启动filebeat:
1
| systemctl start filebeat
|
filebeat 输出
通过 kafka-console-consumer.sh
去消费kafka,filebeat输出的内容如下,根据这些内容后续就可以通过logstash来解析日志了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| { "@timestamp": "2020-07-16T06:44:53.494Z", "@metadata": { "beat": "filebeat", "type": "_doc", "version": "7.8.0" }, "log": { "offset": 261308944, "file": { "path": "/var/log/nginx/access.log" } }, "message": "103.213.96.50 - - [16/Jul/2020:14:44:53 +0800] \"GET /api/xxxxxxx.jsp HTTP/1.1\" 200 26697 2.964 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)\" \"-\" \"-\" www.example.com \"1.2.3.4:8080\" \"200\" \"2.954\"", "tags": [ "nginx" ], "input": { "type": "log" }, "ecs": { "version": "1.5.0" }, "host": { "name": "host1" }, "agent": { "type": "filebeat", "version": "7.8.0", "hostname": "host1", "ephemeral_id": "aa60042e-a650-4ce3-9024-c1e1893b2f2a", "id": "cac3f85e-5589-49ad-bd52-3cf50ddae917", "name": "host1" } }
|