ELK+Filebeat

Filebeat概述

1.Filebeat简介

Filebeat是一款轻量级的日志收集工具,可以在非JAVA环境下运行。

因此,Filebeat常被用在非JAVAf的服务器上用于替代Logstash,收集日志信息
实际上,Filebeat几乎可以起到与Logstash相同的作用,可以将数据转发到Logstash、Redis或者是Elasticsearch中进行直接处理。

2.使用Filebeat的原因

因为logstash是jvm跑的,资源消耗比较大,启动一个logstash就需要消耗500M左右的内存
(这就是为什么logstash启动特别慢的原因)。

而filebeat只需要10M左右的内存资源。

常用的ELK日志采集方案中,大部分的做法就是将所有节点的日志内容通过filebeat发送到logstash,lostash根据配置文件进行过滤,然后将过滤之后的文件传输到elasticsearch中,最后通过kibana展示

3.Filebeat结合logstash的好处

Filebeat结合logstash中,Filebeat负责收集日志,logstash负责过滤

  • 1、通过logstash,具有基于磁盘的自适应缓冲系统,该系统将吸收传入的吞吐量,从而减轻Elasticsearch持续写入数据的压力。
  • 2、从其它数据源(例如数据库,s3对象存储或消息传递队列)中提取
  • 3、将数据发送到多个目的地,例如S3,HDFS(hadoop分部署文件系统)或写入文件
  • 4、使用数据流逻辑组成更复杂的处理管道。

本机直接收集日志实验

(Nginx日志)

实验组件

复制代码
Node1节点:node1/20.0.0.20			      Elasticsearch
Node2节点:node2/20.0.0.30				  Elasticsearch
Filebeat节点:20.0.0.10				      Logstash  Kibana  Filebeat  Nginx

实验步骤

1.安装Filebeat

复制代码
#10
cd /opt/
--上传filebeat-6.7.2-linux-x86_64.tar.gz--
tar -xf filebeat-6.7.2-linux-x86_64.tar.gz
mv filebeat-6.7.2-linux-x86_64 filebeat
vim /etc/logstash/logstash.yml
--64--
path.config: /opt/log

systemctl restart logstash

2.时间同步

复制代码
#所有节点
yum -y install ntpdate
ntpdate ntp.aliyun.com 
date

3.配置filebeat

复制代码
#修改nginx的端口,防止与apache冲突
vim /etc/nginx/nginx.conf
...
listen       8080;
listen       [::]:8080;
复制代码
#给nginx日志文件赋权
cd /var/log/nginx/
chmod 777 access.log error.log
复制代码
#配置filebeat
cd /opt/filebeat/
vim filebeat.yml
filebeat.inputs:

- type: log
  enabled: true	
  paths:
    - /var/log/nginx/access.log
    - /var/log/nginx/error.log
  tags: ["nginx"]
  fields:
    service_name: 20.0.0.10_nginx
    log_type: nginx
    from: 20.0.0.10

--------------Elasticsearch output-------------------
(全部注释掉)

----------------Logstash output---------------------
output.logstash:
  hosts: ["20.0.0.10:5044"]      #指定logstash的IP和端口

4.配置logstash

复制代码
cd /opt
mkdir log
cd log
vim file_nginx.conf

input {

        beats { port => "5044"}

}

output {

        if "nginx" in [tags] {
           elasticsearch {

                hosts => ["20.0.0.20:9200","20.0.0.30:9200"]
                index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"

           }

        }

stdout {
        codec => rubydebug
       }

}

5.启动filebeat

复制代码
nohup ./filebeat -e -c filebeat.yml > filebeat.out &
-------------------------------------------------------------------------------------------
nohup:表示在后台记录执行命令的过程
./filebeat:运行文件
-e:使用标准输出的同时禁用syslog文件输出
-c:指定配置文件
将执行过程输出到filebeat.out文件当中
&:后台运行
-------------------------------------------------------------------------------------------
logstash -f file_nginx.conf --path.data /opt/test1 &

6.结果

远程收集多个日志实验

(Nginx+Apache+Mysql日志)

实验组件

复制代码
logstash节点:20.0.0.10
Node1节点:node1/20.0.0.20			      Elasticsearch
Node2节点:node2/20.0.0.30				  Elasticsearch
日志来源服务器:20.0.0.81                   MYsql  Nginx  Apache  Filebeat

实验步骤

1.安装Filebeat并配置

复制代码
#81
cd /opt/
--上传filebeat-6.7.2-linux-x86_64.tar.gz--
tar -xf filebeat-6.7.2-linux-x86_64.tar.gz
mv filebeat-6.7.2-linux-x86_64 filebeat
cd filebeat/
vim filebeat.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
    - /var/log/nginx/error.log
  tags: ["nginx_81"]
  fields:
    service_name: 20.0.0.81_nginx
    log_type: nginx
    from: 20.0.0.81

- type: log
  enabled: true
  paths:
    - /etc/httpd/logs/access_log
    - /etc/httpd/logs/error_log
  tags: ["httpd_81"]
  fields:
    service_name: 20.0.0.81_httpd
    log_type: httpd
    from: 20.0.0.81

- type: log
  enabled: true
  paths:
    - /usr/local/mysql/data/mysql_general.log
  tags: ["mysql_81"]
  fields:
    service_name: 20.0.0.81_mysql
    log_type: mysql
    from: 20.0.0.81

--------------Elasticsearch output-------------------
(全部注释掉)

----------------Logstash output---------------------
output.logstash:
  hosts: ["20.0.0.10:5045"]      #指定logstash的IP和端口

2.配置logstash

复制代码
10:
cd /opt/log/
vim nhm_81.conf 

input {
        beats { port => "5045"}
}

output {
        if "nginx_81" in [tags] {
           elasticsearch {
                hosts => ["20.0.0.20:9200","20.0.0.30:9200"]
                index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
           }
        }

        if "httpd_81" in [tags] {
           elasticsearch {
                hosts => ["20.0.0.20:9200","20.0.0.30:9200"]
                index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
           }
        }

        if "mysql_81" in [tags] {
           elasticsearch {
                hosts => ["20.0.0.20:9200","20.0.0.30:9200"]
                index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
           }
        }

stdout {
        codec => rubydebug
       }

}

3.启动filebeat

复制代码
nohup ./filebeat -e -c filebeat.yml > filebeat.out &

logstash -f nhm_81.conf --path.data /opt/test2 &

4.结果

相关推荐
勿芮介5 天前
[微服务]ELK Stack安装与配置全指南
elk·微服务·架构
core5129 天前
基于elk实现分布式日志
分布式·elk·日志·logstash
晴子呀12 天前
使用Springboot实现简单的ELK日志搜索系统
spring boot·后端·elk
悟能不能悟13 天前
如何搭建ELK
elk
only_Klein17 天前
K8S部署ELK(二):部署Kafka消息队列
elk·kafka·kubernetes
core51219 天前
elk快速部署、集成、调优
elk·springboot·kibana·索引·查询
only_Klein19 天前
K8S部署ELK(一):部署Filebeat日志收集器
elk·容器·kubernetes
晴子呀19 天前
ELK实践指南
elk
三不原则19 天前
日志管理工具 ——ELK Stack
运维·elk
only_Klein19 天前
K8S部署ELK(五):集成Kibana实现日志可视化
elk·容器·kubernetes