實戰:完整監控系統
Vibe Prompt
「幫我寫一個完整的 docker-compose.yml,包含 Prometheus、Grafana、Loki、Promtail、Jaeger、Node Exporter。」
完整 Compose
version: "3.8"
services:
prometheus:
image: prom/prometheus:latest
ports: ["9090:9090"]
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
grafana:
image: grafana/grafana:latest
ports: ["3000:3000"]
environment:
GF_SECURITY_ADMIN_PASSWORD: admin
GF_INSTALL_PLUGINS: grafana-lokiexplore-app
volumes:
- grafana_data:/var/lib/grafana
loki:
image: grafana/loki:latest
ports: ["3100:3100"]
command: -config.file=/etc/loki/local-config.yaml
promtail:
image: grafana/promtail:latest
volumes:
- /var/log:/var/log
- ./promtail-config.yaml:/etc/promtail/config.yaml
jaeger:
image: jaegertracing/all-in-one:latest
ports: ["16686:16686", "4317:4317"]
environment:
COLLECTOR_OTLP_ENABLED: "true"
node_exporter:
image: prom/node-exporter:latest
ports: ["9100:9100"]
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
volumes:
prometheus_data:
grafana_data:
三大支柱
Metrics (Prometheus) → 系統數值指標
Logs (Loki) → 事件記錄
Traces (Jaeger) → 請求流程
↓
Grafana 統一視圖
課程總結
監控課程完成!
- ✅ Prometheus 指標收集
- ✅ Grafana 儀表板
- ✅ Loki 日誌聚合
- ✅ OpenTelemetry + Jaeger
- ✅ 完整可觀測性平台
完整監控架構總覽
使用者請求
│
▼
┌─────────────┐ ┌──────────────┐ ┌──────────────┐
│ Nginx Ingress│───▶│ API Server │───▶│ PostgreSQL │
│ (Prometheus) │ │ (OTel SDK) │ │ (exporter) │
└─────────────┘ └──────────────┘ └──────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────┐
│ Prometheus (指標收集) │
└─────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌──────────────┐ ┌──────────────┐
│ Grafana │ │ Loki (日誌) │ │ Jaeger │
│ (儀表板) │ │ │ │ (追蹤) │
└─────────────┘ └──────────────┘ └──────────────┘
│
▼
┌─────────────┐
│ Alertmanager│
│ Slack/Email │
└─────────────┘
定價估算(自管 vs SaaS)
| 方案 | 月費 | 優點 | 缺點 | |------|:----:|------|------| | 自管 Prometheus+Grafana | $0(只花 EC2 費用) | 完全控制,無限資料 | 需要維護 | | Grafana Cloud (Free) | $0 | 免維護,14 天保留 | 有限 Metric/Logs | | Grafana Cloud (Pro) | ~$50/月起 | 免維護,可擴展 | 費用隨資料量成長 | | Datadog | ~$15/主機/月 | 整合最完整 | 高流量時費用快速上升 | | AWS Managed Prometheus | $0.09/百萬樣本 | AWS 整合最佳 | 只能 AWS 使用 |
下一步
- 建立 SLO Dashboard:結合 SLI 與錯誤預算
- 設計 On-Call Rotations:與 PagerDuty/Opsgenie 整合
- 實作 Auto Remediation:用 Webhook 觸發自動修復腳本
常見錯誤
程式碼範例
建立完整的可觀測性平台
把 Prometheus、Grafana、Loki、OpenTelemetry 全部整合——Metrics(發生什麼)、Logs(為什麼發生)、Traces(在哪裡發生)。
三大支柱
| 支柱 | 工具 | 回答的問題 | |:----|:----|:---------| | Metrics | Prometheus | CPU 飆高?請求數暴增? | | Logs | Loki | 錯誤訊息的詳細內容? | | Traces | OpenTelemetry | 哪個服務最慢? |
課程總結
這堂監控課你從 Prometheus、Grafana、Loki、OpenTelemetry 到完整系統——現在你可以為任何系統建立完整的監控方案。