Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程

        如果你没有听说过Elastic Stack,那你一定听说过ELK,实际上ELK是三款软件的简称,分别是Elasticsearch、Logstash、Kibana组成,在发展的过程中,又有新成员Beats的加入,所以就形成了Elastic Stack。所以说,ELK是旧的称呼,Elastic Stack是新的名字。

全系的Elastic Stack技术栈包括:

由上图可以看出Beats并不是指单一的某个技术,它是指一系列技术在总称,采集能力更加轻量级更加强大,并且已经逐渐取代Logstash的地位。

https://www.elastic.co/cn/beats/

1. Beats简介

2. Beats之Filebeat

2.1 架构

用于监控、收集服务器日志文件。Harvester:收割机   Spooler:卷轴,把数据传输到下游


2.2 部署与运行

下载: https://www.elastic.co/downloads/beats    本文下载的是:filebeat-7.10.0-linux-x86_64.tar.gz
解压到指定目录:tar -zxvf filebeat-7.10.0-linux-x86_64.tar.gz  -C ../opt/ 
进入到filebeat目录:cd /opt/filebeat-7.10.0-linux-x86_64/

2.2.1 读取标准输入

#创建如下配置文件 vim yztest.yml   注意有空格

filebeat.inputs:
- type: stdin                            标准输入
  enabled: true                        启用输入
setup.template.settings:
  index.number_of_shards: 3  指定ES索引分区数,现在用不到
output.console:                       输出到控制台
  pretty: true                            输出美化
  enable: true                          输出启用

 
 
# 启动 filebeat
 

./filebeat -e -c yztest.yml

# 输入 hello 运行结果如下:
 


2.3 读取文件

# 配置读取文件项 yztest-log.yml   注意有空格
 

