iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0

今天來體驗 Prometheus ,是一個監控系統加排序資料庫(Time-series DB),主要用於監控主機跟服務的性能。

安裝

  • 安裝一樣透過docker來實現,搭配docker-compose,主打一個高效方便
# 預期的資料結構
C:\USERS\ASUS\STEVEN\PROMETHEUS
│  docker-compose.yml
│
└─prometheus
        prometheus.yml
  • docker-compose.yml
version: "3.9"

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"          # Prometheus UI
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
    command: ["--config.file=/etc/prometheus/prometheus.yml"]

  example-app:
    image: quay.io/brancz/prometheus-example-app:v0.3.0
    container_name: prom-example
    ports:
      - "8080:8080"          # 範例應用的 /metrics
    environment:
      - HTTP_LISTEN_ADDRESS=:8080
      # 這個 app 會自動在 /metrics 暴露 counter/gauge/histogram

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"          # Grafana UI (帳密 admin/admin)
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana-data:/var/lib/grafana

volumes:
  grafana-data:
  • prometheus/prometheus.yml
global:
  scrape_interval: 5s
  evaluation_interval: 15s

scrape_configs:
  - job_name: "example-app"
    static_configs:
      - targets: ["example-app:8080"]   # 由 compose DNS 解析到容器

  • 啟動 prometheus
docker-compose up -d

Grafana

  • grafana是prometheus的一個UI,幫助我們看到prometheus收集到的數據最終被視覺化的地方。
  • 前面的 docker-compose 順利執行後,grafana-ui 直接按這個就能打開網站了

監控數據

進到了 grafana 裡面後,我們就來監控一下剛剛docker-compose中有順帶起的一個prometheus的範例容器,好幫助我們來熟悉如何去監控我們的應用

  1. 點擊Grafana 左側的Connections,再選擇Add new connection

https://ithelp.ithome.com.tw/upload/images/20250929/201119968vjC2lFTay.png

  1. 搜尋 prometheus,點下去,再按下Add new data source

https://ithelp.ithome.com.tw/upload/images/20250929/20111996nCEEHh00yD.png
https://ithelp.ithome.com.tw/upload/images/20250929/20111996bzmB035gVB.png

  1. 再Connection中填入 http://prometheus:9090 ,再按 Save & Test

https://ithelp.ithome.com.tw/upload/images/20250929/20111996VdlIYLFDaX.png

  1. 點擊Grafana 左側的Dashboards,再選擇Create dashboard

https://ithelp.ithome.com.tw/upload/images/20250929/20111996duA9tpqtpS.png

  1. 按下Add visualization,data source選擇prometheus,第3步存的

https://ithelp.ithome.com.tw/upload/images/20250929/20111996tvnKf6E5VJ.png
https://ithelp.ithome.com.tw/upload/images/20250929/201119968b8EVCupaz.png

  1. 在下面Queries的部分,按Code的地方,輸入http_requests_total,按Save dashboard,再按Back to dashboard
  2. 執行 http://localhost:8080/ 幾次,讓prometheus去讀取metrics,之後就能在dashboard上看到相關的數據了

https://ithelp.ithome.com.tw/upload/images/20250929/20111996XKje2zd0uM.png
https://ithelp.ithome.com.tw/upload/images/20250929/20111996FC7halNcXt.png

明日接續

今天先簡單體驗一下prometheus到一個範例監控的小場景,明天來搭配之前的C++ grpc server做相關的監控。


上一篇
sysbench 體驗
下一篇
Prometheus 體驗 2
系列文
天花亂墜的走馬看花之軟體體驗之旅17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言