0、轉(zhuǎn)載
go-zero docker-compose 搭建課件服務(wù)(七):prometheus+grafana服務(wù)監(jiān)控
0.1源碼地址
https://github.com/liuyuede123/go-zero-courseware
1古拴、什么是prometheus
Prometheus是一個(gè)開源的系統(tǒng)監(jiān)控和警報(bào)工具包。自2012年啟動(dòng)以來枯冈,許多公司和組織都采用了Prometheus伦连,該項(xiàng)目擁有非常活躍的開發(fā)人員和用戶社區(qū)磕洪。它現(xiàn)在是一個(gè)獨(dú)立的開源項(xiàng)目搅吁,獨(dú)立于任何公司進(jìn)行維護(hù)威创。Prometheus于2016年加入云原生計(jì)算基金會(huì)落午,成為繼Kubernetes之后的第二個(gè)托管項(xiàng)目。
特性:
- 一個(gè)多維數(shù)據(jù)模型那婉,包含由指標(biāo)名稱和鍵/值對(duì)(Tag)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù)
- PromQL是一種靈活的查詢語音板甘,用于查詢并利用這些維度數(shù)據(jù)
- 不依賴分布式存儲(chǔ),單個(gè)服務(wù)器節(jié)點(diǎn)是自治的
- 時(shí)間序列收集是通過HTTP上的pull模型進(jìn)行的(支持Pull)
- 推送時(shí)間序列是通過一個(gè)中間網(wǎng)關(guān)來支持的(也支持Push)
- 目標(biāo)是通過服務(wù)發(fā)現(xiàn)或靜態(tài)配置發(fā)現(xiàn)的
- 多種模式的圖形和儀表盤支持
2详炬、什么是grafana
grafana是用于可視化大型測(cè)量數(shù)據(jù)的開源程序盐类,他提供了強(qiáng)大和優(yōu)雅的方式去創(chuàng)建、共享呛谜、瀏覽數(shù)據(jù)在跳。dashboard中顯示了你不同metric數(shù)據(jù)源中的數(shù)據(jù)。
Grafana是一個(gè)開源的隐岛,擁有豐富dashboard和圖表編輯的指標(biāo)分析平臺(tái)猫妙,和Kibana不同的是Grafana專注于時(shí)序類圖表分析,而且支持多種數(shù)據(jù)源聚凹,如Prometheus割坠、Graphite、InfluxDB妒牙、Elasticsearch彼哼、Mysql、K8s湘今、Zabbix等敢朱。
3、prometheus部署
根目錄下增加prometheus的Dockerfile
FROM bitnami/prometheus:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
增加prometheus配置
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 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: 'file_ds'
file_sd_configs:
- files:
- targets.json
[
{
"targets": ["user-api:9081"],
"labels": {
"job": "user-api",
"app": "user-api",
"env": "test",
"instance": "user-api:8300"
}
},
{
"targets": ["user-rpc:9091"],
"labels": {
"job": "user-rpc",
"app": "user-rpc",
"env": "test",
"instance": "user-api:9300"
}
},
{
"targets": ["courseware-api:9082"],
"labels": {
"job": "courseware-api",
"app": "courseware-api",
"env": "test",
"instance": "courseware-api:8400"
}
},
{
"targets": ["courseware-rpc:9092"],
"labels": {
"job": "courseware-rpc",
"app": "courseware-rpc",
"env": "test",
"instance": "courseware-rpc:9400"
}
}
]
文件結(jié)構(gòu)如下
prometheus
├── Dockerfile
├── prometheus.yml
└── target.json
docker-compose中增加prometheus配置摩瞎,默認(rèn)9090端口
...
prometheus:
build:
context: ./prometheus
environment:
- TZ=Asia/Shanghai
privileged: true
volumes:
- ./prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml # 將 prometheus 配置文件掛載到容器里
- ./prometheus/target.json:/opt/bitnami/prometheus/conf/targets.json # 將 prometheus 配置文件掛載到容器里
ports:
- "9090:9090" # 設(shè)置容器9090端口映射指定宿主機(jī)端口拴签,用于宿主機(jī)訪問可視化web
networks:
- backend
restart: always
user-api配置中增加
...
Prometheus:
Host: 0.0.0.0
Port: 9081
Path: /metrics
user-rpc配置中增加
...
Prometheus:
Host: 0.0.0.0
Port: 9091
Path: /metrics
courseware-api配置中增加
...
Prometheus:
Host: 0.0.0.0
Port: 9082
Path: /metrics
courseware-rpc配置中增加
Prometheus:
Host: 0.0.0.0
Port: 9092
Path: /metrics
刪除容器和鏡像重新生成構(gòu)建容器docker-compose up -d --build
瀏覽器中訪問http://localhost:9090/到prometheus后臺(tái)查看是否生效
訪問http://localhost:9090/targets?search=可以看到,4個(gè)服務(wù)的metrics都進(jìn)來了
[圖片上傳失敗...(image-6c3070-1666052299927)]
請(qǐng)求用戶詳情接口旗们,然后訪問下http://localhost:9090/graph蚓哩,搜索欄中輸入{app="user-api"}
,會(huì)看到
[圖片上傳失敗...(image-683994-1666052299927)]
4上渴、部署grafana
新建grafana文件夾岸梨,并創(chuàng)建Dockerfile
FROM grafana/grafana:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
docker-compose中新增grafana服務(wù)
...
grafana:
build:
context: ./grafana
environment:
- TZ=Asia/Shanghai
privileged: true
ports:
- "3000:3000"
networks:
- backend
restart: always
刪除容器和鏡像重新生成構(gòu)建容器docker-compose up -d --build
訪問http://localhost:3000/,默認(rèn)賬號(hào)admin驰贷,密碼admin
點(diǎn)擊設(shè)置新增數(shù)據(jù)源
[圖片上傳失敗...(image-ba738-1666052299927)]
[圖片上傳失敗...(image-7770b4-1666052299927)]
新增看板
[圖片上傳失敗...(image-c241c3-1666052299927)]
[圖片上傳失敗...(image-e0b032-1666052299927)]
數(shù)據(jù)源選擇prometheus統(tǒng)計(jì)user-api qps然后點(diǎn)擊保存
[圖片上傳失敗...(image-a845ed-1666052299927)]
[圖片上傳失敗...(image-d4026f-1666052299927)]