天天看點

Prometheus實戰-從0建構高可用監控平台(二)

當今的網際網路應用系統越來越複雜,其中涉及的元件和服務越來越多,需要進行高效、可靠的監控,以保證系統的穩定性和性能。Prometheus是一款功能強大的開源監控系統,可以實時監控多個次元的名額資料,并支援強大的查詢語言和告警機制,是目前廣泛使用的雲原生應用監控系統之一。

本文檔合集《Prometheus實戰:從0建構高可用監控平台》将從零開始,手把手教您如何建構一套高可用的Prometheus監控平台,涵蓋了以下内容:

1. Prometheus叢集搭建:實作高可用和可擴充的監控系統

2. 動态監控名額:自動發現和注冊要監控的目标

3. 告警機制配置:靈活配置告警規則、分組、過濾、抑制,實時通知異常情況

4. Grafana可視化展示:直覺了解系統運作狀态和趨勢

本文檔合集的目标讀者是具有一定Linux系統和網絡知識的系統管理者和DevOps工程師。通過本文檔合集的學習,您将掌握Prometheus的核心概念和實踐技巧,能夠快速搭建一套高效、可靠的監控平台,幫助您更好地管理和維護複雜的網際網路應用系統。

本文内容是基于Thanos安裝一個高可用和可擴充的Prometheus叢集。

基礎資訊見上一篇文章,這篇主要介紹Prometheus叢集搭建和配置注意事項

Prometheus全家桶

如之前規劃,node2 和 node3 都安裝 Prometheus , Alertmanager, PrometheusAlert.

Prometheus叢集安裝

node2 node3 都執行以下安裝内容
cd /usr/local/src/
wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar xf prometheus-2.37.0.linux-amd64.tar.gz -C /usr/local
ln -sv /usr/local/prometheus-2.37.0.linux-amd64 /usr/local/prometheus

groupadd -r prometheus
useradd -r -g prometheus -s /sbin/nologin -c "prometheus Daemons" prometheus

mkdir /data/prometheus
chown -R prometheus. /data/prometheus
chown prometheus:prometheus /usr/local/prometheus/ -R


cat > /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.enable-admin-api --web.enable-lifecycle --storage.tsdb.path=/data/prometheus --storage.tsdb.retention=15d
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=655350
LimitNPROC=655350
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus           

允許使用者通過HTTP請求來查詢、配置和管理Prometheus執行個體。

// 啟動時候添加參數--web.enable-admin-api
/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.enable-admin-api --storage.tsdb.path=/data/prometheus

curl -X POST -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={job="JMX"}'           

Config

上面設定好就可以配置采集節點和告警規則。具體見後面文章

mkdir /usr/local/prometheus/{rules,targets}           
Prometheus配置檔案
global:
  external_labels:
    region: "fb"
alerting:
  alertmanagers:
    - static_configs:
      - targets:
        - 10.2.0.27:19093
rule_files:
  - "rules/*.yml"
scrape_configs:
  - job_name: 'consul-exporter'
    metrics_path: /metrics
    scheme: http
    consul_sd_configs:
      - server: localhost:8500
        token: 'xxxxxxxx'
        services: ['consul_exporter']
    relabel_configs:
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap
  
  - job_name: "prometheus"
    static_configs:
      - targets:
        - '10.2.0.10:9090'
        - '10.2.0.41:9090'
  - job_name: "alertmanager"
    static_configs:
      - targets:
        - '10.2.0.10:9093'
        - '10.2.0.41:9093'
remote_write:
  - url: http://10.2.0.6:10908/api/v1/receive
    headers:
      THANOS-TENANT: fb
  - url: http://10.2.0.10:10908/api/v1/receive
    headers:
      THANOS-TENANT: fb
  - url: http://10.2.0.41:10908/api/v1/receive
    headers:
      THANOS-TENANT: fb           

注意點:

  1. node2 和 node3 是同一業務線的高可用的Prometheus叢集,故兩個節點配置一樣。同其他業務線差別使用external_labels(這個配置項也是thanos做資料去重的重要依據), 如:
global:
  external_labels:
    region: "bf"           
  1. 安裝之前的規劃,Alertmanager 是主備模式,這個地方填的是 keepalived 配置的 vip (haproxy),見下一篇文章。
  2. 這裡配置了一個consul_sd_configs,友善動态添加收集節點 , token對應上篇文章cosul安裝的時候生成的token。
添加服務的功能腳本見: https://mp.weixin.qq.com/s/rXMsnY-j_xQj9JiKhllOqQ           
  1. 由于prometheus 和 alertmanager 一般不會變,這裡使用了static_configs收集相關服務的名額資料。
  2. remote_write 對應 上篇文章 -- Receive配置。

繼續閱讀