Kong 分析与监控插件之Prometheus详解

概述

以Prometheus公开格式暴露与Kong和经过代理的上游服务相关的指标,这些指标可以由Prometheus服务器获取。

该示例配置内容如下:

  • kong的代理信息

IP地址:192.168.0.90(根据自己的部署情况调整为自己的ip)

代理服务端口:80、443(根据自己的部署情况调整为自己的端口)

  • 后端服务

地址:http://192.168.1.196/prometheus(根据实际情况,自己定义)

浏览访问效果:

  • Kong管理API工具

konga作为示例配置kong的管理工具。关于konga 的安装请参考,Linux 下源码安装Kong管理工具konga。

访问地址:http://192.168.0.90:1337

创建服务及路由

  • 创建服务

通过konga的管理菜单SERVICES,添加一个服务。


维护对应的name及url即可。其中name为:prometheus_example、url的地址为http://192.168.1.196/。

  • 创建路由

通过konga的管理菜单SERVICES,可以看到上面添加的“prometheus_example”服务。然后点击此服务,进入服务的详情页面。


服务详情

在服务的详情页面点击左侧导航的“Routes”,进行服务下路由的管理操作,按如下操作创建一个新路由。


把属性Paths设置为/prometheus然后回车(记得一定要回车,不然值添加不上),Path handling设置v0,Strip Path设置:NO,这些属性的详解,请参考Kong 管理API详解之八——路由对象 。

已上操作完成之后,即完成了一个通过路由代理转发后端服务的配置。可以通过上面提供的kong代理服务的信息访问代理后的效果地址为:http(s)://192.168.0.90/prometheus。


代理之后的效果

可以看到跟直接访问后端服务页面效果一样。

在服务或路由上添加插件Prometheus

根据实际场景选择是在服务或路由上添加插件,只是插件作用域的范围不一样,如果在服务上添加插件,则对服务下的所有路由都生效。如果在路由上添加插件,则只作用该路由本身。该示例演示通过在路由上添加插件。

  • Prometheus插件参数介绍
  1. name:插件的名称,值为prometheus。数据类型:string。
  2. service.id:插件目标服务的ID(通过管理api配置时指定)。数据类型:string。
  3. route.id:插件目标路由的ID(通过管理api配置时指定)。数据类型:string。
  4. enabled:是否应用此插件。数据类型:boolean,默认值:true。

在http://localhost:/metrics端点上,管理API和状态API上都有对应的指标变量。注意,这些api的URL将特定于您的安装;参见访问下面的指标。

这个插件记录并公开节点级别的指标。您的Prometheus服务器需要通过服务发现机制发现所有Kong节点,并使用每个节点配置/metrics端点的数据

  • Grafana仪表盘

通过插件导出的指标可以在Grafana中使用一个drop in仪表盘生成图表:https://grafana.com/dashboards/7424。

  • 可用的指标
  • Status codes:上游服务返回的HTTP状态码。每个服务和所有服务都可以使用它们。
  • Latencies Histograms:在Kong测量的延迟:

Request:Kong和上游服务处理请求所花费的总时间。

Kong:Kong路由请求并运行所有配置的插件所花费的时间。

Upstream:上游服务响应请求所花费的时间。

  • Bandwidth:流经Kong的总带宽(出口/入口)。此指标对每个服务可用,并作为所有服务的总和。
  • DB reachability:一种计量类型,其值为0或1,表示孔节点是否可以到达DB。
  • Connections:各种Nginx连接指标,如活动、读、写和接受的连接数。
  • Target Health:属于给定上游的目标的健康状态(healthchecks_off,healthy,unhealthy或dns_error)。

下面是您可能期望从/metrics端点得到的输出示例:

curl -i http://localhost:8001/metrics
HTTP/1.1 200 OK
Server: openresty/1.15.8.3
Date: Tue, 7 Jun 2020 16:35:40 GMT
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *

