Prometheus監控
架構

優點
- 外部依賴少,效能優秀,部署方便
- 完善的資料模型,豐富的外掛整合
- 提供強大的查詢語言
模組構成
- Server:核心服務模組,取樣並存儲時間序列資料(預設管理面板埠9090)
- Retrieval 取樣模組
- Storage 儲存模組
- PromQL 查詢模組
- PushGateway(可選元件):資料閘道器代理模組,取樣資料臨時儲存,與server通訊
- Export:資料匯出模組,匯出服務監控資料。
- Alertmanager:告警模組。接受prometheus上出發alertrules的告警,合併去重聚合處理,併發送出去(支援企業微信,郵箱,釘釘,webhook等等)
- Grafna:比Prometheus原生UI更強大的視覺化介面(類似於Kibana的視覺化分析平臺),更專注於伺服器及應用效能的分析,如CPU、記憶體、流量等等的圖表分析
時間序列
- 若干標籤關聯下的指標取樣值,隨著時間維度的推進,構成一條時間序列
- 命名規範:應用名稱 _ 監測對像 _ 數值型別 _ 單位,比如http_request_total
- 所有指標值採用float64型別儲存
圖表型別
- Counter:計數值,只增不減
- Gauge:常規數值,可增可見
- Histogram:直方圖
- xxx_bucker{le="上邊界"}:時間序列分桶聚合
- xxx_sum:值累計
- xxx_count:次數累計
- Summery:類似於Histogram,支援quantiles(即按百分比取取樣值)
- xxx{quantile="邊界"}:
- xxx_sum:值累計
- xxx_count:次數累計
Exporter
常用Exporter
- cAdvisor:K8S預設所有主機部署cAdvisor(高版本不再預設),用於提供容器相關的效能指標資料
- node_exporter:主機層次的指標資料,cpu、記憶體、磁碟等
- nginx_exporter:nginx指標輸出
自動監控
已實現取樣介面邏輯的資源,可通過annotation標籤自動將其加入監控
- Pod資源
- prometheus.io/scrape=true
- prometheus.io/path=/metric
- prometheus.io/port=8080
- Service資源
- prometheus.io/probe
- Endpoint資源
- prometheus.io/scrape
- prometheus.io/path
- prometheus.io/port
配置
主配置
global: #服務端全域性配置 scrape_interval: 10s #採集週期 scrape_timeout: 10s evaluation_interval: 10s #rule計算週期 rule_files: #報警規則設定 - "/etc/prometheus-rules/*.rules" scrape_configs: #監控資源配置 - job_name: 'prometheus' #server自身監控 static_configs: - targets: ['localhost:9090'] #預設採集路徑是/metrics上開放的埠服務 - job_name: node #主機資源監控 static_configs: - targets: ['localhost:9100'] - job_name: 'kubernetes-node-exporter' tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token kubernetes_sd_configs: - role: node relabel_configs: - source_labels: [__address__] regex: '(.*):10250' replacement: '${1}:10255' target_label: __address__
關於relabel_configs配置
- 用於在目標被採集之前重寫其標籤集合
- 目標採集前自動追加的標籤
- job:值為job_name
- _ address _:採集目標的主機埠地址
- relabel期間額外提供了__meta_字首的標籤(服務發現機制提供的標籤)
- relabel操作結束後自動追加的標籤
- instance:設定為__address__標籤值(如果relabel期間未配置)
- _ scheme _:採集請求的協議
- _ metrics_path _:採集請求的路徑
- _ param < name >:設定為採集請求url引數中的name欄位值
- relabel操作結束後__字首的標籤被自動清除
標籤操作
- replace:針對source_labels正則匹配,賦值target_label為replacement值(正則匹配失敗則不操作)
- keep:丟棄source_labels正則不匹配的採集目標
- drop:丟棄source_labels正則匹配的採集目標
- labelmap:對映正則匹配標籤的值到replacement指定的標籤上
- labeldrop:剔除掉正則匹配的標籤
- labelkeep:僅保留正則匹配的標籤
相關配置欄位
(.*)
告警配置
groups: - name: test-rule rules: - alert: KubeCPUOvercommit expr: sum(kube_resourcequota{job="kube-state-metrics",resource="requests.cpu",type="hard"}) / sum(node:node_num_cpu:sum) > 1.5 for: 5m labels: severity: warning annotations: message: Overcommited CPU resource request quota on Namespaces.
查詢語言
http_requests_total{method=”POST”, code="200"} #標籤過濾 count(http_requests_total) #時間序列統計 rate(http_requests_total[1m]) #最近一分鐘每秒請求量
安裝
服務編排方案 採用開源方案: https://github.com/giantswarm/kubernetes-prometheus
針對國內具體場景做了調整: https://github.com/maifusha/kubernetes-prometheus
(feature/optimize分支)
Bug ClusterIP /data
Helm編排方案
- 變更配置不方便,尤其是Prometheus系統棧配置較多
- 適合於快速啟動的測試
功能使用
Prometheus
/-/reload
Grafana
admin:admin
grafana-cli plugins install grafana-piechart-panel
AlertMagager
本文轉自開源中國- ofollow,noindex">Prometheus監控