Skip to content

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 NameTarget(s)Port說明
postgres-exporterpostgres-exporter-prometheus-postgres-exporter / postgres-exporter2-...80PostgreSQL
redis-exporterredis-exporter-prometheus-redis-exporter9121Redis
rabbitmq-exporterrabbitmq-exporter-prometheus-rabbitmq-exporter9419RabbitMQ (HTTP)
rabbitmq-https-exporter10.30.196.5832074RabbitMQ (HTTPS)
statsd-exporterstatsd-exporter-prometheus-statsd-exporter / 多個 NodePort9102 / 31702StatsD

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-apiweb.enable-lifecycle 需明確啟用
  • HTTPS metrics 抓取需在 job 層級設定 scheme: httpstls_config
  • scrape_timeout 必須小於 scrape_interval,否則 Prometheus 會報錯

實際應用

Prometheus 作為整個監控體系的資料收集中心,與各個 Exporter 組成完整的監控管線:各服務的 Exporter 暴露 /metrics 端點,Prometheus 定期抓取並存儲,Grafana 查詢 Prometheus 進行視覺化與告警。當需要監控新服務時,只需在 values.yamlextraScrapeConfigs 中新增對應的 Scrape Job,並 helm upgrade 即可。

部署設定參考

以下為實際部署時使用的完整設定,供日後查詢與複製使用。

環境參數

項目
Namespacemonitoring
StorageClassnfs-client
PVC 大小20Gi
資料保存期限1y
Prometheus Web UIhttp://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: false

Admin 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"}'

相關概念

來源