🌟Filebeat采集数据的原理
Filebeat默认按行采集数据,如果数据没有换行,则该条数据无法采集到
属于有状态服务,可以记录上一次采集数据的位置点信息
修改配置文件
vim /etc/filebeat/config/03-log-to-console.yaml
filebeat.inputs:
- type: logpaths:- /tmp/student.jsonoutput.console:pretty: true
启动Filebeat
filebeat -e -c /etc/filebeat/config/03-log-to-console.yaml
发送测试数据
echo haha >> /tmp/student.json
offset记录的是上一次采集数据的位置点,haha是第一行,所以offset偏移量是0
echo www >> /tmp/student.json[root@elk92 ~]# tail -1 /var/lib/filebeat/registry/filebeat/log.json
{"k":"filebeat::logs::native::1703963-2052","v":{"id":"native::1703963-2052","source":"/tmp/student.json","timestamp":[45803107,1756344477],"identifier_name":"native","FileStateOS":{"inode":1703963,"device":2052},"prev_id":"","offset":0,"ttl":-1,"type":"log"}}
echo -n .zhubl. >> /tmp/student.json
echo xyz >> /tmp/student.json
🌟Filebeat采集nginx日志
安装nginx
apt -y install nginx
systemctl start nginx
ss -ntl | grep 80
访问nginx测试
http://10.0.0.92
编写Filebeat采集nginx日志
vim /etc/filebeat/config/04-nginx-to-es.yaml
filebeat.inputs:
- type: logpaths:- /var/log/nginx/access.log*#output.console:
# pretty: true# 将数据写入到ES集群
output.elasticsearch:# 指定ES集群地址hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]# 指定ES的索引名称index: zhu-nginx# 禁用索引的生命周期,否则自定义索引名称无效
setup.ilm.enabled: false
# 定义索引模板
setup.template.name: "zhu-nginx"
# 定义索引模板的匹配模式
setup.template.pattern: "zhu-nginx*"
# 如果索引模板已经存在是否覆盖
setup.template.overwrite: false
# 配置索引模板
setup.template.settings:# 指定分片数量index.number_of_shards: 3# 指定副本数量index.number_of_replicas: 0
启动Filebeat
filebeat -e -c /etc/filebeat/config/04-nginx-to-es.yaml
Kibana查看数据
🌟Filebeat采集tomcat日志案例
下载tomcat
官网下载tomcat:https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.10/bin/apache-tomcat-11.0.8.tar.gz
wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.10/bin/apache-tomcat-11.0.8.tar.gz
解压软件包
tar xf apache-tomcat-11.0.8.tar.gz -C /usr/local/
添加环境变量
1.编辑环境变量配置文件
vim /etc/profile.d/tomcat.sh
#!/bin/bash
export TOMCAT_HOME=/usr/local/apache-tomcat-11.0.8
export JAVA_HOME=/usr/share/elasticsearch/jdk
export PATH=$PATH:$TOMCAT_HOME/bin:$JAVA_HOME/bin2.加载环境变量配置文件
source /etc/profile.d/tomcat.sh
启动tomcat
startup.sh
浏览器访问
http://10.0.0.93:8080/
编写Filebeat的配置文件
vim /etc/filebeat/config/05-tomcat-to-es.yml
filebeat.inputs:
- type: logpaths:- /usr/local/apache-tomcat-11.0.8/logs/localhost_access_log.*.txt#output.console:
# pretty: true# 将数据写入到ES集群
output.elasticsearch:# 指定ES集群地址hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]# 指定ES的索引名称index: zhu-tomcat# 禁用索引的生命周期,否则自定义索引名称无效
setup.ilm.enabled: false
# 定义索引模板
setup.template.name: "zhu-tomcat"
# 定义索引模板的匹配模式
setup.template.pattern: "zhu-tomcat*"
# 如果索引模板已经存在是否覆盖
setup.template.overwrite: false
# 配置索引模板
setup.template.settings:# 指定分片数量index.number_of_shards: 3# 指定副本数量index.number_of_replicas: 0
启动Filebeat
filebeat -e -c /etc/filebeat/config/06-tomcat-to-es.yaml
Kibana查看数据
🌟Filebeat多实例
如果在工作中需要启动多个Filebeat实例,则可以使用" --path.data"参数指定不同的数据目录,若目录不存在则会自动创建
如果工作中Filebeat停止可以使用kill命令操作来停止服务
启动实例一
filebeat -e -c /etc/filebeat/config/04-nginx-to-es.yml
启动实例二
filebeat -e -c /etc/filebeat/config/05-tomcat-to-es.yaml --path.data /tmp/xixi
查看进程信息
ps -ef | grep filebeat
🌟Filebeat采集多种业务日志
编写Filebeat配置文件
vim /etc/filebeat/config/06-multiple_input-to-es.yaml
filebeat.inputs:
- type: logpaths:- /var/log/nginx/access.log*tags: "nginx"
- type: logpaths:- /usr/local/apache-tomcat-11.0.8/logs/localhost_access_log.*.txttags: "tomcat"output.elasticsearch:hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]# 根据条件判断,将数据写入不同的索引indices:# 指定索引的名称- index: "zhu-contains-nginx-%{+yyyy.MM.dd}"# 指定匹配条件when.contains:tags: "nginx"- index: "zhu-contains-tomcat-%{+yyyy.MM.dd}"when.contains:tags: "tomcat"setup.ilm.enabled: false
setup.template.name: "zhu-contains"
setup.template.pattern: "zhu-contains*"
setup.template.overwrite: false
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 0
启动实例
filebeat -e -c /etc/filebeat/config/06-multiple_input-to-es.yaml
kibana出图展示
🌟Filebeat采集docker日志
安装Filebeat
dpkg -i filebeat-7.17.29-amd64.deb
编写Filebeat配置文件
vim /etc/filebeat/docker-to-es.yaml
filebeat.inputs:
- type: containerpaths: - '/var/lib/docker/containers/*/*.log'# 添加处理器。
processors:# 添加docker的元数据信息。- add_docker_metadata:# 找本地的套接字文件。host: "unix:///var/run/docker.sock"output.elasticsearch:hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]index: zhu-dockersetup.ilm.enabled: false
setup.template.name: "zhu-docker"
setup.template.pattern: "zhu-docker*"
setup.template.overwrite: false
setup.template.settings:index.number_of_shards: 6index.number_of_replicas: 0
启动实例
filebeat -e -c /etc/filebeat/docker-to-es.yaml
kibana查询
🌟Filebeat排除不必要的行exclude_lines
编写Filebeat配置文件
vim /etc/filebeat/docker-to-es.yaml
filebeat.inputs:
- type: containerpaths: - '/var/lib/docker/containers/*/*.log'# 排除不必要的行。exclude_lines: ['.*notice','.*entrypoint','.*listen']# 添加处理器。
processors:# 添加docker的元数据信息。- add_docker_metadata:# 找本地的套接字文件。host: "unix:///var/run/docker.sock"output.elasticsearch:hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]index: zhu-dockersetup.ilm.enabled: false
setup.template.name: "zhu-docker"
setup.template.pattern: "zhu-docker*"
setup.template.overwrite: false
setup.template.settings:index.number_of_shards: 6index.number_of_replicas: 0
启动Filebeat实例
filebeat -e -c /etc/filebeat/docker-to-es.yaml
Kibana出图展示
🌟Kibana分析容器的相关指标
创建可视化
选择基于聚合
选择指标
选择docker
计数显示记录的日志总数
显示容器的数量
🌟Filebeat模块应用案例
启用模块
filebeat modules enable nginx tomcat
修改nginx的模块文件
vim /etc/filebeat/modules.d/nginx.yml
- module: nginxaccess:enabled: truevar.paths: ["/var/log/nginx/access.log*"]error:enabled: truevar.paths: ["/var/log/nginx/error.log*"]ingress_controller:enabled: false
修改tomcat的模块文件
vim /etc/filebeat/modules.d/tomcat.yml
- module: tomcatlog:enabled: truevar.input: filevar.paths:- /usr/local/apache-tomcat-11.0.8/logs/localhost_access_log.*.txt
修改Filebeat的配置文件
vim /etc/filebeat/config/07-modules-to-es.yaml
# 启用Filebeat模块
filebeat.config.modules:# 指定模块的路径path: ${path.config}/modules.d/*.yml# 是否支持热加载配置reload.enabled: trueoutput.elasticsearch:hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]index: zhu-modules-xixisetup.ilm.enabled: false
setup.template.name: "zhu-modules"
setup.template.pattern: "zhu-modules*"
setup.template.overwrite: false
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 0
启动Filebeat实例
filebeat -e -c /etc/filebeat/config/07-modules-to-es.yaml
🌟基于Filebeat的模块分析nginx访问日志
Filebeat模块配置
vim /etc/filebeat/modules.d/nginx.yml
- module: nginxaccess:enabled: truevar.paths: ["/root/access.log*"]error:enabled: falsevar.paths: ["/var/log/nginx/error.log*"]ingress_controller:enabled: false
编写Filebeat配置文件
vim /etc/filebeat/config/08-modules_nginx-to-es.yaml
filebeat.config.modules:path: ${path.config}/modules.d/nginx.ymlreload.enabled: trueoutput.elasticsearch:hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]index: zhu-modules-efk-nginxsetup.ilm.enabled: false
setup.template.name: "zhu-modules"
setup.template.pattern: "zhu-modules*"
setup.template.overwrite: false
setup.template.settings:index.number_of_shards: 3index.number_of_replicas: 0
启动Filebeat实例
filebeat -e -c /etc/filebeat/config/08-modules_nginx-to-es.yaml
Kibana出图分析指标
wc -l /root/access.log