08 - Metrics - 观察系统的健康指标 (2/6) - 使用 Metricbeat 掌握 Elastic Stack 的健康状态

Metrics - 观察系统的健康指标 系列文章


本篇学习重点

  • Kibana 的 Elastic Stack Monitoring 简介
  • 监控 Elastic Stack 健康状态的实践技巧
  • 如何使用 Metricbeat 收集 Elastic Stack 的健康状态

在进入介绍如何使用 Metrics 掌控我们的 Infrastructure 之前,由於我们使用的是 Elastic Stack 这个解决方案来当作我们 Observability 工具,因此在这边要先介绍如何透过 Metricbeat 来掌控 Elastic Stack 这组工具的健康状态。

Kibana 的 Stack Monitoring

身为 Elastic Stack 的最主要的入口 - Kibana,当然也拥有监控整个 Elastic Stack 的能力, 下图是 Kibana 的 Stack Monitoring 的画面,在这张图当中,我们可以看到 Elastic Stack 的主要产品都能在这个 Dashboard 上观看每个服务的 Metrics 状态,目前这个画面所呈现的资讯,除了我截图当下的 Elastic Stack 还没有串接 Filebeat,因此 Logs 的区块显示为黄色的 No log data found 也就是还没有资料,其他所有看到的资讯都是透过 Metricbeat 所收集到的。

08-kibana-stack-monitoring-overview

进入 Stack Monitoring 的功能画面,可以从 Kibana 左方的功能选单,找到 Management > Stack Monitoring

08-kibana-stack-monitoring-entry

Kibana 的 Stack Monitoring 拥有非常详细的 Elastic Stack 的资讯,以下针对 Observability 相关举例列出说明 (非全部的功能):

  • Elasticsearch: 整个 Cluster 的状态、Node 的状态、Index 状态、即时 indexing 与 searching 存取的数据、shard 的状态、各种 Metrics…等。
  • Kibana: 总共有多少个 Kibana Instance、请求的数量、回应的时间、记忆体用量…等。
  • Logstash: 所有 Nodes 的状态、即时掌握有多少 events 在处理、Pipeline 的数量与执行状况、甚至 Pipeline 里的每个步骤所处理的 event 数都能即时看到。
  • Beats: 各种 Beats 的 events 处理监控、失败率、记忆体用量、资料传输流量…等。
  • APM Server: 掌握所有的 APM Server、收了多少 requests、处理了多少的 events、记忆体用量、资料传输流量…等。

在这边就不细节的介绍每个功能,大家可以从官方文件去查阅,另外也可以使用 Elastic Demo 网站 来试玩。

监控 Elastic Stack 的健康状态的实践技巧

当我们要来监控 Elastic Stack 的健康状态时,这边列了几个小技巧提供大家参考。

尽可能使用独立的 Monitoring Elasticsearch Cluster

如果在规划使用 Elasticsearch 的时候,不是单纯的当作监控使用,而是有用来提供产品特定的服务,例如:产品的搜寻、当成 NoSQL 资料库、当作另一个服务的资料来源,在这样的情况下,就会建议将收集 Monitoring 的资讯这项任务,独立安排另一个 Elasticsearch Cluster 来处理,以避免监控的资料或是人为的操作,影响到正式服务的运作,另外如果有多个 Elasticsearch Cluster 在运作时,也会建议另外安排独立的 Monitoring 专用的 Elasticsearch Cluster,来监控这些 Cluster 的状态。

使用 Metricbeat 收集 Elastic Stack 的服务 Metrics,而不要用服务自己本身的 Monitoring 机制来发送 Metrics 资讯

当服务不稳定的时候,有时是因为主机挂掉,又或是 CPU loading 过重、网路有问题、记忆体不够…等等,一但当这些问题发生时,通常很高的比例服务本身就是无法正常运作,所以如果我们的 Metrics 资讯是由服务本身传送给 Elasticsearch,代表出问题的时候,我们会拿不到关键的数据。

因此会强烈建议使用另外部署的 Metricbeat 来收集服务或系统的 Metrics,不要直接透过 Elasticsearch、Kibana、Beats…等本身所提供的传送 Monitoring 资讯的功能。

Elastic 官方也很重视这件事,所以已经开始准备弃用由服务本身传送 Metric 资讯的功能,建议大家提早全面使用 Metricbeat 来收集这些资讯。

