关于prometheus:gozero-dockercompose-搭建课件服务七prometheusgrafana服务监控

3次阅读

共计 3627 个字符,预计需要花费 10 分钟才能阅读完成。

0、索引

go-zero docker-compose 搭建课件服务(一):编写服务 api 和 proto

go-zero docker-compose 搭建课件服务(二):编写 courseware rpc 服务

go-zero docker-compose 搭建课件服务(三):编写 courseware api 服务

go-zero docker-compose 搭建课件服务(四):生成 Dockerfile 并在 docker-compose 中启动

go-zero docker-compose 搭建课件服务(五):欠缺 user 服务

go-zero docker-compose 搭建课件服务(六):欠缺 jwt 鉴权和返回构造

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 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"
    }
  }
]

文件构造如下

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… 能够看到,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 而后点击保留

正文完
 0