跳转至

搭建日志分析平台

为增加 ELK 集群的运行效率,一般建议在 k8s 集群外使用物理机部署 ELK 集群。

fileBeat 部署

filebeat 可以实现从本地/远程服务器提取日志文件内容,它可以保证日志文件的内容将至少传递一次到配置的输出并且没有数据丢失。

1. 下载 rpm 包
yum -y install java-11-openjdk
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.15.1-linux-x86_64.tar.gz
tar -xf filebeat-8.15.1-linux-x86_64.tar.gz

fileBeat 配置

1. vim nginx.yaml
filebeat.modules:
- modules: nginx
  enabled: true    #true表示当前的paths会被监听  false表示不监听
  access:
    enabled: true
    var.paths: ["/var/log/nginx/access.log"]
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log"]

#-------------------- Kafka output -----------------------#

output.kafka:
  codec.format:
    string: '%{[@timestamp]} %{[message]}'
  #自己kafka的服务地址
  hosts: ["10.10.0.2:9092", "10.10.0.4:9092", "10.10.0.5:9092"]
  topic: 'nginx-log-topic'
  #控制分区行为
  partition.round_robin:
    reachable_only: false
  #kafka的请求确认等级
  required_acks: 1
  #kafka的压缩
  compression: gzip
  #kafka最大消息大小设置
  max_message_bytes: 1000000

fileBeat 启动

1. 测试文件是否正确
./filebeat test config -c nginx.yml
./filebeat test output -c nginx.yml
2. 启动
./filebeat -e -c nginx.yml
/etc/filebeat/filebeat.yml 核心配置
filebeat.inputs:
- type: log
  enabled: true    #true表示当前的paths会被监听  false表示不监听
  paths:
    - /var/log/messages
#给当前的监听到的日志增加一个标签名称
  tags: ["nginx_access_log"]

output.kafka:
  codec.format:
    string: '%{[@timestamp]} %{[message]}'
  #自己kafka的服务地址
  hosts: ["192.168.3.40:9092"]
  topic: 'nginx-log-topic'
  #控制分区行为
  partition.round_robin:
    reachable_only: false
  #kafka的请求确认等级
  required_acks: 1
  #kafka的压缩
  compression: gzip
  #kafka最大消息大小设置
  max_message_bytes: 1000000

fileBeat 启动

1. 启动
systemctl enable kafka --now
2. 查看状态
systemctl status kafka
3. 测试文件是否正确
filebeat test config -c /etc/filebeat/filebeat.yml
filebeat test output -c /etc/filebeat/filebeat.yml