
Elasticsearch Exporter
本页内容
简介
以下快速入门指南提供了 Elasticsearch Exporter 的设置说明和预配置的仪表盘、告警规则和记录规则。完成本快速入门指南中的步骤后,您将拥有
已设置和配置 Elasticsearch Exporter 以收集 Elasticsearch 指标,例如集群状态、活动分片、JVM 指标、Elasticsearch 负载。Elasticsearch Exporter 将把这些指标以 Prometheus 风格的指标形式公开。
已配置 Prometheus 以抓取 Elasticsearch Exporter 指标,并可选择将其发送到 Grafana Cloud。
已设置一组预配置和精心策划的记录规则,以缓存常用的 Prometheus 查询。
已导入 Grafana 仪表盘以可视化您的指标数据。
已设置 Prometheus 告警规则,以便在您的指标数据上发出警报。
指标使用
此导出器默认发布大约 392 个 Prometheus 时间序列。要查看此导出器默认提供的指标列表,请在此处下载示例指标抓取。
请注意,根据其配置,Elasticsearch Exporter 可能会收集和发布远多于此默认集的指标。要了解有关配置 Elasticsearch Exporter 和切换其收集器的更多信息,请参阅 Elasticsearch Exporter GitHub 存储库。
除了切换 Elasticsearch Exporter 的设置外,您还可以通过删除不需要存储在 Prometheus 或 Grafana Cloud 中的时间序列来减少指标使用量。要了解如何执行此操作,请参阅 Grafana Cloud 文档中的使用重新标记减少 Prometheus 指标使用量。
Grafana Cloud 的 Elasticsearch 集成
如果您正在使用 Grafana Cloud,则可以通过安装 Elasticsearch Exporter 集成来跳过本指南中的所有步骤,该集成旨在帮助您在几个命令和点击中启动并运行。免费注册。
要了解如何使用 Elasticsearch Exporter 集成设置 Elasticsearch Exporter,请参阅 Grafana Cloud 文档中的Elasticsearch Exporter 集成。
本页内容
Elasticsearch Exporter 快速入门
在本指南中,您将学习如何设置和配置 Elasticsearch Exporter 以收集 Elasticsearch 指标,例如集群状态、活动分片、JVM 指标、Elasticsearch 负载,并将它们作为 Prometheus 风格的指标公开。然后,您将配置 Prometheus 以抓取 Elasticsearch 指标,并可选择将其发送到 Grafana Cloud。最后,您将设置一组预配置和精心策划的记录规则、Grafana 仪表盘和告警规则。在本指南结束时,您将拥有可用于可视化 Elasticsearch 指标的仪表盘,以及一组预配置的告警。
如果您正在使用 Grafana Cloud,Elasticsearch 集成可以帮助您快速启动并运行。Elasticsearch 集成将 Elasticsearch Exporter 嵌入到 Grafana Cloud Agent 中,并自动配置告警规则和仪表盘,因此您不必完成本指南中的步骤。要了解如何使用 Elasticsearch 集成设置 Elasticsearch Exporter,请参阅 Grafana Cloud 文档中的Elasticsearch 集成。
先决条件
在开始之前,您应该准备好以下内容
- 与 Elasticsearch Exporter 版本兼容的机器。要查看可用版本列表,请参阅版本。
- 在您的机器上运行的 Elasticsearch。要了解有关安装和运行 Elasticsearch 的更多信息,请参阅Elasticsearch 安装指南。
- 在您的环境或直接在机器上运行的 Prometheus。要了解如何安装 Prometheus,请参阅 Prometheus 文档中的安装。
- 在您的环境或直接在机器上运行的 Grafana。要了解如何安装 Grafana,请参阅 Grafana 文档中的安装 Grafana。
- (可选)Grafana Cloud 帐户。Grafana Cloud 托管 Grafana 和一个基于 Mimir 的 Prometheus 指标端点。您仍然需要抓取指标,可以使用安装在您环境中的 Prometheus,或者 Grafana Cloud Agent。要了解有关 Grafana Cloud 的更多信息,请参阅Grafana Cloud。
步骤 1:设置 Elasticsearch Exporter
在此步骤中,您将在您的机器上设置 Elasticsearch 导出器,以收集 Prometheus 格式的 Elasticsearch 指标并公开它们。本指南使用 Ubuntu 20.04 系统和 Elasticsearch 7.12.0。步骤可能会因您的操作系统和 Elasticsearch 版本而略有不同。
首先,登录到您的机器并下载相关的 Elasticsearch Exporter 二进制文件。本指南使用 linux-amd64
二进制文件,但您应该选择与您的系统操作系统和架构相对应的文件
wget https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.1.0/elasticsearch_exporter-1.1.0.linux-amd64.tar.gz
将 v1.1.0
替换为您要安装的版本。本指南可能过时,因此最好查看 Elasticsearch Exporter 版本页面以获取最新的稳定版本。
解压 tarball 并 cd
进入该目录
tar xvfz elasticsearch_exporter-1.1.0.linux-amd64.tar.gz
cd elasticsearch_exporter-1.1.0.linux-amd64
如果您在同一台 Linux 机器上运行 Elasticsearch,则您的 Elasticsearch 端点将为 https://127.0.0.1:9200
。根据您的设置,这可能会有所不同,并且可以使用 --es.uri
标志进行设置。
最后,运行导出器
./elasticsearch_exporter --es.uri="https://127.0.0.1:9200"
level=info ts=2021-03-31T10:39:42.180223026Z caller=clusterinfo.go:200 msg="triggering initial cluster info call"
level=info ts=2021-03-31T10:39:42.180423443Z caller=clusterinfo.go:169 msg="providing consumers with updated cluster info label"
level=info ts=2021-03-31T10:39:42.18398451Z caller=main.go:148 msg="started cluster info retriever" interval=5m0s
level=info ts=2021-03-31T10:39:42.184156478Z caller=main.go:188 msg="starting elasticsearch_exporter" addr=:9114
如果您看到以上输出,则说明您已成功运行 Elasticsearch Exporter。
Elasticsearch Exporter 在端口 9114 上以 Prometheus 格式发布 Elasticsearch 指标。您可以使用 curl
测试这一点。您需要打开一个新的 SSH 会话或在后台运行 Elasticsearch Exporter 进程才能使用 curl
。
curl https://127.0.0.1:9114/metrics
# HELP elasticsearch_node_stats_up Was the last scrape of the ElasticSearch nodes endpoint successful.
# TYPE elasticsearch_node_stats_up gauge
elasticsearch_node_stats_up 1
# HELP elasticsearch_nodes_roles Node roles
# TYPE elasticsearch_nodes_roles gauge
elasticsearch_nodes_roles{cluster="es-docker-cluster",host="172.29.0.2",name="es01",role="client"} 1
elasticsearch_nodes_roles{cluster="es-docker-cluster",host="172.29.0.2",name="es01",role="data"} 1
elasticsearch_nodes_roles{cluster="es-docker-cluster",host="172.29.0.2",name="es01",role="ingest"} 1
elasticsearch_nodes_roles{cluster="es-docker-cluster",host="172.29.0.2",name="es01",role="master"} 1
# HELP elasticsearch_os_cpu_percent Percent CPU used by OS
# TYPE elasticsearch_os_cpu_percent gauge
elasticsearch_os_cpu_percent{cluster="es-docker-cluster",es_client_node="true",es_data_node="true",es_ingest_node="true",es_master_node="true",host="172.29.0.2",name="es01"} 0
# HELP elasticsearch_os_load1 Shortterm load average
# TYPE elasticsearch_os_load1 gauge
elasticsearch_os_load1{cluster="es-docker-cluster",es_client_node="true",es_data_node="true",es_ingest_node="true",es_master_node="true",host="172.29.0.2",name="es01"} 0
# HELP elasticsearch_os_load15 Longterm load average
# TYPE elasticsearch_os_load15 gauge
elasticsearch_os_load15{cluster="es-docker-cluster",es_client_node="true",es_data_node="true",es_ingest_node="true",es_master_node="true",host="172.29.0.2",name="es01"} 0.05
# HELP elasticsearch_os_load5 Midterm load average
# TYPE elasticsearch_os_load5 gauge
elasticsearch_os_load5{cluster="es-docker-cluster",es_client_node="true",es_data_node="true",es_ingest_node="true",es_master_node="true",host="172.29.0.2",name="es01"} 0
# HELP elasticsearch_os_mem_actual_free_bytes Amount of free physical memory in bytes
# TYPE elasticsearch_os_mem_actual_free_bytes gauge
elasticsearch_os_mem_actual_free_bytes{cluster="es-docker-cluster",es_client_node="true",es_data_node="true",es_ingest_node="true",es_master_node="true",host="172.29.0.2",name="es01"} 0
如果您看到以上输出,则说明您已准备好开始使用 Prometheus 抓取 Elasticsearch 指标。
为了避免从命令行运行和管理 Elasticsearch Exporter,您可以创建一个 systemd
服务。要了解如何执行此操作,请参阅创建 systemd 服务以管理 Agent。将 Agent 二进制文件的路径替换为 Elasticsearch Exporter 的路径。
Elasticsearch 导出器提供分片级统计信息、集群快照统计信息等功能。要了解有关这些功能的更多信息,请参阅Elasticsearch Exporter GitHub 存储库。
步骤 2:使用 Prometheus 抓取 Elasticsearch Exporter
现在 Elasticsearch Exporter 已经在您的机器上启动并运行,您可以配置 Prometheus 抓取作业以收集和存储 Elasticsearch Exporter 指标。
将以下抓取作业配置添加到您的 prometheus.yml
配置文件的 scrape_configs
部分
- job_name: elasticsearch
static_configs:
- targets: ['elasticsearch_exporter_machine_IP_address:9114']
将 elasticsearch_exporter_machine_IP_address
替换为运行 Elasticsearch Exporter 的机器的 IP 地址。如果您在同一台机器上运行 Prometheus,则这将是 localhost
。要了解有关配置 Prometheus 的更多信息,请参阅 Prometheus 文档中的配置。
如果您没有 prometheus.yml
配置文件,请使用您喜欢的文本编辑器创建一个简单的配置文件。打开您首选的文本编辑器并粘贴以下 Prometheus 配置
global:
scrape_interval: 15s
scrape_configs:
- job_name: elasticsearch
static_configs:
- targets: ['elasticsearch_exporter_machine_IP_address:9114']
此配置告诉 Prometheus 每 15 秒抓取所有作业。唯一配置的抓取作业名为 elasticsearch
,并定义了一个 elasticsearch_exporter_machine_IP_address:9114
目标。默认情况下,Prometheus 将使用 HTTP 抓取 /metrics
端点。
保存并关闭文件。然后,您可以使用以下命令使用该文件运行 Prometheus
./prometheus --config.file=./prometheus.yml
将指标发送到 Grafana Cloud
要将 MySQL Exporter 指标从 Prometheus 发送到 Grafana Cloud,请在您的 prometheus.yml
配置文件中配置 remote_write
参数。要了解更多信息,请参阅 Grafana Cloud 文档中的指标 — Prometheus。要了解有关 remote_write
参数的更多信息,请参阅 Prometheus 文档中的remote_write
。
步骤 3:配置记录规则
使用记录规则,您可以预先计算和缓存频繁查询的指标。例如,如果仪表盘面板使用计算密集型查询(如 rate()
),您可以创建一个记录规则,该规则以常规的较短间隔运行,并将密集型查询的结果保存在新的时间序列中。这避免了每次仪表盘刷新时都获取和计算数据。要了解有关 Prometheus 记录规则的更多信息,请参阅 Prometheus 文档中的记录规则。
您应该在本指南中加载仪表盘之前加载以下记录规则。 仪表盘查询和告警规则使用记录规则来减少 Prometheus 或 Grafana Cloud Metrics 服务器上的负载,具体取决于您评估规则的位置。
您可以在此处获取记录规则 YAML 文件。
此记录规则 YAML 文件是使用 Elasticsearch Exporter mixin 生成的。
将记录规则加载到 Prometheus 中
要将记录规则加载到 Prometheus 中,请将以下内容添加到您的 prometheus.yml
配置文件
rule_files:
- "elasticsearch_exporter_recording_rules.yml"
请务必将 elasticsearch_exporter_recording_rules.yml
替换为您的 Elasticsearch Exporter 记录规则 YAML 文件的路径。
将记录规则加载到 Grafana Cloud 中
要了解如何将记录规则加载到 Grafana Cloud 中,请参阅使用 cortextool 的 Prometheus 和 Loki 规则。
步骤 4:配置仪表盘
本快速入门包括一个仪表盘
- Elasticsearch 概览
要了解如何将这些仪表盘导入到 Grafana 中,请参阅 Grafana 文档中的导入仪表盘。
这些仪表盘查询可能取决于上一步中定义的记录规则。请务必在导入仪表盘之前导入这些规则。
您可以在此处获取仪表盘。
步骤 5:配置告警
使用 Prometheus 告警规则,您可以定义当 PromQL 表达式超过某个阈值或在一段时间内满足指定条件时触发的告警。例如,您可以定义一个 HighRequestLatency
告警,当请求延迟指标在一段时间内大于某个阈值时触发该告警。一旦告警条件被触发,告警将进入 Pending
状态。在满足 for
参数定义的时间段的条件后,告警将进入 Firing
状态。您可以使用 Alertmanager 等工具为触发告警配置路由和通知。Alertmanager 也内置于 Grafana Cloud 中。
您可以在此处获取告警规则 YAML 文件。
将告警规则加载到 Prometheus 中
要将告警规则加载到 Prometheus 中,请将以下内容添加到您的 prometheus.yml
配置文件
rule_files:
- "elasticsearch_exporter_alerting_rules.yml"
请务必将 elasticsearch_exporter_alerting_rules.yml
替换为您的 Elasticsearch 告警规则 YAML 文件的路径。
将告警规则加载到 Grafana Cloud 中
要了解如何将告警规则加载到 Grafana Cloud 中,请参阅使用 cortextool 的 Prometheus 和 Loki 规则。
结论
在本快速入门中,您在 Linux 机器上安装并运行了 Elasticsearch Exporter。然后,您配置了 Prometheus 以抓取数据库和 Elasticsearch Exporter 公开的 Elasticsearch 指标。您将记录规则和告警规则加载到 Prometheus 中,最后导入了 Grafana 仪表盘以可视化您的 Elasticsearch 指标。
如果您正在使用 Grafana Cloud,则可以通过安装 Grafana Cloud Agent 的 Elasticsearch 集成来跳过本指南中的所有步骤。此集成将预配置的 Elasticsearch Exporter 嵌入到 Agent 中,并自动配置 Grafana 仪表盘和 Prometheus 告警和记录规则,因此您不必手动导入它们。要了解如何设置 Elasticsearch 集成,请参阅Grafana Cloud 集成。
仪表盘、记录规则和告警规则是使用 Elasticsearch Exporter Mixin 生成的。Mixin 是由主题 matter 专家策划和设计的用于仪表盘、记录规则和告警的可重用模板。要了解更多信息,请参阅Elasticsearch Mixin 存储库。
本页内容
您应该在本指南中加载仪表盘之前加载以下记录规则。 仪表盘查询使用记录规则来减少 Prometheus 或 Grafana Cloud Metrics 服务器上的负载,具体取决于您评估规则的位置。
本快速入门包括以下记录规则
elasticsearch_process_cpu_high
elasticsearch_os_cpu_high
elasticsearch_yellow_cluster_status
elasticsearch_red_cluster_status
elasticsearch_filesystem_data_used_percent
elasticsearch_filesystem_data_free_percent
elasticsearch_node_disk_watermark_reached
elasticsearch_heap_utilization_percentage
groups:
- name: elasticsearch_rules
rules:
- record: elasticsearch_filesystem_data_free_percent
expr: 100 - elasticsearch_filesystem_data_used_percent
- record: elasticsearch_red_cluster_status
expr: sum by (cluster) (elasticsearch_cluster_health_status == 2)
- record: elasticsearch_yellow_cluster_status
expr: sum by (cluster) (elasticsearch_cluster_health_status == 1)
- record: elasticsearch_process_cpu_high
expr: sum by (cluster, instance, name) (elasticsearch_process_cpu_percent)
- record: elasticsearch_os_cpu_high
expr: sum by (cluster, instance, name) (elasticsearch_os_cpu_percent)
- record: elasticsearch_filesystem_data_used_percent
expr: sum by (cluster, instance, name) (
100 * (elasticsearch_filesystem_data_size_bytes - elasticsearch_filesystem_data_free_bytes)
/ elasticsearch_filesystem_data_size_bytes)
- record: elasticsearch_node_disk_watermark_reached
expr: sum by (cluster, instance, name) (round(
(1 - (elasticsearch_filesystem_data_available_bytes / elasticsearch_filesystem_data_size_bytes)
) * 100, 0.001))
- record: elasticsearch_heap_utilization_percentage
expr: sum by (cluster, instance, name) (
100 * (elasticsearch_jvm_memory_used_bytes{area="heap"} / elasticsearch_jvm_memory_max_bytes{area="heap"}))
此记录规则 YAML 文件是使用 Elasticsearch Exporter mixin 生成的。
本页内容
注意:这些仪表盘可能取决于上一选项卡中的记录规则。请务必在导入仪表盘之前导入这些规则。
Elasticsearch 概览仪表盘
此仪表盘包括以下指标的面板
- 集群健康状况
- CPU 和内存
- 磁盘使用率
- 网络使用率
- 平均负载
- JVM 内存使用率
- JVM 垃圾回收
- 断路器跳闸次数
- 节点
- 待处理任务
- 每个集群的打开文件描述符数
- 活动主分片
- 活动、正在初始化、正在重新定位和延迟的分片
- 事务日志
- 断路器
- 文档总数和大小
- 节点上的文档计数
- 文档索引、删除和合并速率
- 文档合并字节数
- 查询、索引和合并时间
- 索引存储的节流时间
- 总操作速率、时间和统计信息
- 线程池
- 缓存
- 段
您可以使用以下仪表盘 ID 直接导入仪表盘:14191
您可以在此处获取仪表盘 JSON。
此仪表盘是使用 Elasticsearch Exporter mixin 生成的。
本页内容
本快速入门包括以下告警规则
ElasticsearchTooFewNodesRunning
运行的 Elasticsearch 节点少于预期
ElasticsearchHeapTooHigh
堆内存使用率过高
ElasticsearchClusterNotHealthy
黄色或红色 Elasticsearch 集群状态
ElasticsearchNodeDiskWatermarkReached
磁盘存储空间即将耗尽
ElasticsearchProcessCPUHigh
Elasticsearch 进程使用的 CPU 过多
SystemCPUHigh
Elasticsearch 节点使用的 CPU 过多
下载以下告警规则 YAML 文件groups:
- name: elasticsearch-alerts
rules:
- alert: ElasticsearchTooFewNodesRunning
expr: elasticsearch_cluster_health_number_of_nodes < 3
for: 5m
annotations:
description: "There are only {{ $value }} < 3 ElasticSearch nodes running"
summary: ElasticSearch running on less than 3 nodes
labels:
severity: critical
- alert: ElasticsearchHeapTooHigh
expr: elasticsearch_heap_utilization_percentage > 90
for: 15m
annotations:
description: The heap usage is over 90% for 15m
summary: "ElasticSearch node {{ $labels.name }} heap usage is high"
labels:
severity: critical
- alert: ElasticsearchClusterNotHealthy
expr: elasticsearch_red_cluster_status
for: 2m
annotations:
message: "Cluster {{ $labels.cluster }} health status has been RED for at least 2m. Cluster does not accept writes, shards may be missing or master node hasn't been elected yet."
summary: Cluster health status is RED
labels:
severity: critical
- alert: ElasticsearchClusterNotHealthy
expr: elasticsearch_yellow_cluster_status
for: 20m
annotations":
message": "Cluster {{ $labels.cluster }} health status has been YELLOW for at least 20m. Some shard replicas are not allocated."
summary": Cluster health status is YELLOW
labels:
severity: warning
- alert: ElasticsearchNodeDiskWatermarkReached
expr: elasticsearch_node_disk_watermark_reached > 85
for: 5m
annotations:
message: "Disk Low Watermark Reached at {{ $labels.node }} node in {{ $labels.cluster }} cluster. Shards can not be allocated to this node anymore. You should consider adding more disk to the node."
summary: "Disk Low Watermark Reached - disk saturation is {{ $value }}%"
labels:
severity: warning
- alert: ElasticsearchNodeDiskWatermarkReached
expr: elasticsearch_node_disk_watermark_reached > 90
for: 5m
annotations:
message: "Disk High Watermark Reached at {{ $labels.node }} node in {{ $labels.cluster }} cluster. Some shards will be re-allocated to different nodes if possible. Make sure more disk space is added to the node or drop old indices allocated to this node."
summary: "Disk High Watermark Reached - disk saturation is {{ $value }}%"
labels:
severity: critical
- alert: ElasticsearchJVMHeapUseHigh
expr: elasticsearch_heap_utilization_percentage > 75
for: 10m
annotations:
message: "JVM Heap usage on the node {{ $labels.node }} in {{ $labels.cluster }} cluster is {{ $value }}%."
summary: JVM Heap usage on the node is high
labels:
severity: critical
- alert: SystemCPUHigh
expr: elasticsearch_os_cpu_high > 90
for: 1m
annotations":
message: "System CPU usage on the node {{ $labels.node }} in {{ $labels.cluster }} cluster is {{ $value }}%"
summary: System CPU usage is high
labels:
severity: critical
- alert: ElasticsearchProcessCPUHigh
expr: elasticsearch_process_cpu_high > 90
for: 1m
annotations:
message: "ES process CPU usage on the node {{ $labels.node }} in {{ $labels.cluster }} cluster is {{ $value }}%"
summary: ES process CPU usage is high
labels:
severity: critical
此告警规则 YAML 文件是使用 Elasticsearch Exporter mixin 生成的。
本页内容
Grafana Cloud 附带不断扩展的集成集,可在几分钟内快速启动并运行可观测性堆栈。内置于 Grafana Cloud Agent 的 Elasticsearch 集成公开并抓取必要的 Elasticsearch 指标,并将它们推送到 Grafana Cloud。Agent 将使用嵌入式 Elasticsearch Exporter 抓取指标,Grafana Cloud 将自动配置定制的 Grafana 仪表盘和告警,以便可视化和处理这些数据。
要了解更多信息,请查看 Grafana Cloud 文档。
工作原理
配置、安装、连接和维护 Prometheus 监控组件通常涉及大量的领域知识。从设置到仪表盘和告警可能需要相当长的时间。作为 Grafana 的创建者 - 以及 Prometheus 和 Cortex 的核心贡献者 - 我们构建简单的集成来抽象出一些工作,以便快速开始使用。工作原理
- 注册(或登录)免费 Grafana Cloud 帐户。
- 选择您要观察的目标(不断扩展的目录)。
- 运行单行命令以安装 Grafana Agent。Agent 嵌入并预配置 Exporter 以公开默认指标,并将它们推送到 Grafana Cloud 指标后端。
- 瞧!您将看到定制的 Grafana 仪表盘,并将受益于合理的默认告警。
正在寻找不同的 Exporter 或集成?查看我们不断增长的流行组件集成库,例如 MySQL、Postgres、Redis、Memcached 等。