一、文档介绍
本文档详细描述 Filebeat 部署步骤,以及描述常用类型日志的配置采集过程。
二、部署说明
- 操作系统:
Linux(CentOS 7.6) - 安装包版本:
Filebeat 8.15.2
三、下载
官网下载地址:https://artifacts.elastic.co/downloads/beats/filebeat/
cd /usr/local/srcwget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.15.2-linux-x86_64.tar.gz
四、安装
cd /usr/local/srctar xzvf filebeat-8.15.2-linux-x86_64.tar.gzmv filebeat-8.15.2-linux-x86_64 /usr/local/filebeat-8.15.2cd /usr/local/ln -s filebeat-8.15.2 filebeat
五、创建目录
mkdir -p /data/filebeat_data/{conf,data,tmp}cd /usr/local/filebeat# 配置文件mv filebeat.yml /data/filebeat_data/conf/ln -s /data/filebeat_data/conf/filebeat.yml /usr/local/filebeat/filebeat.yml# 日志目录mkdir -p /data/logs/filebeatln -s /data/logs/filebeat /usr/local/filebeat/logs# 数据目录ln -s /data/filebeat_data/data /usr/local/filebeat/data# modules.d目录mv modules.d /data/filebeat_data/ln -s /data/filebeat_data/modules.d /usr/local/filebeat/modules.d
六、创建服务
cat <<"EOF" | tee /usr/lib/systemd/system/filebeat.service[Unit]Description=Filebeat sends log files to Logstash or directly to Elasticsearch.Documentation=https://www.elastic.co/products/beats/filebeatWants=network-online.targetAfter=network-online.target[Service]WorkingDirectory=/usr/local/filebeatEnvironment="BEAT_CONFIG_OPTS=-c /usr/local/filebeat/filebeat.yml"ExecStart=/usr/local/filebeat/filebeat --environment systemd $BEAT_CONFIG_OPTSRestart=always[Install]WantedBy=multi-user.targetEOF
七、基础配置
配置文件路径:/usr/local/filebeat/filebeat.yml。
path.home: /usr/local/filebeatpath.config: /usr/local/filebeatpath.data: /usr/local/filebeat/datapath.logs: /usr/local/filebeat/logslogging.level: infologging.to_files: truelogging.files:path: /usr/local/filebeat/logsname: filebeat.logkeepfiles: 7permissions: 0644filebeat.config.modules:path: /usr/local/filebeat/modules.d/*.ymlreload.enabled: truereload.period: 10s
八、采集配置
配置文件路径:/usr/local/filebeat/filebeat.yml。以下场景请根据实际场景进行挑选:
(一) 采集配置: 文本日志
filebeat.inputs:- type: logenabled: truepaths:- /var/log/history.logprocessors:- add_fields:target: logfields:type: "linux-history"
(二) 采集配置: java 日志
filebeat.inputs:- type: logenabled: truepaths:- /data/bizlogs/tomcat/catalina.outencoding: gbkprocessors:- add_fields:target: logfields:type: "java"app_name: "tomcat"multiline.pattern: '^(\d\d){1,2}-(?:0[1-9]|1[0-2])-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]) (?:2[0123]|[01]?[0-9]):(?:[0-5][0-9]):(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)'multiline.negate: truemultiline.match: after- type: logenabled: truepaths:- /data/bizlogs/java/*/*.logprocessors:- add_fields:target: logfields:type: "java"app_name: "jar"multiline.pattern: '^(\d\d){1,2}-(?:0[1-9]|1[0-2])-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]) (?:2[0123]|[01]?[0-9]):(?:[0-5][0-9]):(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)'multiline.negate: truemultiline.match: after
(三) 采集配置: mysql 日志
启用 mysql module,并添加配置。
cd /usr/local/filebeat/echo '# Module: mysql# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.17/filebeat-module-mysql.html- module: mysqlerror:enabled: truevar.paths: ["/data/logs/mysql/3306/error.log*"]slowlog:enabled: truevar.paths: ["/data/logs/mysql/3306/slow.log*"]' > modules.d/mysql.yml./filebeat modules enable mysql./filebeat modules list
(四) 采集配置: docker 日志
filebeat.autodiscover:providers:- type: dockercontainers:path: "/var/lib/docker/containers/"hints.enabled: true# 选项为true,默认所有容器开启采集# 选项为false,配置了co.elastic.logs/enabled: true的容器才开启采集hints.default_config.enabled: false
(五) 采集配置: k8s 日志
filebeat.autodiscover:providers:- type: kubernetesadd_resource_metadata:deployment: truehints.enabled: truehints.default_config:type: containerpaths:- /var/log/containers/*.log # CRI pathprocessors:- add_fields:target: ""fields:log_type: "k8slog"multiline:pattern: ^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2} #匹配Java日志开头时间negate: true # 正则是否开启,默认false不开启match: after
(六) 采集配置: 自定义解析日志
filebeat.inputs:- type: containerenabled: truepaths:- /var/log/containers/*.logprocessors:- script:lang: javascriptid: k8s_metadatasource: >function process(event) {event.Tag("js");var path = event.Get('log.file.path');var file_name = path.split('/')[4]var pod_str = file_name.split('.')[0]var pod_info = pod_str.split('_')var container_info = pod_info[pod_info.length-1].split('-')event.Put('k8s.pod_name', pod_info[0])event.Put('k8s.namesapce_name', pod_info[1]);event.Put('k8s.container_name', container_info.slice(0,-1).join('-'));event.Put('k8s.container_id', container_info[container_info.length-1]);}
九、输出配置
(一) 输出配置: elasticsearch
output.elasticsearch:hosts: ["http://127.0.0.1:9200"]username: "elastic"password: "YOUR_PASSWORD"
(二) 输出配置: logstash
output.logstash:hosts:- "127.0.0.1:5044"
(三) 输出配置: kafka
output.kafka:hosts: ['127.0.0.1:9092']topic: "mysql-slow-log"
十、启动服务
systemctl daemon-reloadsystemctl enable filebeat --now
夜雨聆风