如何在Linux中运行多个Filebeat实例
在本教程中,您将学习如何在Linux系统中运行多个filebeat实例。 文件拍 是Elastic Beat之一,是用于收集,转发和集中事件日志数据的轻量级托运人。它作为代理程序安装在要从中收集日志的服务器上。它可以将正在收集的日志转发到Elasticsearch或Logstash进行直接索引编制或进一步处理,然后再分别发送到Elasticsearch进行索引编制。
在Linux中运行多个Filebeat实例
在以前的教程中,我们讨论了如何安装单实例Filebeat。
在Ubuntu 20.04上安装和配置Filebeat
在CentOS 8上安装和配置Filebeat
在Fedora 30 / Fedora 29 / CentOS 7上安装Filebeat
在Ubuntu 18.04 / Debian 9.8上安装和配置Filebeat 7
在以上所有教程中,我们只有一个Filebeat实例在系统上运行,并且在将日志数据直接发送到Elasticsearch或Logstash之前进行进一步处理,然后再发送到Elasticsearch。
有时候,您可能希望将系统日志发送到多个输出。例如将一些日志直接发送到Elasticsearch,同时将其他一些日志直接发送到Logstash进行处理。
因此,在本教程中,让我们看看在Linux系统中安装和运行多个filebeat实例的可能性,以便能够将数据发送到多个输出中。
如何在Linux中运行多个Filebeat实例
您可以通过多种方式在Linux中安装和运行多个filebeat实例。其中一些包括:
- 使用Filebeat-god在Linux中运行多个Filebeat实例
- 使用systemd在Linux中运行多个Filebeat实例
使用Filebeat-god在Linux中运行多个Filebeat实例
Go守护进程 (要不就 神)是一种实用程序,用于“守护”最初仅在前台运行的Go程序,并将日志写入控制台。 Filebeat-god (Filebeat Go daemon)
因此,它是一个实用程序,用于守护本来可以在前台运行的Filebeat进程。
只是要注意,该指南已经在Ubuntu 20.04和CentOS 8上进行了测试。但是,我猜它也应该在其他系统上工作-:)。
安装Filebeat-god
每当安装Filebeat时,Filebeat-god就会一起安装,并且通常与Filebeat二进制文件位于 /usr/share/filebeat/bin
目录。
要找到Filebeat-god二进制文件,只需使用以下find命令即可;
find / -iname filebeat-god
/usr/share/filebeat/bin/filebeat-god
现在,默认情况下,Filebeat使用以下路径;
家 | Filebeat安装的主目录。 | /usr/share/filebeat |
箱子 | 二进制文件的位置。 | /usr/share/filebeat/bin |
配置 | 配置文件的位置。 | /etc/filebeat |
数据 | 永久数据文件的位置。 | /var/lib/filebeat |
日志 | Filebeat创建的日志的位置。 | /var/log/filebeat |
假设您想使用系统模块将系统日志和系统身份验证事件直接转发到Elasticsearch,同时又想将其他自定义日志发送到Logstash进行进一步处理,然后再发送到Elasticsearch,那么您必须使用Filebeat-god运行Filebeat进程, /usr/share/filebeat/bin/filebeat-god
, 如下;
创建Filebeat配置文件的副本。
cp -r /etc/filebeat{,-elasticsearch}
此命令创建一个 /etc/filebeat
-org目录。
现在我们有了这些FIlebeat配置目录。
ls /etc/filebeat*
/etc/filebeat:
fields.yml filebeat.reference.yml filebeat.yml modules.d
/etc/filebeat-elasticsearch:
fields.yml filebeat.reference.yml filebeat.yml modules.d
让我们收集 系统日志 和 认证方式 日志使用 /etc/filebeat-elasticsearch
配置并转发到Elasticsearch。启用系统模块;
cp /etc/filebeat-elasticsearch/modules.d/system.yml{.disabled,}
配置Elasticsearch输出
vim /etc/filebeat-elasticsearch/filebeat.yml
...
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["192.168.57.30:9200"]
...
保存并退出配置。
验证是否可以通过在调试模式下运行Filebeat来建立与Elasticsearch的连接;
filebeat -e --path.config /etc/filebeat-elasticsearch --path.home /usr/share/filebeat --path.data /var/lib/filebeat-elasticsearch
如果一切顺利,您应该会看到正在建立Elasticsearch连接。
... 2020-11-24T19:47:12.332Z INFO [index-management] idxmgmt/std.go:298 Loaded index template. 2020-11-24T19:47:12.333Z INFO [index-management] idxmgmt/std.go:309 Write alias successfully generated. 2020-11-24T19:47:12.333Z INFO [publisher_pipeline_output] pipeline/output.go:151 Connection to backoff(elasticsearch(http://192.168.57.30:9200)) established
接下来,停止Filebeat在调试模式下运行,并使用Filebeat-god作为守护程序将其启动,如下所示;
/usr/share/filebeat/bin/filebeat-god -n -p /var/run/filebeat-elasticsearch.pid -r / -- /usr/share/filebeat/bin/filebeat --path.config /etc/filebeat-elasticsearch --path.home /usr/share/filebeat --path.data /var/lib/filebeat-elasticsearch --path.logs /var/log/filebeat-elasticsearch
验证Filebeat现在是否正在作为守护程序运行;
ps aux | grep filebeat | grep -v grep
root 121377 0.0 0.0 9300 636 pts/1 S 20:14 0:00 /usr/share/filebeat/bin/filebeat-god -n -p /var/run/filebeat-elasticsearch.pid -r / -- /usr/share/filebeat/bin/filebeat --path.config /etc/filebeat-elasticsearch --path.home /usr/share/filebeat --path.data /var/lib/filebeat-elasticsearch --path.logs /var/log/filebeat-elasticsearch
root 121378 4.8 2.0 1609528 127224 pts/1 SLl 20:14 0:02 /usr/share/filebeat/bin/filebeat --path.config /etc/filebeat-elasticsearch --path.home /usr/share/filebeat --path.data /var/lib/filebeat-elasticsearch --path.logs /var/log/filebeat-elasticsearch
拖尾原木;
tail -f /var/log/filebeat-elasticsearch/filebeat
... 2020-11-25T17:22:30.532Z INFO template/load.go:97 Template filebeat-7.10.0 already exists and will not be overwritten. 2020-11-25T17:22:30.532Z INFO [index-management] idxmgmt/std.go:298 Loaded index template. 2020-11-25T17:22:30.533Z INFO [index-management] idxmgmt/std.go:309 Write alias successfully generated. 2020-11-25T17:22:30.536Z INFO [publisher_pipeline_output] pipeline/output.go:151 Connection to backoff(elasticsearch(http://192.168.57.30:9200)) established
配置Logstash输出
现在,如果要在同一系统上收集其他一些日志,然后直接发送到Logstash进行进一步处理,而不是发送到Elasticsearch,则只需创建一个单独的Filebeat配置文件,如下所示;
cp -r /etc/filebeat{,-logstash}
vim /etc/filebeat-logstash/filebeat.yml
指定日志文件以读取数据并将数据发送到Logstash。在此示例中,我们正在收集wordpress日志并转发到Logstash进行处理。
# ============================== Filebeat inputs =============================== filebeat.inputs: - type: log # Change to true to enable this input configuration. enabled: true # Paths that should be crawled and fetched. Glob based paths. paths: - /var/log/wordpress/kifarunix-demo.com.log ...
... # ================================== Outputs =================================== # Configure what output to use when sending the data collected by the beat. # ---------------------------- Elasticsearch Output ---------------------------- #output.elasticsearch: # Array of hosts to connect to. #hosts: ["192.168.57.30:9200"] ... ... # ------------------------------ Logstash Output ------------------------------- output.logstash: # The Logstash hosts hosts: ["192.168.57.30:5044"] ...
保存并退出配置文件。
使用上面创建的logstash的自定义Filebeat配置在调试模式下运行Filebeat。
filebeat -e --path.config /etc/filebeat-logstash/ --path.home /usr/share/filebeat --path.data /var/lib/filebeat-logstash
...
2020-11-25T18:06:30.298Z INFO log/harvester.go:302 Harvester started for file: /var/log/wordpress/kifarunix-demo.com.log
2020-11-25T18:06:33.300Z INFO [add_cloud_metadata] add_cloud_metadata/add_cloud_metadata.go:89 add_cloud_metadata: hosting provider type not detected.
2020-11-25T18:06:34.302Z INFO [publisher_pipeline_output] pipeline/output.go:143 Connecting to backoff(async(tcp://192.168.57.30:5044))
2020-11-25T18:06:34.303Z INFO [publisher] pipeline/retry.go:219 retryer: send unwait signal to consumer
2020-11-25T18:06:34.304Z INFO [publisher] pipeline/retry.go:223 done
2020-11-25T18:06:34.305Z INFO [publisher_pipeline_output] pipeline/output.go:151 Connection to backoff(async(tcp://192.168.57.30:5044)) established
如果一切正常,请运行Filebeat实例,该实例将数据作为守护程序发送到Logstash;
/usr/share/filebeat/bin/filebeat-god -n -p /var/run/filebeat-logstash.pid -r / -- /usr/share/filebeat/bin/filebeat --path.config /etc/filebeat-logstash/ --path.home /usr/share/filebeat --path.data /var/lib/filebeat-logstash --path.logs /var/log/filebeat-logstash
同样,您可以使用 ps 命令。
使用Systemd在Linux中运行多个Filebeat实例
对于使用Systemd初始化的Linux系统,甚至不需要费心地在Linux中使用Filebeat-god运行多个Filebeat实例。
使用Systemd在Linux中运行多个Filebeat实例非常简单。
假设您想收集各种日志并将其发送到各种输出,如上面的示例所示,那么您要做的就是为Filebeat收集每个特定日志文件上的日志创建一个Systemd服务单元。
一个基于上面的示例配置文件创建Filebeat以将日志发送到Elasticsearch的示例;
复制默认的FIlebeat systemd服务单元,从而对其进行重命名。
cp /lib/systemd/system/filebeat.service /etc/systemd/system/filebeat-elasticsearch.service
编辑定制服务单元的配置文件并设置适当的目录
vim /etc/systemd/system/filebeat-elasticsearch.service
[Unit] Description=Filebeat sends log files to directly to Elasticsearch. Documentation=https://www.elastic.co/products/beats/filebeat Wants=network-online.target After=network-online.target [Service] Environment="BEAT_LOG_OPTS=" Environment="BEAT_CONFIG_OPTS=-c /etc/filebeat-elasticsearch/filebeat.yml" Environment="BEAT_PATH_OPTS=--path.home /usr/share/filebeat --path.config /etc/filebeat-elasticsearch --path.data /var/lib/filebeat-elasticsearch --path.logs /var/log/filebeat-elasticsearch" ExecStart=/usr/share/filebeat/bin/filebeat --environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS Restart=always [Install] WantedBy=multi-user.target
同样,用于发送到Logstash;
cp /lib/systemd/system/filebeat.service /etc/systemd/system/filebeat-logstash.service
vim /etc/systemd/system/filebeat-logstash.service
[Unit]
Description=Filebeat sends log files to Logstash.
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target
[Service]
Environment="BEAT_LOG_OPTS="
Environment="BEAT_CONFIG_OPTS=-c /etc/filebeat-logstash/filebeat.yml"
Environment="BEAT_PATH_OPTS=--path.home /usr/share/filebeat --path.config /etc/filebeat-logstash --path.data /var/lib/filebeat-logstash --path.logs /var/log/filebeat-logstash"
ExecStart=/usr/share/filebeat/bin/filebeat --environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS
Restart=always
[Install]
WantedBy=multi-user.target
重新加载系统配置;
systemctl daemon-reload
启动两项服务;
systemctl start filebeat-logstash filebeat-elasticsearch
检查状态;
systemctl status filebeat-logstash filebeat-elasticsearch
● filebeat-logstash.service - Filebeat sends log files to Logstash.
Loaded: loaded (/etc/systemd/system/filebeat-logstash.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2020-11-25 19:29:20 UTC; 1min 21s ago
Docs: https://www.elastic.co/products/beats/filebeat
Main PID: 129521 (filebeat)
Tasks: 9 (limit: 7031)
Memory: 28.4M
CGroup: /system.slice/filebeat-logstash.service
└─129521 /usr/share/filebeat/bin/filebeat --environment systemd -c /etc/filebeat-logstash/filebeat.yml --path.home /usr/share/filebeat --path.config /etc/file>
...
● filebeat-elasticsearch.service - Filebeat sends log files to directly to Elasticsearch.
Loaded: loaded (/etc/systemd/system/filebeat-elasticsearch.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2020-11-25 19:29:20 UTC; 1min 21s ago
Docs: https://www.elastic.co/products/beats/filebeat
Main PID: 129540 (filebeat)
Tasks: 9 (limit: 7031)
Memory: 23.9M
CGroup: /system.slice/filebeat-elasticsearch.service
└─129540 /usr/share/filebeat/bin/filebeat --environment systemd -c /etc/filebeat-elasticsearch/filebeat.yml --path.home /usr/share/filebeat --path.config /et...
然后你去。这就是在Linux中运行多个Filebeat实例所需要的全部。
参考
。