現(xiàn)在的項(xiàng)目有個(gè)需求,需要分析每一條 api 的請(qǐng)求時(shí)間,目前看到一套比較好的方案是 kong + Prometheus + Grafana,之前是聽(tīng)說(shuō)過(guò) kong 擴(kuò)展性比較好,但一直沒(méi)有實(shí)踐的機(jī)會(huì),借此機(jī)會(huì)來(lái)玩一玩核武器。
我熟悉的方式還是 docker,還是用它從頭搭建這一套系統(tǒng)。
立個(gè) flag,今年必須學(xué)會(huì) kubernetes。
參考了多篇文章各取所長(zhǎng),理順了各個(gè)軟件之間關(guān)系之后,發(fā)現(xiàn)網(wǎng)上的這些方法都過(guò)于復(fù)雜,很多文章也是相互拼湊,容器化不像容器化,其實(shí) kong、Prometheus、Grafana 之間的兼容性已經(jīng)足夠好了,我在操作過(guò)程中確實(shí)也踩了一些坑,但是我已經(jīng)可以將他們徹底容器化部署了。
運(yùn)行 kong
創(chuàng)建子網(wǎng)
docker network create kong-net
運(yùn)行數(shù)據(jù)庫(kù)
創(chuàng)建數(shù)據(jù)庫(kù)
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6
數(shù)據(jù)庫(kù)遷移
docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:1.5 kong migrations bootstrap
運(yùn)行 kong
docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:1.5
運(yùn)行 kong gui
~運(yùn)行 kong-dashboard~
建議直接跳過(guò),dashboard 可以運(yùn)行,但是前端盡是報(bào)錯(cuò),github上面的issue也沒(méi)人處理,應(yīng)該是不維護(hù)了。
docker run --network=kong-net \
--name kong-dashboard \
-p 8080:8080 \
-d pgbi/kong-dashboard \
start --kong-url http://kong:8001 \
--basic-auth user1=password1
運(yùn)行 konga
docker run -p 8080:1337 \
--network kong-net \
--name konga \
-e "NODE_ENV=production" \
-d pantsel/konga:0.14.7
運(yùn)行 prometheus
docker run --name prometheus \
--network=kong-net \
-p 9090:9090 \
-v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-d prom/prometheus:v2.16.0
prometheus.yml 文件配置如下
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['kong:8001']
核心點(diǎn)就是上面的 targets,相信很多分析類(lèi)的文章都沒(méi)有提到。
運(yùn)行 grafana
docker run -d --network=kong-net --name=grafana -p 3000:3000 grafana/grafana:6.6.2
運(yùn)行之后,直接添加一個(gè) dashboard 即可。
小節(jié)一下
api 經(jīng)過(guò) kong,會(huì)留下日志,prometheus 通過(guò) kong:8001 端點(diǎn)標(biāo)準(zhǔn)化采集的日志,
grafana 通過(guò) prometheus 得到標(biāo)準(zhǔn)后的日志進(jìn)行展示。