使用独立部署的 Metricbeat 来收集 Metrics

如同前一点所提到的,当问题发生时,很常时候服务所在的主机都有异常,因此不建议直接将 Metricbeat 直接与服务安装在同一台主机上,应该使用独立部署与安装的方式,让 Metricbeat 透过网路收集这些系统或服务的 Metrics。

使用 Metricbeat 监控 Elastic Stack

以下将说明如何使用 Metricbeat 来取得监控 Elastic Stack 的资讯。

设定以使用 Stack Monitoring 来观看 Metrics 数据

要让 Metricbeat 所收集到的 Elastic Stack Metrics 资讯,出现在 Kibana 的 Stack Monitoring 之中,会需要特别的设定,并且 Metricbeat 仅支援以下几种 modules 支援这种设定:

  • Elasticsearch module
  • Beats module
  • Kibana module

在这些有支援的 modules 里,可以有二种设定的选择:

  1. 使用 -xpack 结尾的 module name。
  2. 使用 elasticsearchkibanabeats 这种非 -xpack 结尾的 module,但是把 xpack.enabled: true 开启,并且移除所有额外设定的 metricsets

注意:设定要透过 Stack Monitoring 观看资讯的这些 module,Metricbeat 会将资料传送到 Elasticsearch 的 Index .monitoring-* 储存,而非预设 Metricbeat 的 Index metricbeat-*

设定 Elastic Stack 与 Metricbeat

接下来将各别介绍 Elasti Stack 的设定方式,在每个服务设定的说明,我们会分成两个部份,Metricbeat 端与服务端的配置方式:

设定 Elasticsearch

Elasticsearch 端的配置

由於 Metricbeat 要收集的 Metrics 资讯,其实都在 Elasticsearch 一般的 RESTful API 里了,所以并不需要特别开启另外的 Metrics 专用 API,所以预设的 Elasticsearch 不用特别配置,不过可以特别留意,不要开启 xpack.monitoring.collection.enabled 的设定,以避免 Elasticsearch 自行将 Metrics 资讯传送进 Elasticsearch 的 Cluster 之中。[1]

Metricbeat 端的配置

首先要开启 elasticsearch-xpack module

./metricbeat module enable elasticsearch-xpack

接着在 ./modules.d/elasticsearch-xpack.xml 设定 Elasticsearch 的位置,每个 node 的 host 都要设定好。

- module: elasticsearch
  xpack.enabled: true
  period: 10s
  hosts: ["http://localhost:9200","http://localhost:9201"]

设定完成後,重新启动 metricbeat 即可。

设定 Logstash

Logstash 端的配置

由於 Metricbeat 要收集的 Metrics 资讯,在 Logstash 会要另外开启 metrics 资讯的 API,这个部份会要调整 logstash.yml 的配置档。

# ------------ HTTP API Settings -------------
# Define settings related to the HTTP API here.
#
# The HTTP API is enabled by default. It can be disabled, but features that rely
# on it will not work as intended.
http.enabled: true
#
# By default, the HTTP API is bound to only the host's local loopback interface,
# ensuring that it is not accessible to the rest of the network. Because the API
# includes neither authentication nor authorization and has not been hardened or
# tested for use as a publicly-reachable API, binding to publicly accessible IPs
# should be avoided where possible.
#
# http.host: 127.0.0.1
#
# The HTTP API web server will listen on an available port from the given range.
# Values can be specified as a single port (e.g., `9600`), or an inclusive range
# of ports (e.g., `9600-9700`).
#
http.port: 9600-9700

主要就是把 http.enabled: true 打开,并且指定 http.port

Metricbeat 端的配置

开启 logstash-xpack module

./metricbeat module enable logstash-xpack

接着在 ./modules.d/logstash-xpack.xml 设定 Logstash metrics API 的位置。

- module: logstash
  xpack.enabled: true
  period: 10s
  hosts: ["localhost:9600"]

设定完成後,重新启动 metricbeat 即可。

设定 Beats

Beats 的设定在 Elastic Stack 中的各种 Beats: filebeatmetricbeatheartbeat…等的设定方式都是一样的,唯一不同的是 .yml 的档名,这部份大家自己去对应一下,以下会以 filebeat 为例。

小提醒:如果使用同一台主机安装多种 beats 时,记得 port 不要冲突。

Beats 端的配置

