go-zero docker-compose 搭建課件服務(七):prometheus+grafana服務監控

slowquery發表於2022-08-28

0、轉載

go-zero docker-compose 搭建課件服務(七):prometheus+grafana服務監控

0.1原始碼地址

github.com/liuyuede123/go-zero-cou...

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?search=可以看到,4個服務的metrics都進來了

https://cdn.learnku.com/uploads/images/202208/28/25530/X90CyqY6nP.png!large

請求使用者詳情介面,然後訪問下http://localhost:9090/graph,搜尋欄中輸入`{app="user-api"}`,會看到

https://cdn.learnku.com/uploads/images/202208/28/25530/jh1M1UYYJL.png!large

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

點選設定新增資料來源

https://cdn.learnku.com/uploads/images/202208/28/25530/cUdfTggHEE.png!large

https://cdn.learnku.com/uploads/images/202208/28/25530/GqwmWWiweZ.png!large

新增看板

https://cdn.learnku.com/uploads/images/202208/28/25530/c90ZV6ItqC.png!large

https://cdn.learnku.com/uploads/images/202208/28/25530/tOAVF4d9gj.png!large

資料來源選擇prometheus統計user-api qps然後點選儲存

https://cdn.learnku.com/uploads/images/202208/28/25530/xqdOqKDqMc.png!large

https://cdn.learnku.com/uploads/images/202208/28/25530/cWtcsxInOL.png!large

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章