# Prometheus + Grafana

URL: https://caijiao.org/elasticsearch/05.admin/08.monitoring-prometheus
Source: docs/elasticsearch/05.admin/08.monitoring-prometheus.md
Description: 本章介绍如何通过 Prometheus 收集 Elasticsearch 指标，并使用 Grafana 实现可视化展示，是构建开源监控方案的推荐实践。

在开源环境中，**Prometheus + Grafana** 是构建高可定制化监控平台的主流组合。Elasticsearch 提供了 Exporter 工具，可将运行时指标暴露给 Prometheus 收集，并通过 Grafana 仪表盘进行直观展示。

---

## 1. 架构概览

```

Elasticsearch → Exporter → Prometheus → Grafana

````

- Elasticsearch 通过 Exporter 暴露 `/metrics` 接口  
- Prometheus 周期性拉取 Exporter 指标数据  
- Grafana 使用 PromQL 从 Prometheus 获取数据并进行可视化

---

## 2. 安装 Elasticsearch Exporter

推荐使用 [prometheus/elasticsearch_exporter](https://github.com/prometheus-community/elasticsearch_exporter)

```bash
docker run -d \
  -p 9114:9114 \
  --name es-exporter \
  quay.io/prometheuscommunity/elasticsearch-exporter \
  --es.uri=http://elasticsearch:9200
````

> ⚠️ 若启用认证，请添加 `--es.username` 和 `--es.password` 参数。

---

## 3. 配置 Prometheus

在 `prometheus.yml` 中添加：

```yaml
scrape_configs:
  - job_name: 'elasticsearch'
    static_configs:
      - targets: ['es-exporter:9114']
```

重启 Prometheus 服务：

```bash
docker restart prometheus
```

---

## 4. 配置 Grafana 仪表盘

1. 添加 Prometheus 数据源
2. 导入社区模板仪表盘（例如 ID： 4358 或 16277）
3. 常见可视化指标包括：

| 指标名称                             | 说明                       |
| -------------------------------- | ------------------------ |
| `elasticsearch_cluster_health`   | 集群健康状态（green/yellow/red） |
| `elasticsearch_indices_docs`     | 索引文档数量                   |
| `elasticsearch_jvm_memory_*`     | JVM 内存使用                 |
| `elasticsearch_indices_search_*` | 查询吞吐量和耗时                 |
| `elasticsearch_thread_pool_*`    | 线程池队列与活动数                |

---

## 5. 示例仪表盘推荐

* [Grafana.com Dashboard 4358](https://grafana.com/grafana/dashboards/4358-elasticsearch-cluster-monitoring/)
* [Dashboard 16277 - ElasticSearch 7+ exporter](https://grafana.com/grafana/dashboards/16277)
* 也可自定义图表：结合 PromQL 表达式创建业务维度图

---

## 6. 安全与认证（可选）

* 若启用了 Elasticsearch TLS / 认证，可在 exporter 启动参数中添加：

```bash
--es.uri=https://elasticsearch:9200
--es.all --es.ssl-skip-verify \
--es.username=monitoring_user \
--es.password=yourpassword
```

* 推荐为 Exporter 创建专属角色，仅授予 `monitor` 权限：

```json
PUT /_security/role/monitoring_role
{
  "cluster": ["monitor"]
}
```

---

## 7. 监控示例图（可选自行添加）

你可以展示以下信息：

* JVM 内存趋势图
* 搜索/写入 QPS
* 分片健康状态
* 线程池积压告警

---

## 8. 与 Stack Monitoring 的对比

| 维度        | Stack Monitoring | Prometheus + Grafana |
| --------- | ---------------- | -------------------- |
| 来源方式      | 内建 / Metricbeat  | Exporter 拉取          |
| 配置灵活性     | 固定视图为主           | 高度自定义                |
| 兼容组件      | Elastic Stack 全套 | Prometheus 生态兼容更广    |
| 企业功能（ML等） | 支持               | 不支持                  |
| 开源可控性     | 部分功能需商业授权        | 完全开源                 |

---

通过 Prometheus + Grafana 方案，你可以灵活构建针对 Elasticsearch 的性能监控平台，并轻松接入其他服务如 Redis、Kafka、Nginx 等，统一观测系统运行状况。
