使用 Prometheus 监控 APISIX 指标
Prometheus 是一个流行的系统监控和警报工具包。它收集并存储多维时间序列数据,如带有键值对标签的指标。
APISIX 提供了向 Prometheus 暴露大量指标的能力 低延迟,允许持续监控和诊断。
本指南将向你展示如何启用 prometheus 插件以与 Prometheus 和 Grafana 服务集成,收集和可视化 APISIX HTTP 指标。

前置条件
启用 Prometheus 插件
全局启用 prometheus 插件。或者,你可以在路由上启用该插件。
- Admin API
- ADC
- Ingress Controller
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 暴露它们。端口和路径可以在 配置文件 中自定义。
global_rules:
prometheus: {}
将配置同步到 APISIX:
adc sync -f adc.yaml
APISIX 收集内部运行时指标,默认通过端口 9091 和路径 /apisix/prometheus/metrics 暴露它们。端口和路径可以在 配置文件 中自定义。
- Gateway API
- APISIX CRD
更新你的 GatewayProxy 清单以将 prometheus 启用为全局插件:
要在添加其他配置之前查看当前 GatewayProxy 配置,请运行:
kubectl get gatewayproxy apisix-config -o yaml
apiVersion: apisix.apache.org/v1alpha1
kind: GatewayProxy
metadata:
namespace: ingress-apisix
name: apisix-config
spec:
provider:
type: ControlPlane
controlPlane:
auth:
adminKey:
value: edd1c9f034335f136f87ad84b625c8f1
type: AdminKey
service:
name: apisix-admin
port: 9180
plugins:
- name: prometheus
enabled: true
将配置应用到你的集群:
kubectl apply -f gateway-proxy.yaml
创建一个 Kubernetes 清单文件以将 prometheus 启用为全局插件:
apiVersion: apisix.apache.org/v2
kind: ApisixGlobalRule
metadata:
namespace: ingress-apisix
name: global-prometheus
spec:
ingressClassName: apisix
plugins:
- name: prometheus
enable: true
将配置应用到你的集群:
kubectl apply -f global-prometheus.yaml
要在指定的路径和端口上暴露 Prometheus 指标,请更新你的 Helm 安装:
helm upgrade apisix apisix/apisix \
--namespace ingress-apisix \
--set apisix.deployment.role=traditional \
--set apisix.deployment.role_traditional.config_provider=yaml \
--set etcd.enabled=false \
--set ingress-controller.enabled=true \
--set ingress-controller.config.provider.type=apisix-standalone \
--set ingress-controller.apisix.adminService.namespace=ingress-apisix \
--set ingress-controller.gatewayProxy.createDefault=true \
--set apisix.prometheus.enabled=true \
--set apisix.prometheus.path="/apisix/prometheus/metrics" \
--set apisix.prometheus.containerPort=9091
将 Prometheus 指标服务端口转发到你的本地机器端口:
kubectl port-forward service/apisix-prometheus-metrics 9091:9091 &
向路由 /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 中的目标,以从中收集指标。
- Docker
- Kubernetes
创建一个配置文件 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
创建一个 ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: ingress-apisix
name: prometheus-config
data:
prometheus.yml: |
scrape_configs:
- job_name: "apisix"
scrape_interval: 15s
metrics_path: "/apisix/prometheus/metrics"
static_configs:
- targets: ["apisix-prometheus-metrics:9091"]
为 Prometheus 创建一个部署文件:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ingress-apisix
name: apisix-quickstart-prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
namespace: ingress-apisix
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus:latest
ports:
- containerPort: 9090
volumeMounts:
- name: config-volume
mountPath: /etc/prometheus/
volumes:
- name: config-volume
configMap:
name: prometheus-config
创建一个服务文件以暴露 Prometheus:
apiVersion: v1
kind: Service
metadata:
namespace: ingress-apisix
name: apisix-quickstart-prometheus
spec:
type: ClusterIP
selector:
app: prometheus
ports:
- protocol: TCP
port: 9090
targetPort: 9090
将配置应用到你的集群:
kubectl apply -f cm-prometheus.yaml -f deployment-prometheus.yaml -f service-prometheus.yaml
将 Prometheus 目标页面端口转发到你的本地机器端口:
kubectl port-forward svc/apisix-quickstart-prometheus 9092:9090 &
你现在可以在 Prometheus 目标页面 上检查 APISIX 指标端点状态是否为 UP。Prometheus 将通过抓取此端点从 APISIX 收集指标。

配置 Grafana
Grafana 可以可视化存储在 Prometheus 中的指标。
- Docker
- Kubernetes
在 Docker 中启动一个 Grafana 实例,端口为 3000:
docker run -d --name=apisix-quickstart-grafana \
-p 3000:3000 \
--network=apisix-quickstart-net \
grafana/grafana-oss
为 Grafana 创建一个部署文件:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ingress-apisix
name: apisix-quickstart-grafana
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
namespace: ingress-apisix
labels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana-oss:latest
ports:
- containerPort: 3000
apiVersion: v1
kind: Service
metadata:
namespace: ingress-apisix
name: apisix-quickstart-grafana
spec:
type: ClusterIP
selector:
app: grafana
ports:
- protocol: TCP
port: 3000
targetPort: 3000
将配置应用到你的集群:
kubectl apply -f deployment-grafana.yaml -f service-grafana.yaml
将 Grafana 控制台端口转发到你的本地机器端口:
kubectl port-forward svc/apisix-quickstart-grafana 3000:3000 &
访问 Grafana 控制台 并将上面创建的 Prometheus 实例作为数据源添加到 Grafana。如果你的 Prometheus 实例在 Docker 中运行,请使用 http://127.0.0.1:9092,如果你的 Prometheus 实例部署在 Kubernetes 上,请使用 http://apisix-quickstart-prometheus:9090。

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

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

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