k8s prometheus annotations scrape config
prometheus 在 k8s 環境中
利用 kubernetes_sd_config
可以自動發現 meta labels
可以用於動態設定 scrape target
因此在 k8s 環境中 prometheus 的 scrape config 幾乎是固定的
如果使用 prometheus helm chart 安裝 prometheus 時, 已經寫好 scrape config
readme 有簡短的說明 Scraping Pod Metrics via Annotations
簡單來說就是只要加上 pod annotations 即可被 prometheus 監控
 | 
 | 
然而這說明實在太過簡單
實際上官方提供的 scrape config 可以達成的功能並不只這樣
實際了解 vaule file
可以了解 annotations 設定方式也支援 service endpoint
將 annotations 設定在 service, 會自動對該 service 下所有 endpoint target scrape metrics
以下詳細說明能支援的 annotations config
annotations config format
以下為支援的 annotations 格式
 | 
 | 
example annotations config
以下提供範例 如果設定
 | 
 | 
那麼抓取 URL 會變成 https://<pod_ip>:9527/metrics_hello?token=mysecrettoken
enahance annotations scrape config
既然 annotations 可以動態調整, 同理也能用它來調整 scrape interval。
在 charts values 的 scrape job_name: ‘kubernetes-pods’ 中加入這段 relabel_configs:
 | 
 | 
接著設定 annotations:
 | 
 | 
如此一來,即可單獨調整每個 target 的 scrape interval。
Conclusion
透過 annotations 設定 scrape 相當簡易,無需像 Prometheus Operator 那樣新增 manifest。
這表示在使用各種 Helm charts 時,基本上都能直接設定監控,避免多出一個不必要的 PodMonitors manifest。
這方式也是目前大部分 exporter chart 的預設 monitor 方式
prometheus-node-exporter
然而,annotations 不適合設定過於複雜的內容,因此與 Prometheus Operator 搭配使用會是更合理的選擇。