CentOS7安装Filebeat

一、介绍及规划

Filebeat 是 ELK 的一个组件,用于收集应用服务器的日志。Filebeat 的版本绝对不能高于 Elasticsearch 的版本

主机名 ip地址 系统版本 软件版本
es101 192.168.10.101 centos7 8.19.9
es102 192.168.10.102 centos7 8.19.9
es103 192.168.10.103 centos7 8.19.9

二、安装

有多种方式安装,RPM包安装、二进制文件解压安装。这里以 8.19.9 版本为例。

下载地址

2.1 RPM包安装

  1. 下载
1
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.19.9-x86_64.rpm
  1. 安装
1
rpm -ivh filebeat-8.19.9-x86_64.rpm
  1. 验证
1
filebeat version

2.2 二进制包安装

  1. 下载
1
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.19.9-linux-x86_64.tar.gz
  1. 解压
1
tar xf filebeat-8.19.9-linux-x86_64.tar.gz -C /usr/local
  1. 创建软链接
1
ln -s /usr/local/filebeat-8.19.9-linux-x86_64 /usr/local/filebeat
  1. 设置环境变量
1
echo 'export PATH=/opt/elasticsearch:$PATH' > /etc/profile.d/filebeat.sh
  1. 刷新环境变量
1
source /etc/profile
  1. 验证版本
1
filebeat version
  1. 使用 systemd 管理
1
vi /usr/lib/systemd/system/filebeat.service
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Filebeat log shipper
Documentation=https://www.elastic.co/guide/en/beats/filebeat/current/index.html
Wants=network-online.target
After=network-online.target

[Service]
# 假设你的软链接或实际目录在 /opt/filebeat
ExecStart=/opt/filebeat/filebeat -c /opt/filebeat/filebeat.yml -path.home /opt/filebeat -path.config /opt/filebeat -path.data /opt/filebeat/data -path.logs /opt/filebeat/logs
Restart=always

[Install]
WantedBy=multi-user.target
  1. 自启动
1
2
3
4
systemctl daemon-reload
systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat

三、配置使用

  1. 默认配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@lnmp ~]# egrep -v '^$|#' /etc/filebeat/filebeat.yml 
filebeat.inputs:
- type: filestream
id: my-filestream-id
enabled: false
paths:
- /var/log/*.log
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.kibana:
output.elasticsearch:
hosts: ["localhost:9200"]
preset: balanced
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
[root@lnmp ~]#
  1. 对接 ES 集群配置,output.elasticsearch 模块
1
vim /etc/filebeat/filebeat.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
output.elasticsearch:
# 这里的 hosts 是一个列表,填入你所有的 ES 节点地址
hosts: ["192.168.10.101:9200", "192.168.10.102:9200", "192.168.10.103:9200"]

# 如果 ES 启用了用户名密码(X-Pack 安全插件)
username: "elastic"
password: "elastic"

# 设置索引名称(可选,默认是 filebeat-%{[agent.version]}-%{+yyyy.MM.dd})
# index: "nginx-logs-%{+yyyy.MM.dd}"

# 如果自定义了索引名称,需要关闭 setup.template
# setup.template.name: "nginx"
# setup.template.pattern: "nginx-*"
  1. 启用 Nginx 模块
1
filebeat modules enable nginx
1
vim /etc/filebeat/modules.d/nginx.yml
1
2
3
4
5
6
7
8
- module: nginx
access:
enabled: true
# 根据你 Nginx 的实际路径修改
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
  1. 加载资产
1
filebeat setup -e
  1. 测试配置文件
1
filebeat test config -c /etc/filebeat/filebeat.yml
  1. 启动服务
1
2
systemctl start filebeat
systemctl enable filebeat
  1. 检查日志确认 Filebeat 是否成功连接到 ESs 集群
1
tail -f /var/log/filebeat/filebeat
  1. 图形化工具查看是否出现新的索引