由於 Metricbeat 要收集的 Metrics 资讯,在 Beats 也要另外开启 metrics 资讯的 API,这个部份会要调整 filebeat.ymlheartbeat.ymlmetricbeat.yml …等的配置档。

# =============================== HTTP Endpoint ================================

# Each beat can expose internal metrics through a HTTP endpoint. For security
# reasons the endpoint is disabled by default. This feature is currently experimental.
# Stats can be access through http://localhost:5066/stats . For pretty JSON output
# append ?pretty to the URL.

# Defines if the HTTP endpoint is enabled.
http.enabled: true

# The HTTP endpoint will bind to this hostname, IP address, unix socket or named pipe.
# When using IP addresses, it is recommended to only use localhost.
#http.host: localhost

# Port on which the HTTP endpoint will bind. Default is 5066.
http.port: 5066

主要就是把 http.enabled: true 打开,并且指定 http.port

Metricbeat 端的配置

开启 beat-xpack module

./metricbeat module enable beat-xpack

接着在 ./modules.d/logstash-xpack.xml 设定 Logstash metrics API 的位置。

- module: beat
  xpack.enabled: true
  period: 10s
  hosts: ["http://localhost:5066","http://localhost:5067","http://localhost:5068","http://localhost:5069"]
  #username: "user"
  #password: "secret"

设定完成後,重新启动 metricbeat 即可。

设定 APM Server

APM Server 端的配置

APM Server 的设定方式,其实和 Beats 一样,底层应该是同样的实作方式,要调整 apm-server.yml 配置档。

#=============================== HTTP Endpoint ===============================

# apm-server can expose internal metrics through a HTTP endpoint. For security
# reasons the endpoint is disabled by default. This feature is currently experimental.
# Stats can be access through http://localhost:5066/stats. For pretty JSON output
# append ?pretty to the URL.

# Defines if the HTTP endpoint is enabled.
http.enabled: true

# The HTTP endpoint will bind to this hostname or IP address. It is recommended to use only localhost.
http.host: localhost

# Port on which the HTTP endpoint will bind. Default is 5066.
http.port: 5066

主要就是把 http.enabled: true 打开,并且指定 http.port

Metricbeat 端的配置

开启 beat-xpack module

小提醒:如果 Beats 已经有开启过,就不用再次开启,只要把对应的 port 加入设定档即可

./metricbeat module enable beat-xpack

接着在 ./modules.d/logstash-xpack.xml 设定 Logstash metrics API 的位置。

- module: beat
  xpack.enabled: true
  period: 10s
  hosts: ["http://localhost:5066","http://localhost:5067","http://localhost:5068","http://localhost:5069"]
  #username: "user"
  #password: "secret"

设定完成後,重新启动 metricbeat 即可。

完成设定後,开始使用 Kibana Stack Monitoring 吧!

透过以上的设定配置,就可以透过 Metricbeat 收集 Elastic Stack 的服务状态资讯,接下来打开 Kibana 即可查看 Stack Monitoring。

参考资料

  1. 官方文件 - Elasticsearch Monitoring Settings
  2. 官方文件 - Metricbeat Reference

查看最新 Elasticsearch 或是 Elastic Stack 教育训练资讯: https://training.onedoggo.com
欢迎追踪我的 FB 粉丝页: 乔叔 - Elastic Stack 技术交流
不论是技术分享的文章、公开线上分享、或是实体课程资讯,都会在粉丝页通知大家哦!


<<:  Day 8 浏览器上画图

>>:  Day07【CSS】特异性 Specificity(样式权重)

Day 24 - [Android APP] 02-界面设计

昨天讲完架构面,今天不那麽技术,来讲界面设计。 画面设计上由於介面是设计给长者使用,因此字型较大。 ...

Sass 基础教学 DAY31

Sass 我们应该大部分的人都有听过 那它到底是什麽东西呢?? 我们先来介绍它到底是啥 维基百科ht...

网页变形-30天学会HTML+CSS,制作精美网站

transform属性是变形的意思,可运用在2D及3D变形,可以对网页元素做旋转、缩放、平移、倾斜、...

Flutter基础介绍与实作-Day15 Onboarding、Login、Sign Up范例实作(2)

了解上面的意思後我们就可以开始了喔,再开始写程序前我们先来做个简单的构思吧! Login页面构思:由...

Python for回圈

今天要来教大家for回圈,for回圈在Python也是常常会用到的一种语法,有时候我们会希望让程序中...