Appearance
Prometheus on Kubernetes 部署與設定
使用 prometheus-community Helm Chart 在 Kubernetes 上部署 Prometheus,含 extraScrapeConfigs 設定、Admin API 啟用與時序資料管理。
概述
Prometheus 是監控系統的核心組件,負責週期性抓取(scrape)各個目標的 /metrics 端點並存儲時序資料。在 Kubernetes 環境中,Prometheus 透過 prometheus-community Helm Chart 部署,以 values.yaml 宣告式設定持久化儲存、資料保存期限、Scrape Job 清單等關鍵參數。
Prometheus 預設資料保存 15 天,在需要長期趨勢分析的場景下可調整為更長的保存期(如 1y)。Admin API(web.enable-admin-api)預設關閉,啟用後可透過 HTTP API 刪除特定的時序資料,對資料管理和清理非常有用。
核心內容
Scrape Job 設定模式
extraScrapeConfigs 是 Helm Values 中新增 Scrape Job 的主要方式,內容為 YAML 字串(需在 key 後加 |)。常見的兩種模式:
標準 HTTP 抓取:
yaml
- job_name: 'my-exporter'
static_configs:
- targets: ['service-name.namespace.svc.cluster.local:port']HTTPS 抓取(跳過憑證驗證):
yaml
- job_name: 'my-https-exporter'
scheme: https
tls_config:
insecure_skip_verify: true
static_configs:
- targets: ['10.x.x.x:port']處理慢速 Exporter(Scrape Timeout):
yaml
- job_name: 'slow-exporter'
scrape_interval: 30s
scrape_timeout: 25s # 必須小於 scrape_interval
static_configs:
- targets: ['exporter-service:80']已配置的 Exporter 一覽
| Job Name | Target(s) | Port | 說明 |
|---|---|---|---|
| postgres-exporter | postgres-exporter-prometheus-postgres-exporter / postgres-exporter2-... | 80 | PostgreSQL |
| redis-exporter | redis-exporter-prometheus-redis-exporter | 9121 | Redis |
| rabbitmq-exporter | rabbitmq-exporter-prometheus-rabbitmq-exporter | 9419 | RabbitMQ (HTTP) |
| rabbitmq-https-exporter | 10.30.196.58 | 32074 | RabbitMQ (HTTPS) |
| statsd-exporter | statsd-exporter-prometheus-statsd-exporter / 多個 NodePort | 9102 / 31702 | StatsD |
Admin API 操作
啟用 web.enable-admin-api 後,可透過以下 API 刪除時序資料(用於清理測試資料或下線服務的殘留 metrics):
bash
# 刪除特定 instance 的所有資料
curl -X POST 'http://prometheus:30990/api/v1/admin/tsdb/delete_series?match[]={instance="target-service:9102"}'
# 刪除特定 metric + label 組合
curl -X POST 'http://prometheus:30990/api/v1/admin/tsdb/delete_series?match[]=gunicorn_request_duration_count{app="localhost"}'非 K8s 環境整合
對於不在 K8s 叢集內的 Exporter,需編輯 prometheus.yml 新增靜態 Scrape Target:
yaml
- job_name: 'postgres_exporter'
static_configs:
- targets: ['xxx.xxx.xxx.xxx:9187']關鍵要點
extraScrapeConfigs後必須加|才能正確解析 YAML 字串- 資料保存期限預設 15 天,長期分析場景建議調整
retention參數 web.enable-admin-api與web.enable-lifecycle需明確啟用- HTTPS metrics 抓取需在 job 層級設定
scheme: https與tls_config scrape_timeout必須小於scrape_interval,否則 Prometheus 會報錯
實際應用
Prometheus 作為整個監控體系的資料收集中心,與各個 Exporter 組成完整的監控管線:各服務的 Exporter 暴露 /metrics 端點,Prometheus 定期抓取並存儲,Grafana 查詢 Prometheus 進行視覺化與告警。當需要監控新服務時,只需在 values.yaml 的 extraScrapeConfigs 中新增對應的 Scrape Job,並 helm upgrade 即可。
部署設定參考
以下為實際部署時使用的完整設定,供日後查詢與複製使用。
環境參數
| 項目 | 值 |
|---|---|
| Namespace | monitoring |
| StorageClass | nfs-client |
| PVC 大小 | 20Gi |
| 資料保存期限 | 1y |
| Prometheus Web UI | http://10.248.36.248:30990 |
安裝指令
bash
helm install prometheus -f values.yaml \
--namespace monitoring \
--insecure-skip-tls-verify \
prometheus-community/prometheus完整 values.yaml
yaml
server:
persistentVolume:
accessModes:
- ReadWriteOnce
size: 20Gi
storageClass: "nfs-client"
retention: "1y"
replicaCount: 1
extraFlags:
## web.enable-admin-api: 啟用管理 HTTP API(包含刪除時序資料功能),預設為關閉
- web.enable-admin-api
- web.enable-lifecycle
# 額外的 scrape configs(必須是字串,extraScrapeConfigs 後面需加 |)
extraScrapeConfigs: |
- job_name: 'postgres-exporter'
static_configs:
- targets:
- postgres-exporter-prometheus-postgres-exporter:80
- postgres-exporter2-prometheus-postgres-exporter:80
- job_name: 'redis-exporter'
static_configs:
- targets:
- redis-exporter-prometheus-redis-exporter:9121
- job_name: 'rabbitmq-exporter'
static_configs:
- targets:
- rabbitmq-exporter-prometheus-rabbitmq-exporter:9419
- job_name: 'rabbitmq-https-exporter'
scheme: https
tls_config:
insecure_skip_verify: true
static_configs:
- targets:
- 10.30.196.58:32074
- job_name: 'statsd-exporter'
static_configs:
- targets:
- statsd-exporter-prometheus-statsd-exporter:9102
- 10.30.202.6:31702
- 10.30.224.92:31702
alertmanager:
enabled: falseAdmin API 操作指令
bash
# 刪除特定 instance 的所有資料
curl -X POST 'http://10.248.36.248:30990/api/v1/admin/tsdb/delete_series?match[]={instance="template-server-service.default.svc.cluster.local:9102"}'
# 刪除特定 metric + label 的資料
curl -X POST 'http://10.248.36.248:30990/api/v1/admin/tsdb/delete_series?match[]=gunicorn_request_duration_count{app="localhost"}'相關概念
- Grafana on Kubernetes 部署 — Prometheus 資料的視覺化前端
- Prometheus Exporter 部署模式 — Exporter 的通用 K8s 部署模式(Deployment + ClusterIP Service)
- Postgres Exporter 部署 — PostgreSQL 指標匯出
- Redis Exporter 部署 — Redis 指標匯出
- RabbitMQ Exporter 部署 — RabbitMQ 指標匯出
- Prometheus StatsD Exporter 部署 — 接收應用程式推送指標
- KEDA(Kubernetes Event-Driven Autoscaling) — 基於 Prometheus 指標觸發 Pod 自動擴縮
- RabbitMQ on Kubernetes 部署 — 可整合 RabbitMQ Exporter 提供指標給 Prometheus
- Redis on Kubernetes 部署與維運 — 可整合 Redis Exporter 提供指標給 Prometheus
- Node Exporter(VM 系統層監控) — 在 Ubuntu VM 上部署 Node Exporter,採集系統層指標