filebeat.inputs:
- type: log                                             输入类型为日志文件
  enabled: true                                      启用输入
  paths:    
        - /opt/test/logs/*.log                      要收集的日志路径
setup.template.settings:
    index.number_of_shards: 3            指定ES索引分区数,现在用不到
output.console:                                    输出到控制台
    pretty: true                                       输出美化
    enable: true                                     输出启用

# 启动 filebeat
 

./filebeat -e -c yztest-log.yml

#另外打开一个窗口,在/opt/test/logs 下创建 a.log 文件,并写入hello
 
 
# 观察 filebeat 输出
 

可以看出,已经检测到日志文件有更新,立刻就会读取到更新的内容,并且输出到控制台。

2.4 自定义字段,给要收集的日志定义tab标签用于区分来自哪里

# 配置读取文件项 yztest-log.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:  
       - /opt/test/logs/*.log
  tags: ["fl-carloan-web"]         #添加自定义tag,便于后续的处理
  fields:                                   #添加自定义字段,说明日志来自哪里
         from: fl-carloan-web
  fields_under_root: true          #true为添加到根节点,false为添加到子节点中
setup.template.settings:
  index.number_of_shards: 3                       
output.console:
  pretty: true
  enable: true

# 启动 filebeat
./filebeat -e -c yztest-log.yml
 
在a.log添加新的内容:echo '1234' >> /opt/test/logs/a.log
# 执行效果
 

2.5 输出到Elasticsearch  vim yztest-log.yml

filebeat.inputs:
- type: log                                 #输入类型为log
  enabled: true 
  paths:                                     #要收集的日志路径              
       - /opt/test/logs/*.log
  tags: ["fl-carloan-web"]          #添加自定义tag,便于后续的处理
  fields:                                     #添加自定义字段,说明日志来自哪里
         from: fl-carloan-web
  fields_under_root: true          #true为添加到根节点,false为添加到子节点中
setup.template.settings:
  index.number_of_shards: 3        #指定elasticsearch索引的分区数                 
 
setup.template.name: "filebeat"         # 如果指定索引,需要设置模板
setup.template.pattern: "filebeat-*"
output.elasticsearch:                     #指定输出到elasticsearch
  hosts: ["192.168.226.30:9200","192.168.226.31:9200","192.168.226.32:9200"] 
  index: "%{[fields.log_type]}-%{[agent.version]}-%{+yyyy.MM.dd}"   # 设置索引 filebeat-7.10.0-2020-12-02
# 启动 filebeat
./filebeat -e -c yztest-log.yml
 
在a.log添加新的内容:echo 'filebeat来收集' >> /opt/test/logs/a.log
 
 
去ES查看,可以看到ES已经收到filebeat推送过来的数据:
 
 
 
 

2.6 FileBeat工作原理

Filebeat由两个主要组件组成:prospector(勘探者) 和 harvester(收割机)。
harvester:
       负责读取单个文件的内容。
       如果文件在读取时被删除或重命名,Filebeat将继续读取文件。
prospector:
       prospector 负责管理harvester并找到所有要读取的文件来源。
       如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester。
       Filebeat目前支持两种prospector类型:log和stdin。
Filebeat如何保持文件的状态(就是Filebeat宕机之后从哪里开始读取):
       Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中(文件状态记录在filebeat-7.10.0/data/registry/filebeat/log.json文件中)。
       该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。
       如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用时继续读取文件。
       在Filebeat运行时,每个prospector内存中也会保存的文件状态信息,当重新启动Filebeat时,将使用注册文件的数据来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取。

cat log.json  记录读取日志文件和最后偏移量
      

启动命令:

./filebeat -e -c yztest.yml
./filebeat -e -c yztest.yml -d "publish"
# 参数说明
-e : 输出到标准输出,默认输出到 syslog logs
-c : 指定配置文件
-d "publish"  : 输出 debug 信息(在输出到ES中时,使用debug参数,可以在filebeat控制台查看debug信息)


2.7 读取Nginx日志文件,filebeat整合nginx做个测试

简要介绍Nginx日志分析系统

1.1、项目需求
Nginx是一款非常优秀的web服务器,往往nginx服务会作为项目的访问入口,那么,nginx的性能保障就变得非常重要了,如果nginx的运行出现了问题就会对项目有较大的影响,所以,我们需要对nginx的运行有监控措施,实时掌握nginx的运行情况,那就需要收集nginx的运行指标和分析nginx的运行日志了。
1.2、业务流程

说明:
1. 通过Beats采集Nginx的指标数据和日志数据
2. Beats采集到数据后发送到Elasticsearch中
3. Kibana读取数据进行分析
4. 用户通过Kibana进行查看分析报表

部署安装 Nginx
1.  下载并上传至服务器  http://nginx.org/en/download.html
2.  解压 tar -zxvf nginx-1.19.4.tar.gz -C /opt/
3. 下载依赖  yum install -y gcc gcc-c++ zlib-devel pcre pcre-devel openssl openssl-devel
4. 编译 cd nginx-1.19.4/    执行:./configure 等待配置完成 , 配置完成后执行:make install 等待编译安装
5. 启动 cd /usr/local/nginx/sbin/   ./nginx 启动nginx
6. 浏览器访问页面,默认80端口。 http://192.168.226.30/
6. 查看日志  tail -f /usr/local/nginx/logs/access.log   刷新页面就会有一条请求日志

filebeat 读取nginx日志文件

# 编写配置文件 cd /opt/filebeat-7.10.0/   vim yztest-nginx.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
     - /usr/local/nginx/logs/*.log
  tags: ["nginx"]
setup.template.settings:
  index.number_of_shards: 3 #指定ES索引的分区数
output.elasticsearch:             #指定ES的配置
  hosts: ["192.168.226.30:9200","192.168.226.31:9200","192.168.226.32:9200"]
 

# 启动 (先把之前的es中filebeat数据删除,方便本次查看)
 
./filebeat -e -c yztest-nginx.yml
 
启动后,可以在Elasticsearch中看到索引以及查看数据:
 

可以看到,在message中已经获取到了nginx的日志,但是,内容并没有经过处理,只是读取到原数据,那么对于我们后期的操作是不利的,可以使用内置的module来处理一下。

2.8 Module

前面要想实现日志数据的读取以及处理都是自己手动配置的,比如手动解析message字符串的内容。其实,在Filebeat中,有大量的Module(使用module解析message字符串,好比工具类),可以简化我们的配置,直接就可以使用,如下: 命令 ./filebeat modules list

可以看到,内置了很多的 module ,但是都没有启用,如果需要启用需要进行 enable 操作,以启用nginx module为例:
 
./filebeat modules enable nginx  #启动 
./filebeat modules disable nginx #禁用
 

2.7.1 nginx module 配置

cd modules.d/   ll查看

vim  nginx.yml 

- module: nginx
  # Access logs
  access:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/access.log*"]    # 日志按天结尾

  # Error logs
  error:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/error.log*"]       # 日志按天结尾

2.7.2 、配置 fifilebeat
# cp yztest-nginx.yml yztest-nginx-module.yml    vim yztest-nginx-module.yml  把输入注释掉,配置为modules输入
  
filebeat.inputs:
  tags: ["nginx"]                    #添加自定义tag,便于后续的处理
  fields:                                #添加自定义字段,说明日志来自哪里
     from: nginx
  fields_under_root: true          #true为添加到根节点,false为添加到子节点中
setup.template.settings:
  index.number_of_shards: 3        #指定elasticsearch索引的分区数              
output.elasticsearch:              #指定输出到elasticsearch,hosts为es集群地址
  hosts: ["192.168.226.30:9200","192.168.226.31:9200","192.168.226.32:9200"]
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

启动  ./filebeat -e -c yztest-nginx-module.yml 

如果有报错,按下面的方式解决,这里启动没有报错。

ERROR fileset/factory.go:142 Error loading pipeline: Error loading pipeline for fileset nginx/access: This module requires the following Elasticsearch plugins: ingest-user-agent, ingest-geoip. You can install them by running the following commands on all the Elasticsearch nodes:
sudo bin/elasticsearch-plugin install ingest-user-agent
sudo bin/elasticsearch-plugin install ingest-geoip
# 解决:需要在 Elasticsearch 中安装 ingest-user-agent ingest-geoip插件
# 其中, ingest-user-agent.tar ingest-geoip.tar 解压到 plugins
#ingest-geoip-conf.tar 解压到 config 下 houd 

请求几次nginx,让filebeat读取nginx日志文件信息并刷新到elasticsearch中,在elasticsearch可以看到,使用nginx-modul后,返回信息比之前拆的细一点。

其他的 Module 的用法参加官方文档:

 

   

主要采集指标的数据
       定期收集操作系统或应用服务的指标数据
       存储到Elasticsearch中,进行实时分析

3.1  Metricbeat 组成

Metricbeat有2部分组成,一部分是Module,另一部分为Metricset。
Module: 收集的对象,如:mysql、redis、nginx、操作系统等;
Metricset:收集指标的集合,如:cpu、memory(内存)、network(网络)等;

Redis Module 为例:

3.2 Metricbeat部署与收集系统指标

1. tar -zxvf metricbeat-7.10.0-linux-x86_64.tar.gz -C /opt/     mv metricbeat-7.10.0-linux-x86_64/ metricbeat-7.10.0
2. cd metricbeat-7.10.0
3. vim metricbeat.yml   只修改elasticsearch输出路径即可,其他地方保持不变,es分片数可改可不改:

hosts: ["192.168.226.30:9200","192.168.226.31:9200","192.168.226.32:9200"]

metricbeat.config.modules:
  path: ${path.config} /modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
  index.codec: best_compression
setup.kibana:
output.elasticsearch:
  hosts: ["192.168.226.30:9200","192.168.226.31:9200","192.168.226.32:9200" ]
processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

默认系统指标收集开启:

# 启动
./metricbeat -e  当启动之后就会自动收集系统的指标数据发送到esasticsearch中,因为在metricbeat.yml中使用的是modlues,并且在moddules.d目录中,系统modul是开启状态。
 
在esasticsearch中可以看到,由 metricbeat收集并推送的系统指标数据已经过来了:
 

3.3 Metricbeat 的 Module,基本和Filebeat的Module用法一样
./metricbeat modules list # 查看列表

3.4  Nginx Module

在nginx中,需要开启状态查询,才能查询到指标数据

#重新编译nginx
cd /opt/nginx-1.19.4/   执行: ./configure --prefix=/opt/context/nginx --with-http_ssl_module --with-http_stub_status_module
先执行:make  在执行:make install
cd /usr/local/nginx/sbin 执行: ./nginx -V #查询版本信息 nginx状态模块已安装

#配置nginx
cd /usr/local/nginx/conf   vim nginx.conf 添加:

location /nginx-status {
             stub_status on;
             access_log off;

重启nginx : cd /usr/local/nginx/sbin/  执行: ./nginx -s reload  (命令不好使的话,就先停止nginx: ./nginx -s stop 在启动 ./nginx)

访问刚刚配置的 /nginx-status

结果说明:

Active connections:正在处理的活动连接数
server accepts handled requests
        第一个 server 表示Nginx启动到现在共处理了15个连接
        第二个 accepts 表示Nginx启动到现在共成功创建 15 次握手
        第三个 handled requests 表示总共处理了 23次请求
        请求丢失数 = 握手数 - 连接数 ,可以看出目前为止没有丢失请求
Reading: 0 Writing: 1 Waiting: 1
        Reading:Nginx 读取到客户端的 Header 信息数
        Writing:Nginx 返回给客户端 Header 信息数
        Waiting:Nginx 已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于Active - (Reading+Writing)

3.4.1  配置 Nginx Module
# 启用redis module     cd /opt/metricbeat-7.10.0
./metricbeat modules enable nginx
# 修改 redis module配置  cd /opt/metricbeat-7.10.0/modules.d
vim nginx.yml    

- module: nginx
  #metricsets:
  #  - stubstatus
  period: 10s
  # Nginx hosts
  #hosts: ["http://127.0.0.1"]
  hosts: ["http://192.168.226.30"]
  # Path to server status. Default server-status
  #server_status_path: "server-status"
  server_status_path: "nginx-status"
  #username: "user"
  #password: "secret"

#启动  cd /opt/metricbeat-7.10.0
./metricbeat -e
测试:请求nginx,在ES中查看:
可以看到, nginx 的指标数据已经写入到了Elasticsearch。
更多的 Module 使用参见官方文档:
https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-modules.html
 
4.  Kibana

Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作。您可以使用 Kibana 对 Elasticsearch 索引中的数据进行搜索、查看、交互操作。您可以很方便的利用图表、表格及地图对数据进行多元化的分析和呈现。
4.1 配置安装

# 1. 下载(注意下载和Elasticsearch相同的版本,不然不兼容无法启动)上传至服务器,
解压: tar -zxvf kibana-7.9.3-linux-x86_64.tar.gz -C /opt/
# 2. 修改配置文件   cd /opt/  mv kibana-7.9.3-linux-x86_64/ kibana-7.9.3  cd kibana-7.9.3/
vim config/kibana.yml
server.host: "192.168.226.30"                                      # 对外暴露服务的地址
elasticsearch.hosts: ["http://192.168.226.30:9200","http://192.168.226.31:9200","http://192.168.226.32:9200"]   # 配置 Elasticsearch
# 3.  启动
./bin/kibana  提示不能用root启动,换个账号启动  chown -R elsearch:elsearch kibana-7.9.3/   cd kibana-7.9.3/  su elsearch  ./bin/kibana
启动会有几个警告:
1. 在kibana.yml末尾添加配置:

xpack.security.encryptionKey: "something_at_least_32_characters"     # 任意32位字符串
xpack.reporting.encryptionKey: "something_at_least_32_characters"   # 任意32位字符串
xpack.encryptedSavedObjects.encryptionKey: "something_at_least_32_characters"  # 任意32位字符串
xpack.reporting.capture.browser.chromium.disableSandbox: false       # 关掉沙箱
 
2. 在es中将索引 .kibana_task_manager_1 删除
再次启动,第一次启动非常耗时:
 
# 4.  通过浏览器进行访问
# 5 关闭kibana  fuser -n tcp 5601 kill -9 pid 或者 ps -ef | grep node kill -9 pid

4.2  功能说明
4.3  数据探索

创建索引之后,稍等一会儿就可以在仪表盘看到性能指标的数据:

我随便添加了几个,太多了,这里就是将es中的数据可视化的一个操作

6.4 Metricbeat 仪表盘安装到Kibana
# 修改 metricbeat.yml 配置
setup.kibana:
   host: "192.168.226.30:5601"
# 安装仪表盘到 Kibana
./metricbeat setup --dashboards   安装的时候确保Kibana处于运行状态

重新将metricbeat运行起来: ./metricbeat -e  让它不断地产生数据。

在kibana中点击仪表盘

这些就是刚刚安装好的仪表盘

找到 [Metricbeat System] Host Services Overview ,点进去就可以看到系统的一些数据信息

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值