Prometheus 網(wǎng)關(guān)流量監(jiān)控(Kong + APISIX)

前言:部分內(nèi)容在 Kong Quick StartAPISIX Quick Start 中有描述,在本篇中不再重復(fù)。

安裝 Prometheus

可以直接到 Prometheus 官方下載頁面下載,https://prometheus.io/download/ 。Prometheus 是一個用 Go 編寫的二進制可執(zhí)行文件,所以支持 Linux、Windows 和 MacOS 等操作系統(tǒng),下載對應(yīng)版本即可,這里使用 Linux:

wget https://github.com/prometheus/prometheus/releases/download/v2.18.1/prometheus-2.18.1.linux-amd64.tar.gz
tar -xzvf prometheus-2.18.1.linux-amd64.tar.gz
cd prometheus-2.18.1.linux-amd64
./prometheus

這里修改 prometheus.yaml 中的 scrape_configs 配置參數(shù),Prometheus 默認情況下使用的 Pull 方式來拉取數(shù)據(jù),即由它的抓取模塊主動訪問目標(biāo)服務(wù)器的接口來獲取各種性能指標(biāo),當(dāng)然也可以通過 gateway 實現(xiàn) Push 方式,這里不詳細描述,可參考官方文檔。修改 scrape_configs 增加 Kong 的 metrics 接口:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'kong'

    scrape_interval: 1s

    static_configs:
    - targets: ['192.168.33.10:8001']

保存后,啟動 Prometheus:

./prometheus --config.file="prometheus.yml"

配置 Kong

Kong 2.0 開始默認啟用了 prometheus 插件,我們可以直接使用。這里我們將 Prometheus 插件配置為全局插件,如果只想針對部分 service 或者 route 進行性能監(jiān)控,可以單獨配置在這部分上面。開啟全局可以監(jiān)控全局流量,后續(xù)的性能數(shù)據(jù)對 service 做了區(qū)分。

curl http://localhost:8001/plugins -d name=prometheus

我們可以直接訪問 /metrics 測試,將會得到 Prometheus 標(biāo)準(zhǔn)的數(shù)據(jù)格式:

curl http://localhost:8001/metrics

注意,開發(fā)環(huán)境可以直接訪問 Admin 的 /metrics 接口,生產(chǎn)部署環(huán)境盡量不要這樣做。我們可以通過 Kong Proxy 的 IP 地址白名單來做訪問限制(詳細配置可以參考 https://github.com/Kong/kong-plugin-prometheus
):

curl -XPOST http://localhost:8001/services -d name=prometheusEndpoint -d url=http://localhost:8001/metrics
curl -XPOST http://localhost:8001/services/prometheusEndpoint/routes -d paths[]=/metrics
curl -XPOST http://localhost:8001/services/prometheusEndpoint/plugins -d name=ip-restriction -d config.whitelist=10.0.0.0/8

此時我們可以訪問:

curl http://localhost:8000/metrics

注意:是 8000 端口,不是 8001 端口。8000 是 Kong 的代理端口,8001 是 Kong 的管理端口。

安裝 Grafana

官方下載頁面:https://grafana.com/grafana/download

啟動 Grafana:

wget https://dl.grafana.com/oss/release/grafana-7.0.3.linux-amd64.tar.gz)
tar -xzvf grafana-7.0.3.linux-amd64.tar.gz
cd grafana-7.0.3
bin/grafana-server

訪問 localhost:3000 ,默認用戶和密碼是 admin/admin

第一步是創(chuàng)建數(shù)據(jù)源( http://localhost:3000/datasources/new ),選擇 Prometheus 創(chuàng)建即可。開發(fā)環(huán)境下可將 Access 訪問方式設(shè)置為 Browser 直接訪問 Prometheus。

圖片.png

第二步是創(chuàng)建 Dashboard,我們需要使用 Kong 或者 APISIX 官方提供的 Dashboard 模板,因此直接使用 Grafana 的 Import( http://localhost:3000/dashboard/import ):

圖片.png

配置好后,我們需要對接口進行壓測,詳細操作步驟可參考 Kong Quick Start ,這里不再詳述。

圖片.png

注意:調(diào)整右上方的時間范圍為「Last 15 minutes」可以更清楚地觀察結(jié)果。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。