Prometheus-operator (prometheus-stack) 簡介
前言
目前使用 Helm 來安裝時採用的方法,透過自定義 CRD (Kubernetes Custom Resource) 來生成 prometheus 的各種元件
也因為使用 CRD 的關係,學習上會比較不直觀,這部分還是需要對 prometheus 官方文件熟悉一點
預計之後會再針對基本的 prometheus, alertmanager 設定再整理一下
會把 API 存在 monitoring.coreos.com/v1
1 | helm repo add prometheus-community https://prometheus-community.github.io/helm-charts |
這個 stack 包含了很多東西:Prometheus, Prometheus Operator, Alertmanager, Grafana, kube-state-metrics, prometheus-node-exporter …等元件
以下是主要額外定義的 CRD:
Prometheus:聲明式和管理 Prometheus Server 實例;- config 使用 secret 存放,為
.yaml.gz格式 (可用gzip -d解壓)
- config 使用 secret 存放,為
ServiceMonitor:負責聲明式的管理監控配置;- 依照設定的內容產生 jobs config 內容,並且更新到
Prometheus
- 依照設定的內容產生 jobs config 內容,並且更新到
PrometheusRule:負責聲明式的管理告警配置;- 定義 prometheus rules 使用 (新增於 configmap)
- Rule 的設定是透過
configmap掛給Prometheus的,名稱為prometheus-prometheus-operator-kube-p-prometheus-rulefiles-0
Alertmanager:聲明式的建立和管理 Alertmanager 實例。- 使用
alertmanagerconfigs丟入設定檔動態載入
- 使用
Prometheus 這個 Pod 會跑 2 個 container,image 分別為:
prometheus-config-reloader負責監控 secret 物件,一有變動就產生新的 config- 可看到
prometheus-config-reloader的執行指令參數--watch-dir就是指定這個configmap掛載的點,因此只要新增configmap就可以新增 rule prometheus實際跑 server 的 container,是讀取config_out目錄上的設定檔
- 可看到
prometheus 的設定是使用 secret 的方式來掛載,經過 initcontainer 處理後轉成可使用的 config,目錄在 /etc/prometheus/config_out/prometheus.env.yaml
config:由 secret 掛載config_out(實際載入的):emptyDir 共用 (由 reloader 產生)rule_files:由 configMap 掛載- 使用
PrometheusRule來新增,他會去更新 configMap
- 使用
在 Prometheus stack 的系統中,都是透過 labels 來連接的,以下幾個例子:
- Prometheus instance ↔ ServiceMonitors (產生對應 jobs)
- ServiceMonitors ↔ Service (設定 metrices 位置)
- PrometheusRule ↔ Prometheus instance (ruleSelector)
ServiceMonitor
透過綁定 Service 來監控 APP,綁定的方式是使用 label 加上對應的 port Name
以下範例是已經有建立了一個 deployment 與 svc
該 deployment 的 image 設計已經對 /metrics 丟了一些資料,因此沒有利用到額外的 exporter
1 | apiVersion: monitoring.coreos.com/v1 |
設定完後,就可以在 prometheus 上看到新的 target 抓取該 APP 的 metrics
Prometheus instance
這部分比較不會調整到,可聚焦在 label selector 的部分
- 有 serviceMonitorNamespaceSelector, podMonitorSelector ruleSelector
初始化可以使用 values.yaml 調整,也可以直接 kubectl edit
1 | apiVersion: monitoring.coreos.com/v1 |
PrometheusRule
產生 jobs,內容與原本的 prometheus rule 很接近
1 | apiVersion: monitoring.coreos.com/v1 |
AlertmanagerConfig
這部分會與原本的 secret alertmanager-prometheus-operator-kube-p-alertmanager 一起使用,定義的內容都會是在 secret 內部的 (所以要改 global 的設定需要去調整 secret)
要注意的是在 route 的部分,採用這個方式建立的都會自動產生一個 matcher: namespace 來讓每一條 config 都只負責自己的 namespace
以下是簡易設定 slack 通知的
1 | apiVersion: monitoring.coreos.com/v1alpha1 |
小結
這邊算是做一個簡易的紀錄,把各元件基本的設定寫下來,但實際上要先理解原本 prometheus/alertmanager 後才有辦法設定這些,這邊網路上的資源蠻多的,之後再來整理整理
參考資料
Prometheus Operator — 為 Kubernetes 設定及管理 Prometheus | by smalltown | getamis
使用Operator管理Prometheus - prometheus-book (gitbook.io)
通过ServiceMonitor创建服务发现 (aliyun.com)
Prometheus-operator (prometheus-stack) 簡介
https://raylin9981.github.io/2022/09/14/Prometheus-operator-prometheus-stack-簡介/