linux安装filebeat并收集日志到elasticsearch

摘要:

通过filebeat收集服务器上各个应用的日志到elasticsearch,通过tags区分不同的应用创建不同的索引保存日志。

官网地址:

https://www.elastic.co/cn/downloads/past-releases#filebeat

安装步骤:

1:下载并解压(以7.9.3版本为例)

bash 复制代码
cd /usr/local/src
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.9.3-linux-x86_64.tar.gz
tar -zxvf filebeat-7.9.3-linux-x86_64.tar.gz

2:修改配置文件filebeat.yml

bash 复制代码
cd filebeat-7.9.3-linux-x86_64
vi filebeat.yml

filebeat.inputs:

  • type: log

enabled: true

paths:

  • /logs/app1*.log

tags: ["app1"]

  • type: log

enabled: true

paths:

  • /logs/app2*.log

tags: ["app2"]

output.elasticsearch:

hosts: ["172.19.12.3:9200"]

protocol: "http"

username: "elastic"

password: "123456"

indices:

  • index: "app1-%{+yyyy-MM-dd}"

when.contains:

tags: "app1"

  • index: "app2-%{+yyyy-MM-dd}"

when.contains:

tags: "app2"

3:启动filebeat

bash 复制代码
./filebeat -e -c filebeat.yml
#后台运行
nohup  ./filebeat  -c  filebeat.yml  -e  >/dev/null  2>&1  &

问题与解决:

问题一:runtime/cgo: pthread_create failed: Operation not permitted

解决:

在filebeat.yml配置文件添加如下配置,重启filebeat

bash 复制代码
seccomp:
  default_action: allow 
  syscalls:
  - action: allow
    names:
    - rseq

参考文档:

Filebeat and GLIBC Errors on Ubuntu 22.04 - Beats - Discuss the Elastic Stack

问题二:错误日志分行显示

解决:添加正则表达式多行合并,以时间开头的放在一起

bash 复制代码
  multiline:  
    pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'  
    negate: true 
    match: after

pattern:正则表达式

negate:true 或 false;默认是false,匹配pattern的行合并到上一行;true,不匹配pattern的行合并到上一行

match:after 或 before,合并到上一行的末尾或开头

max_lines:合并最大行,默认500

timeout:一次合并事件的超时时间,默认5s,防止合并消耗太多时间甚至卡死

参考文档:(八)Filebeat收集日志方法_filebeat按日期收集-CSDN博客

问题三:Filebeat运行一段时间后自动关闭问题解决

问题解决:

3.1:在 /etc/systemd/system目录下创建一个filebeat.service文件,写入如下内容

bash 复制代码
[Unit]
Description=Filebeat is a lightweight shipper for metrics.
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target
 
[Service]
Environment="LOG_OPTS=-e"
Environment="CONFIG_OPTS=-c /usr/local/src/filebeat-7.9.3-linux-x86_64/filebeat.yml"
Environment="PATH_OPTS=-path.home /usr/local/src/filebeat-7.9.3-linux-x86_64/filebeat -path.config /usr/local/src/filebeat-7.9.3-linux-x86_64 -path.data /usr/local/src/filebeat-7.9.3-linux-x86_64/data -path.logs /usr/local/src/filebeat-7.9.3-linux-x86_64/logs"
ExecStart=/usr/local/src/filebeat-7.9.3-linux-x86_64/filebeat $LOG_OPTS $CONFIG_OPTS $PATH_OPTS
Restart=always
 
[Install]
WantedBy=multi-user.target

3.2:给文件授权限

bash 复制代码
chmod +x /etc/systemd/system/filebeat.service

3.3:启动并查看状态

启动:

systemctl daemon-reload

systemctl enable filebeat

systemctl start filebeat

查看状态

systemctl status filebeat

相关推荐
奥尔特星云大使24 分钟前
详细的Linux系统更新yum源的教程
linux·运维·服务器·ubuntu·centos·yum源·epel源
tritone26 分钟前
在优豆云免费云服务器上初探SSH与SCP的便捷操作
运维·服务器·ssh
lht63193561227 分钟前
Ubuntu Server 系统安装图形界面
linux·运维·ubuntu
大美B端工场-B端系统美颜师28 分钟前
数字孪生为什么需要5G?低延迟与高可靠实现精准控制
运维·服务器·5g
christine-rr29 分钟前
linux常用命令(6)——网络管理
linux·服务器·网络·ubuntu·网络安全
isyangli_blog1 小时前
Linux (CentOS 7.9) vmware 创建与安装
linux·运维·centos
AHUGEFATTY1 小时前
centos离线包获取-附centos7主流离线包资源
linux·运维·centos
tingting01192 小时前
jenkins在使用中遇到的问题
运维·jenkins
hjlgs2 小时前
Kernel5.4 Timer定时器使用
linux
歪歪1002 小时前
在哪些场景下适合使用 v-model 机制?
服务器·前端·javascript·servlet·前端框架·js