安装包准备
filebeat-7.17.5-linux-x86_64.tar.gz
官网链接:https://www.elastic.co/cn/downloads/
软件安装
创建目录
install -d /softwares
install -d /logs/filebeat
install -d /data/filebeat
install -d /softwares/filebeat/config
安装filebeat
tar xf filebeat-7.17.5-linux-x86_64.tar.gz -C /softwares/
创建软链接
ln -s /softwares/filebeat-7.17.5-linux-x86_64/ /softwares/filebeat
编写配置文件
收集nginx、tomcat日志到elasticsearch
这里收集的日志都已转换为json格式,格式转换详情见基于jpress的云上个人博客搭建
vim /softwares/filebeat/config/nginx\&tomcat-to-elasticsearch.yml
filebeat.inputs:
- type: log
paths:
- /logs/nginx/access*.log
tags: ["nginx_access"]
json.keys_under_root: true
json.add_error_key: true
- type: log
paths:
- /logs/nginx/error*.log
tags: ["nginx_error"]
json.keys_under_root: true
json.add_error_key: true
- type: log
paths:
- /logs/tomcat/jpress_access_log.*.txt
tags: ["tomcat_access"]
json.keys_under_root: true
json.add_error_key: true
- type: log
paths:
- /logs/tomcat/catalina.*.log
tags: ["tomcat_error"]
json.keys_under_root: true
json.add_error_key: true
output.elasticsearch:
hosts:
- "http://localhost:9200"
# 指定多个索引,使用when语句
indices:
- index: "gyang.cloud-nginx_access-%{+yyyy.MM.dd}"
when.contains:
tags: "nginx_access"
- index: "gyang.cloud-nginx_error-%{+yyyy.MM.dd}"
when.contains:
tags: "nginx_error"
- index: "gyang.cloud-tomcat_access-%{+yyyy.MM.dd}"
when.contains:
tags: "tomcat_access"
- index: "gyang.cloud-tomcat_error-%{+yyyy.MM.dd}"
when.contains:
tags: "tomcat_error"
setup.ilm.enabled: false
# 设置索引目标的名称
setup.template.name: "gyang.cloud"
setup.template.pattern: "gyang.cloud*"
setup.template.overwrite: true
setup.template.settings:
index.number_of_shards: 1
index.number_of_replicas: 1
启动filebeat测试日志收集
临时启动filebeat,指定配置文件
./filebeat -e -c config/nginx\&tomcat-to-elasticsearch.yml
访问kibana(kibana部署详见elastic之部署kibana)
Stack Management ---> 索引管理
能看到索引说明filebeat收集日志是成功的
注意:索引的运行状况如果为黄色和红色,说明配置文件有错误。
ES集群有几种颜色,分别代表:
- 绿色(GREEN)
代表ES集群的所有主分片和副本分片处于健康状态(可访问)。
- 黄色(YELLLOW)
代表ES集群的部分副本分片无法访问。
- 红色(RED)
代表ES集群的部分主分片无法访问。一般情况下会在ES集群启动时短暂出现该状态。
systemctl管理filebeat
编辑/etc/systemd/system/filebeat.service
vim /etc/systemd/system/filebeat.service
[Unit]
Description=Filebeat Server
After=network.target remote-fs.target nss-lookup.target
[Service]
User=root
Group=root
Environment="BEAT_CONFIG_OPTS=-c /softwares/filebeat/config/nginx&tomcat-to-elasticsearch.yml"
Environment="BEAT_PATH_OPTS=--path.home /softwares/filebeat --path.data /data/filebeat --path.logs /logs/filebeat"
ExecStart=/softwares/filebeat/filebeat $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS
Restart=always
[Install]
WantedBy=multi-user.target
启动filebeat
systemctl daemon-reload
systemctl start filebeat
systemctl enable filebeat.service
systemctl status filebeat.service.