# 使用 Metricbeat 掌握 Elastic Stack 的健康狀態

### 本篇學習重點

* 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](https://i.imgur.com/Hdm96dC.png)

進入 **Stack Monitoring** 的功能畫面，可以從 Kibana 左方的功能選單，找到 **Management** > **Stack Monitoring**。

![08-kibana-stack-monitoring-entry](https://i.imgur.com/AHySsHr.png)

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 網站](https://https/demo.elastic.co/) 來試玩。

## 監控 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. 使用 `elasticsearch`、`kibana`、`beats` 這種非 `-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： `filebeat`、`metricbeat`、`heartbeat`…等的設定方式都是一樣的，唯一不同的是 `.yml` 的檔名，這部份大家自己去對應一下，以下會以 `filebeat` 為例。

> 小提醒：如果使用同一台主機安裝多種 beats 時，記得 port 不要衝突。

**Beats 端的配置**

由於 Metricbeat 要收集的 Metrics 資訊，在 Beats 也要另外開啟 metrics 資訊的 API，這個部份會要調整 `filebeat.yml`、`heartbeat.yml`、`metricbeat.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](https://www.elastic.co/guide/en/elasticsearch/reference/current/monitoring-settings.html)
2. [官方文件 - Metricbeat Reference](https://www.elastic.co/guide/en/beats/metricbeat/7.14/index.html)