# HELP kong_bandwidth_total Total bandwidth in bytes for all proxied requests in Kong
# TYPE kong_bandwidth_total counter
kong_bandwidth_total{type="egress"} 1277
kong_bandwidth_total{type="ingress"} 254
# HELP kong_bandwidth Total bandwidth in bytes consumed per service in Kong
# TYPE kong_bandwidth counter
kong_bandwidth{type="egress",service="google"} 1277
kong_bandwidth{type="ingress",service="google"} 254
# HELP kong_datastore_reachable Datastore reachable from Kong, 0 is unreachable
# TYPE kong_datastore_reachable gauge
kong_datastore_reachable 1
# HELP kong_http_status_total HTTP status codes aggreggated across all services in Kong
# TYPE kong_http_status_total counter
kong_http_status_total{code="301"} 2
# HELP kong_http_status HTTP status codes per service in Kong
# TYPE kong_http_status counter
kong_http_status{code="301",service="google"} 2
# HELP kong_latency Latency added by Kong, total request time and upstream latency for each service in Kong
# TYPE kong_latency histogram
kong_latency_bucket{type="kong",service="google",le="00001.0"} 1
kong_latency_bucket{type="kong",service="google",le="00002.0"} 1
.
.
.
kong_latency_bucket{type="kong",service="google",le="+Inf"} 2
kong_latency_bucket{type="request",service="google",le="00300.0"} 1
kong_latency_bucket{type="request",service="google",le="00400.0"} 1
.
.
kong_latency_bucket{type="request",service="google",le="+Inf"} 2
kong_latency_bucket{type="upstream",service="google",le="00300.0"} 2
kong_latency_bucket{type="upstream",service="google",le="00400.0"} 2
.
.
kong_latency_bucket{type="upstream",service="google",le="+Inf"} 2
kong_latency_count{type="kong",service="google"} 2
kong_latency_count{type="request",service="google"} 2
kong_latency_count{type="upstream",service="google"} 2
kong_latency_sum{type="kong",service="google"} 2145
kong_latency_sum{type="request",service="google"} 2672
kong_latency_sum{type="upstream",service="google"} 527
# HELP kong_latency_total Latency added by Kong, total request time and upstream latency aggreggated across all services in Kong
# TYPE kong_latency_total histogram
kong_latency_total_bucket{type="kong",le="00001.0"} 1
kong_latency_total_bucket{type="kong",le="00002.0"} 1
.
.
kong_latency_total_bucket{type="kong",le="+Inf"} 2
kong_latency_total_bucket{type="request",le="00300.0"} 1
kong_latency_total_bucket{type="request",le="00400.0"} 1
.
.
kong_latency_total_bucket{type="request",le="+Inf"} 2
kong_latency_total_bucket{type="upstream",le="00300.0"} 2
kong_latency_total_bucket{type="upstream",le="00400.0"} 2
.
.
.
kong_latency_total_bucket{type="upstream",le="+Inf"} 2
kong_latency_total_count{type="kong"} 2
kong_latency_total_count{type="request"} 2
kong_latency_total_count{type="upstream"} 2
kong_latency_total_sum{type="kong"} 2145
kong_latency_total_sum{type="request"} 2672
kong_latency_total_sum{type="upstream"} 527
# HELP kong_nginx_http_current_connections Number of HTTP connections
# TYPE kong_nginx_http_current_connections gauge
kong_nginx_http_current_connections{state="accepted"} 8
kong_nginx_http_current_connections{state="active"} 1
kong_nginx_http_current_connections{state="handled"} 8
kong_nginx_http_current_connections{state="reading"} 0
kong_nginx_http_current_connections{state="total"} 8
kong_nginx_http_current_connections{state="waiting"} 0
kong_nginx_http_current_connections{state="writing"} 1
# HELP kong_nginx_metric_errors_total Number of nginx-lua-prometheus errors
# TYPE kong_nginx_metric_errors_total counter
kong_nginx_metric_errors_total 0
# HELP kong_upstream_target_health Health status of targets of upstream. States = healthchecks_off|healthy|unhealthy|dns_error, value is 1 when state is populated.
kong_upstream_target_health{upstream="",target="",address=":",state="healthchecks_off"} 0
kong_upstream_target_health{upstream="",target="",address=":",state="healthy"} 1
kong_upstream_target_health{upstream="",target="",address=":",state="unhealthy"} 0
kong_upstream_target_health{upstream="",target="",address=":",state="dns_error"} 0
  • 访问指标

在大多数配置中,Kong管理API将在防火墙之后或者需要进行认证。这里有几个选项可以访问Prometheus的/metrics端点:

  1. 如果状态API是启用的,那么可以使用它的/metrics端点。这是首选的方法。
  2. 在管理API上也可以使用/metrics端点,如果没有启用状态API,可以使用管理API。注意,当在管理API上启用RBAC时,此端点不可用(Prometheus不支持通过Key-Auth传递令牌)。
  • 在路由上添加Prometheus插件

通konga菜单ROUTES可以获取所有路由的列表,找到刚才添加的路由。然后点击进去可以看到该路由的详情页面。


通过该路由左侧的导航菜单中的“Plugins”管理该路由的插件。选择插件导航中的“Analytics & Monitoring”,然后选择其列表中的Prometheus。


维护Prometheus插件信息。


添加插件


通过浏览器访问http://192.168.0.90:8001/metrics


指标列表

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章