0、转载
go-zero docker-compose 搭建课件服务(七):prometheus+grafana服务监控
0.1 源码地址
https://github.com/liuyuede123/go-zero-courseware
1、什么是 prometheus
Prometheus 是一个开源的系统监控和警报工具包。自 2012 年启动以来,许多公司和组织都采用了 Prometheus,该项目拥有非常活跃的开发人员和用户社区。它现在是一个独立的开源项目,独立于任何公司进行维护。Prometheus 于 2016 年加入云原生计算基金会,成为继 Kubernetes 之后的第二个托管项目。
特性:
- 一个多维数据模型,包含由指标名称和键/值对(Tag)标识的时间序列数据 
- PromQL 是一种灵活的查询语音,用于查询并利用这些维度数据 
- 不依赖分布式存储,单个服务器节点是自治的 
- 时间序列收集是通过 HTTP 上的 pull 模型进行的(支持 Pull) 
- 推送时间序列是通过一个中间网关来支持的(也支持 Push) 
- 目标是通过服务发现或静态配置发现的 
- 多种模式的图形和仪表盘支持 
2、什么是 grafana
grafana 是用于可视化大型测量数据的开源程序,他提供了强大和优雅的方式去创建、共享、浏览数据。dashboard 中显示了你不同 metric 数据源中的数据。
Grafana 是一个开源的,拥有丰富 dashboard 和图表编辑的指标分析平台,和 Kibana 不同的是 Grafana 专注于时序类图表分析,而且支持多种数据源,如 Prometheus、Graphite、InfluxDB、Elasticsearch、Mysql、K8s、Zabbix 等。
3、prometheus 部署
根目录下增加 prometheus 的 Dockerfile
 FROM bitnami/prometheus:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
   复制代码
 
增加 prometheus 配置
 # my global configglobal:  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 configurationalerting:  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"    }  }]
   复制代码
 
文件结构如下
 prometheus├── Dockerfile├── prometheus.yml└── target.json
   复制代码
 
docker-compose 中增加 prometheus 配置,默认 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"                     # 设置容器9090端口映射指定宿主机端口,用于宿主机访问可视化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
   复制代码
 
删除容器和镜像重新生成构建容器docker-compose up -d  --build
浏览器中访问 http://localhost:9090/到 prometheus 后台查看是否生效
访问 http://localhost:9090/targets?search=可以看到,4 个服务的 metrics 都进来了
请求用户详情接口,然后访问下 http://localhost:9090/graph,搜索栏中输入{app="user-api"},会看到
4、部署 grafana
新建 grafana 文件夹,并创建 Dockerfile
 FROM grafana/grafana:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
   复制代码
 
docker-compose 中新增 grafana 服务
  ...     grafana:    build:      context: ./grafana    environment:      - TZ=Asia/Shanghai    privileged: true    ports:      - "3000:3000"    networks:      - backend    restart: always
   复制代码
 
删除容器和镜像重新生成构建容器docker-compose up -d  --build
访问 http://localhost:3000/,默认账号 admin,密码 admin
点击设置新增数据源
新增看板
数据源选择 prometheus 统计 user-api qps 然后点击保存
评论