如果你没有听说过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
https://www.elastic.co/guide/en/beats/filebeat/7.10/elasticsearch-output.html
#启动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.1nginx 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的用法参加官方文档:
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html
3、Metricbeat  https://www.elastic.co/cn/beats/metricbeat 

主要采集指标的数据
       定期收集操作系统或应用服务的指标数据
       存储到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
https://www.elastic.co/cn/kibana

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

# 1. 下载(注意下载和Elasticsearch相同的版本,不然不兼容无法启动)上传至服务器,
https://www.elastic.co/cn/downloads/past-releases#kibana
解压: 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
更多kibana配置:https://www.elastic.co/guide/en/kibana/7.9/settings.html
# 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. 通过浏览器进行访问
http://192.168.226.30:5601/app/home#/
# 5. 关闭kibana fuser -n tcp 5601 kill -9 pid 或者 ps -ef | grep node kill -9 pid

4.2 功能说明
4.3 数据探索
首先先添加索引信息:http://192.168.226.30:5601/app/management/kibana/indexPatterns/create

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

我随便添加了几个,太多了,这里就是将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 ,点进去就可以看到系统的一些数据信息

Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程相关推荐

  1. 如何在Ubuntu 18.04上安装Elasticsearch Logstash Kibana(Elastic Stack)

    In this guide, you will learn to install Elastic stack on Ubuntu 18.04. Elastic stack, formerly know ...

  2. The Elastic Stack (ELK)7.14:简单配置到SSL配置教程(带Beats部署)

    文章目录 一.简介 二.环境 1.安装Docker.Docker Compose 三.部署Elastic Stack(ELK) 1.部署ElasticSearch(Distributed, RESTf ...

  3. 搭建 ELK 集群 kafka filebeat kibana logstash zookeeper elasticsearch

    文章目录 一.前置准备 1. 部署总览 2. 技术选型 3. 全局配置 4. 软件下载 5. 操作声明 二.jdk安装 2.1. 上传 2.2. 解压 2.3. 设置环境变量 三.zk集群 3.1. ...

  4. 【Elastic Stack学习】ELK日志分析平台(一)ELK简介、ElasticSearch集群

    * ELK简介: ELK是Elasticsearch . Logstash.Kibana三个开源软件的缩写.ELK Stack 5.0版本之后新增Beats工具,因此,ELK Stack也改名为Ela ...

  5. Elastic:使用 Elastic Stack 来监督系统日志及指标

    在我之前的许多文章中,我基本上都已经讲到了这些方面的内容.在今天的文章中,我想针对一些开发还没有自己的系统,比如 centos 或 Ubuntu OS 来写一篇非常详细的文章.在这篇文章中,我将详述: ...

  6. 云计算企业级小架构部署应用综合练习-二- Ansible 部署 Elastic Stack(ELK)

    一.Elastic Stack 项目介绍 ⚠️警告 1 这不是零基础的教程,需要掌握 Ansible 和 ELK 各个组件的使用 2 这个只是提供了一套自动化部署 ELK 系统的解决方案 3作为练习之 ...

  7. Elastic Stack之EBK安装

    The Elastic Stack包括 Elasticsearch.Kibana.Beats 和 Logstash(也称为 ELK Stack),能够安全可靠地获取任何来源.任何格式的数据,然后实时地 ...

  8. 十、Docker快速搭建Elastic Stack(下篇)

    @Author : By Runsen @Date : 2020/6/19 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘 ...

  9. Elastic Stack技术栈实践与Filebeat+Kibana企业级案例实战

    Elastic Stack技术栈实践与Filebeat+Kibana企业级案例实战 全新ELK企业级应用实战教程 Elastic Stack技术栈实践基于企业级Elasticsearch应用与Elas ...

最新文章

  1. 【廖雪峰python入门笔记】while循环
  2. 学python买什么书好-学python3什么书好
  3. Java 序列化Serializable详解(附详细例子)
  4. 如何在 JS 代码中消灭 for 循环
  5. 为什么说 Serverless 引领云的下一个十年?
  6. python字符串函数运算_Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 原创...
  7. hook xposed 自定义类_【开始学习React Hook(1)】Hook之useState
  8. springmvc(18)使用WebSocket 和 STOMP 实现消息功能
  9. 【渝粤题库】广东开放大学 跨境电商实务之搜索引擎 形成性考核
  10. 潜在狄利克雷分配(Latent Dirichlet Allocation,LDA)
  11. 数据库系统实训——实验七——触发器
  12. C++最小函数模板demo
  13. mysql截止5.7版本全部异常汇总 Error SQLSTATE 中英文对照
  14. 数据库三大范式、BCNF范式、反范式
  15. 互联网广告表现形式有哪几种?
  16. 【HNOI 2018】毒瘤
  17. 广科院机器人团队邹子平分享学习知识
  18. python培训费用多少钱?学习python课程价格?
  19. java会导致电脑黑屏吗,电脑开机黑屏只显示鼠标怎么办
  20. 【春秋云境】CVE-2015-1427靶场wp

热门文章

  1. 惠普刀片服务器型号,HP ProLiant刀片服务器简介
  2. 微型计算机3月2017,2017年3月计算机一级基础及MSOffice强化习题
  3. 如何检验有调节的中介作用?
  4. 一招教你在Linux命令行下测网速
  5. android声音播放函数双声道合并,Android音频编辑之音频合成功能
  6. 如何创建一个微信公众号(手把手、超详细)
  7. 用PowerBI进行数据分析的基本流程框架
  8. 新零售线上+线下的完美营销
  9. 编程小TIPS:使用函数式风格Either来编程
  10. 拍沪牌服务器响应,上海虹口代拍沪牌费用,百兆光线实时响应