跳到主要内容

使用 Prometheus 监控 APISIX 指标

Prometheus 是一个流行的系统监控和警报工具包。它收集并存储多维时间序列数据,如带有键值对标签的指标。

APISIX 提供了向 Prometheus 暴露大量指标的能力 低延迟,允许持续监控和诊断。

本指南将向你展示如何启用 prometheus 插件以与 Prometheus 和 Grafana 服务集成,收集和可视化 APISIX HTTP 指标。


APISIX Prometheus Grafana 流程图

前置条件

  • 安装 Docker
  • 安装 cURL 以向服务发送请求进行验证。
  • 按照 快速入门教程 在 Docker 或 Kubernetes 中启动一个新的 APISIX 实例。

启用 Prometheus 插件

全局启用 prometheus 插件。或者,你可以在路由上启用该插件。

curl -i "http://127.0.0.1:9180/apisix/admin/global_rules" -X PUT -d '{
"id": "rule-for-metrics",
"plugins": {
"prometheus":{}
}
}'

APISIX 收集内部运行时指标,默认通过端口 9091 和路径 /apisix/prometheus/metrics 暴露它们。端口和路径可以在 配置文件 中自定义。

向路由 /apisix/prometheus/metrics 发送请求以从 APISIX 获取指标:

curl "http://127.0.0.1:9091/apisix/prometheus/metrics"

你应该看到类似以下的指标列表:

# HELP apisix_etcd_modify_indexes Etcd modify index for APISIX keys
# TYPE apisix_etcd_modify_indexes gauge
apisix_etcd_modify_indexes{key="consumers"} 0
apisix_etcd_modify_indexes{key="global_rules"} 0
apisix_etcd_modify_indexes{key="max_modify_index"} 16
apisix_etcd_modify_indexes{key="prev_index"} 15
apisix_etcd_modify_indexes{key="protos"} 0
apisix_etcd_modify_indexes{key="routes"} 16
apisix_etcd_modify_indexes{key="services"} 0
apisix_etcd_modify_indexes{key="ssls"} 0
apisix_etcd_modify_indexes{key="stream_routes"} 0
apisix_etcd_modify_indexes{key="upstreams"} 0
apisix_etcd_modify_indexes{key="x_etcd_index"} 16
# HELP apisix_etcd_reachable Config server etcd reachable from APISIX, 0 is unreachable
# TYPE apisix_etcd_reachable gauge
apisix_etcd_reachable 1
...
# HELP apisix_http_status HTTP status codes per service in APISIX
# TYPE apisix_http_status counter
apisix_http_status{code="200",route="ip",matched_uri="/ip",matched_host="",service="",consumer="",node="52.20.124.211"} 1
...

配置 Prometheus

在 Prometheus 中,目标是 Prometheus 抓取指标的端点。你可以将 APISIX 指标端点配置为 Prometheus 中的目标,以从中收集指标。

创建一个配置文件 prometheus.yml

echo 'scrape_configs:
- job_name: "apisix"
scrape_interval: 15s
metrics_path: "/apisix/prometheus/metrics"
static_configs:
- targets: ["apisix-quickstart:9091"]
' > prometheus.yml

在 Docker 中启动一个 Prometheus 实例。暴露的端口映射到主机上的 9092,因为 9090 已保留给 APISIX。本地配置文件 prometheus.yml 挂载到 Prometheus 容器。

docker run -d --name apisix-quickstart-prometheus \
-p 9092:9090 \
--network=apisix-quickstart-net \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus:latest

你现在可以在 Prometheus 目标页面 上检查 APISIX 指标端点状态是否为 UP。Prometheus 将通过抓取此端点从 APISIX 收集指标。

Prometheus

配置 Grafana

Grafana 可以可视化存储在 Prometheus 中的指标。

在 Docker 中启动一个 Grafana 实例,端口为 3000

docker run -d --name=apisix-quickstart-grafana \
-p 3000:3000 \
--network=apisix-quickstart-net \
grafana/grafana-oss

访问 Grafana 控制台 并将上面创建的 Prometheus 实例作为数据源添加到 Grafana。如果你的 Prometheus 实例在 Docker 中运行,请使用 http://127.0.0.1:9092,如果你的 Prometheus 实例部署在 Kubernetes 上,请使用 http://apisix-quickstart-prometheus:9090

Grafana 数据源

官方 APISIX 指标仪表板发布在 Grafana 仪表板 上,ID 为 11719。然后,你可以使用 ID 将仪表板导入 Grafana。

导入仪表板

如果一切正常,仪表板将自动实时可视化指标。

Grafana 仪表板

下一步

你现在已经学会了如何使用 Prometheus 监控 APISIX 指标并在 Grafana 中将其可视化。有关更多配置选项,请参阅 prometheus 插件文档。