Prometheus部署(一)

Prometheus是最初在SoundCloud上構建的開源系統監視和警報工具包。自2012年成立以來,許多公司和組織都採用了Prometheus,該項目擁有非常活躍的開發人員和用戶社區。Prometheus 於2016年加入了 Cloud Native Computing Foundation,這是繼Kubernetes之後的第二個託管項目。

官網:https://prometheus.io 最新版本: 2.19.0

Exporter是一個採集監控數據並通過Prometheus監控規範對外提供數據的組件,能爲Prometheus提供監控的接口。

Exporter將監控數據採集的端點通過HTTP服務的形式暴露給Prometheus Server,Prometheus Server通過訪問該Exporter提供的Endpoint端點,即可獲取到需要採集的監控數據。不同的Exporter負責不同的業務。

Prometheus              開源的系統監控和報警框架,靈感源自Google的Borgmon監控系統

AlertManager            處理由客戶端應用程序(如Prometheus server)發送的警報。它負責將重複數據刪除,分組和路由到正確的接收者集成,還負責沉默和抑制警報

Node_Exporter           用來監控各節點的資源信息的exporter,應部署到prometheus監控的所有節點

PushGateway             推送網關,用於接收各節點推送的數據並暴露給Prometheus server

文檔:https://prometheus.io/docs/introduction/overview/

下載prometheus各組件:

https://prometheus.io/download/


環境準備

  • 主機說明:
系統 ip 角色 cpu 內存 hostname
CentOS 7.8 192.168.30.135 prometheus、node1 >=2 >=2G prometheus
CentOS 7.8 192.168.30.136 altermanager、node2 >=2 >=2G altermanager
CentOS 7.8 192.168.30.137 grafana、node3 >=2 >=2G grafana
  • 全部關閉防火牆和selinux:
systemctl stop firewalld && systemctl disable firewalld

sed -i 's/=enforcing/=disabled/g' /etc/selinux/config  && setenforce 0

Prometheus介紹

  • prometheus的特點:
1. 多維的數據模型(基於時間序列的Key、Value鍵值對)

2. 靈活的查詢和聚合語言PromQL

3. 提供本地存儲和分佈式存儲

4. 通過基於HTTP的Pull模型採集時間序列數據

5. 可利用Pushgateway(Prometheus的可選中間件)實現Push模式

6. 可通過動態服務發現或靜態配置發現目標機器

7. 支持多種圖表和數據大盤
  • prometheus的組件:
1. Prometheus server,負責拉取、存儲時間序列數據

2. 客戶端庫(client library),插入應用程序代碼

3. 推送網關(push gateway),支持短暫的任務

4. 特殊類型的exporter,支持如HAProxy,StatsD,Graphite等服務

5. 一個alertmanager處理告警

6. 各種支持工具
  • prometheus的架構:

下圖說明了Prometheus的體系結構及其某些生態系統組件:

在這裏插入圖片描述

  • prometheus的使用場景:

prometheus非常適合記錄任何純數字時間序列。它既適合以機器爲中心的監視,也適合監視高度動態的面向服務的體系結構。在微服務世界中,它對多維數據收集和查詢的支持是一種特別的優勢。

prometheus的設計旨在提高可靠性,使其成爲中斷期間要使用的系統,從而使您能夠快速診斷問題。每個prometheus服務器都是獨立的,而不依賴於網絡存儲或其他遠程服務,當基礎設施部分出現問題時仍然可以使用它。


Prometheus概念

  • 數據模型:

prometheus將所有數據存儲爲時間序列:屬於相同 metric名稱和相同標籤組(鍵值對)的時間戳值流。

  • metric 和 標籤:

每一個時間序列都是由其 metric名稱和一組標籤(鍵值對)組成唯一標識。

metric名稱代表了被監控系統的一般特徵(如 http_requests_total代表接收到的HTTP請求總數)。它可能包含ASCII字母和數字,以及下劃線和冒號,它必須匹配正則表達式[a-zA-Z_:][a-zA-Z0-9_:]*

注意:冒號是爲用戶定義的記錄規則保留的,不應該被exporter使用。

標籤給prometheus建立了多維度數據模型:對於相同的 metric名稱,標籤的任何組合都可以標識該 metric的特定維度實例(例如:所有使用POST方法到 /api/tracks 接口的HTTP請求)。查詢語言會基於這些維度進行過濾和聚合。更改任何標籤值,包括添加或刪除標籤,都會創建一個新的時間序列。

標籤名稱可能包含ASCII字母、數字和下劃線,它必須匹配正則表達式[a-zA-Z_][a-zA-Z0-9_]*。另外,以雙下劃線__開頭的標籤名稱僅供內部使用。

標籤值可以包含任何Unicode字符。標籤值爲空的標籤被認爲是不存在的標籤。

  • 表示法:

給定 metric名稱和一組標籤,通常使用以下表示法標識時間序列:

<metric name>{<label name>=<label value>, ...}

例如,一個時間序列的 metric名稱是 api_http_requests_total,標籤是 method="POST"handler="/messages"。可以這樣寫:

api_http_requests_total{method="POST", handler="/messages"}

這和OpenTSDB的表示法是一樣的。

  • metric類型:
Counter             值只能單調增加或重啓時歸零,可以用來表示處理的請求數、完成的任務數、出現的錯誤數量等

Gauge               值可以任意增加或減少,可以用來測量溫度、當前內存使用等

Histogram           取樣觀測結果,一般用來請求持續時間或響應大小,並在一個可配置的分佈區間(bucket)內計算這些結果,提供所有觀測結果的總和
                        
                        累加的 counter,代表觀測區間:<basename>_bucket{le="<upper inclusive bound>"}
                        所有觀測值的總數:<basename>_sum
                        觀測的事件數量:<basenmae>_count

Summary             取樣觀測結果,一般用來請求持續時間或響應大小,提供觀測次數及所有觀測結果的總和,還可以通過一個滑動的時間窗口計算可分配的分位數
                        觀測的事件流φ-quantiles (0 ≤ φ ≤ 1):<basename>{quantile="φ"}
                        所有觀測值的總和:<basename>_sum
                        觀測的事件數量:<basename>_count
  • 實例與任務:

在prometheus中,一個可以拉取數據的端點叫做實例(instance),一般等同於一個進程。一組有着同樣目標的實例(例如爲彈性或可用性而複製的進程副本)叫做任務(job)。

當prometheus拉取目標時,它會自動添加一些標籤到時間序列中,用於標識被拉取的目標:

job:目標所屬的任務名稱

instance:目標URL中的<host>:<port>部分

如果兩個標籤在被拉取的數據中已經存在,那麼就要看配置選項 honor_labels 的值來決定行爲了。

每次對實例的拉取,prometheus會在以下的時間序列中保存一個樣本(樣本指的是在一個時間序列中特定時間點的一個值):

up{job="<job-name>", instance="<instance-id>"}:如果實例健康(可達),則爲 1 ,否則爲 0

scrape_duration_seconds{job="<job-name>", instance="<instance-id>"}:拉取的時長

scrape_samples_post_metric_relabeling{job="<job-name>", instance="<instance-id>"}:在 metric relabeling 之後,留存的樣本數量

scrape_samples_scraped{job="<job-name>", instance="<instance-id>"}:目標暴露出的樣本數量

up 時間序列對於實例的可用性監控來說非常有用。


Prometheus部署

  • 下載prometheus:
mkdir /software && cd /software

wget https://github.com/prometheus/prometheus/releases/download/v2.19.0/prometheus-2.19.0.linux-amd64.tar.gz

tar xf prometheus-2.19.0.linux-amd64.tar.gz

mv prometheus-2.19.0.linux-amd64 /usr/local/prometheus
  • 安裝prometheus:
useradd -M -s /sbin/nologin prometheus

mkdir -p /data/prometheus

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

vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target

[Service]
Type=simple
Environment="GOMAXPROCS=4"
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/data/prometheus \
  --storage.tsdb.retention=30d \
  --web.console.libraries=/usr/local/prometheus/console_libraries \
  --web.console.templates=/usr/local/prometheus/consoles \
  --web.listen-address=0.0.0.0:9090 \
  --web.read-timeout=5m \
  --web.max-connections=10 \
  --query.max-concurrency=20 \
  --query.timeout=2m \
  --web.enable-lifecycle
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
NoNewPrivileges=true
LimitNOFILE=infinity
ReadWriteDirectories=/data/prometheus
ProtectSystem=full

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target
  • 啓動prometheus:
systemctl daemon-reload

systemctl enable prometheus && systemctl start prometheus

netstat -lntp | grep prometheus

tcp6       0      0 :::9090                 :::*                    LISTEN      43742/prometheus

訪問ip:9090

在這裏插入圖片描述

prometheus部署完成,接下來需要配置prometheus。


Prometheus配置

prometheus的配置文件prometheus.yml,它主要分以下幾個配置塊:

全局配置        global

告警配置        alerting

規則文件配置    rule_files

拉取配置        scrape_configs

遠程讀寫配置    remote_read、remote_write
  • 全局配置 global

global指定在所有其他配置上下文中有效的參數。還可用作其他配置部分的默認設置。

global:
  # 默認拉取頻率
  [ scrape_interval: <duration> | default = 1m ]

  # 拉取超時時間
  [ scrape_timeout: <duration> | default = 10s ]

  # 執行規則頻率
  [ evaluation_interval: <duration> | default = 1m ]

  # 通信時添加到任何時間序列或告警的標籤
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    [ <labelname>: <labelvalue> ... ]

  # 記錄PromQL查詢的日誌文件
  [ query_log_file: <string> ]
  • 告警配置 alerting

alerting指定與Alertmanager相關的設置。

alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]
  • 規則文件配置 rule_files

rule_files指定prometheus加載的任何規則的位置,從所有匹配的文件中讀取規則和告警。目前沒有規則。

rule_files:
  [ - <filepath_glob> ... ]
  • 拉取配置 scrape_configs

scrape_configs指定prometheus監控哪些資源。默認會拉取prometheus本身的時間序列數據,通過http://localhost:9090/metrics進行拉取。

一個scrape_config指定一組目標和參數,描述如何拉取它們。在一般情況下,一個拉取配置指定一個作業。在高級配置中,這可能會改變。

可以通過static_configs參數靜態配置目標,也可以使用支持的服務發現機制之一動態發現目標。

此外,relabel_configs在拉取之前,可以對任何目標及其標籤進行修改。

scrape_configs:
job_name: <job_name>

# 拉取頻率
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]

# 拉取超時時間
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]

# 拉取的http路徑
[ metrics_path: <path> | default = /metrics ]

# honor_labels 控制prometheus處理已存在於收集數據中的標籤與prometheus將附加在服務器端的標籤("作業"和"實例"標籤、手動配置的目標標籤和由服務發現實現生成的標籤)之間的衝突
# 如果 honor_labels 設置爲 "true",則通過保持從拉取數據獲得的標籤值並忽略衝突的服務器端標籤來解決標籤衝突
# 如果 honor_labels 設置爲 "false",則通過將拉取數據中衝突的標籤重命名爲"exported_<original-label>"來解決標籤衝突(例如"exported_instance"、"exported_job"),然後附加服務器端標籤
# 注意,任何全局配置的 "external_labels"都不受此設置的影響。在與外部系統的通信中,只有當時間序列還沒有給定的標籤時,它們才被應用,否則就會被忽略
[ honor_labels: <boolean> | default = false ]

# honor_timestamps 控制prometheus是否遵守拉取數據中的時間戳
# 如果 honor_timestamps 設置爲 "true",將使用目標公開的metrics的時間戳
# 如果 honor_timestamps 設置爲 "false",目標公開的metrics的時間戳將被忽略
[ honor_timestamps: <boolean> | default = true ]

# 配置用於請求的協議
[ scheme: <scheme> | default = http ]

# 可選的http url參數
params:
  [ <string>: [<string>, ...] ]

# 在每個拉取請求上配置 username 和 password 來設置 Authorization 頭部,password 和 password_file 二選一
basic_auth:
  [ username: <string> ]
  [ password: <secret> ]
  [ password_file: <string> ]

# 在每個拉取請求上配置 bearer token 來設置 Authorization 頭部,bearer_token 和 bearer_token_file 二選一
[ bearer_token: <secret> ]

# 在每個拉取請求上配置 bearer_token_file 來設置 Authorization 頭部,bearer_token_file 和 bearer_token 二選一
[ bearer_token_file: /path/to/bearer/token/file ]

# 配置拉取請求的TLS設置
tls_config:
  [ <tls_config> ]

# 可選的代理URL
[ proxy_url: <string> ]

# Azure服務發現配置列表
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# Consul服務發現配置列表
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# DNS服務發現配置列表
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# EC2服務發現配置列表
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# OpenStack服務發現配置列表
openstack_sd_configs:
  [ - <openstack_sd_config> ... ]

# file服務發現配置列表
file_sd_configs:
  [ - <file_sd_config> ... ]

# GCE服務發現配置列表
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# Kubernetes服務發現配置列表
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# Marathon服務發現配置列表
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# AirBnB's Nerve服務發現配置列表
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# Zookeeper Serverset服務發現配置列表
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# Triton服務發現配置列表
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# 靜態配置目標列表
static_configs:
  [ - <static_config> ... ]

# 目標relabel配置列表
relabel_configs:
  [ - <relabel_config> ... ]

# metric relabel配置列表
metric_relabel_configs:
  [ - <relabel_config> ... ]

# 每次拉取樣品的數量限制
# metric relabelling之後,如果有超過這個數量的樣品,整個拉取將被視爲失效。0表示沒有限制
[ sample_limit: <int> | default = 0 ]
  • 遠程讀寫配置 remote_read/remote_write

remote_read/remote_write將數據源與prometheus分離,當前不做配置。

# 與遠程寫功能相關的設置
remote_write:
  [ - <remote_write> ... ]

# 與遠程讀功能相關的設置
remote_read:
  [ - <remote_read> ... ]
  • 簡單配置示例:
vim /usr/local/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

Node Exporter部署

  • 下載node_exporter:
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

tar xf node_exporter-1.0.1.linux-amd64.tar.gz

mv node_exporter-1.0.1.linux-amd64 /usr/local/node_exporter
  • 安裝node_exporter:
useradd -M -s /sbin/nologin prometheus              #若已創建,可省略該步

chown -R prometheus:prometheus /usr/local/node_exporter

vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/node_exporter/node_exporter \
  --web.listen-address=0.0.0.0:9100 \
  --web.telemetry-path=/metrics \
  --log.level=info \
  --log.format=logfmt
Restart=always

[Install]
WantedBy=multi-user.target
  • 啓動node_exporter:
systemctl daemon-reload

systemctl enable node_exporter && systemctl start node_exporter

netstat -lntp | grep node_exporter

tcp6       0      0 :::9100                 :::*                    LISTEN      2725/node_exporter

curl http://localhost:9100/metrics | head

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 64410    0 64410    0     0  5761k      0 --:--:-- --:--:-- --:--:-- 6290k
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.

node exporter展示了prometheus可以拉取的指標,包括在輸出中更下方的各種系統指標(帶有前綴node_)。要查看這些指標(以及幫助和類型信息):

curl http://localhost:9100/metrics | grep 'node_'
  • 配置scrape_configs

啓動好node_exporter後,還需要配置prometheus才能訪問node exporter指標。

vim /usr/local/prometheus/prometheus.yml                #修改 scrape_configs 內容
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['192.168.30.135:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['192.168.30.135:9100','192.168.30.136:9100','192.168.30.137:9100']
systemctl restart prometheus
  • 查看node狀態:

訪問prometheus頁面,StatusTargets

在這裏插入圖片描述

可以看到,之前部署的node exporter狀態是UP,說明運行正常。

通過部署的node_exporter可以收集當前主機的系統基礎信息。如查看系統15分鐘平均負載,

在這裏插入圖片描述

node_exporter部署完成。


AlertManager部署

  • 下載alertmanager:
wget https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz

tar xf alertmanager-0.21.0.linux-amd64.tar.gz

mv alertmanager-0.21.0.linux-amd64 /usr/local/alertmanager
  • 安裝alertmanager:
useradd -M -s /sbin/nologin prometheus              #若已創建,可省略該步

mkdir /usr/local/alertmanager/data

chown -R prometheus:prometheus /usr/local/alertmanager

vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=Alertmanager
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/alertmanager/alertmanager \
  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data \
  --web.listen-address=0.0.0.0:9093 \
  --cluster.listen-address=0.0.0.0:9094 \
  --log.level=info \
  --log.format=logfmt
Restart=always

[Install]
WantedBy=multi-user.target
  • 啓動alertmanager:
systemctl daemon-reload

systemctl enable alertmanager && systemctl start alertmanager

netstat -lntp | grep alertmanager

tcp6       0      0 :::9093                 :::*                    LISTEN      33654/alertmanager  
tcp6       0      0 :::9094                 :::*                    LISTEN      33654/alertmanager

curl localhost:9093/metrics | head

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
# HELP alertmanager_alerts How many alerts by state.
# TYPE alertmanager_alerts gauge
alertmanager_alerts{state="active"} 0
alertmanager_alerts{state="suppressed"} 0
# HELP alertmanager_alerts_invalid_total The total number of received alerts that were invalid.
# TYPE alertmanager_alerts_invalid_total counter
alertmanager_alerts_invalid_total{version="v1"} 0
alertmanager_alerts_invalid_total{version="v2"} 0
# HELP alertmanager_alerts_received_total The total number of received alerts.
# TYPE alertmanager_alerts_received_total counter
  • 配置alerting

啓動好alertmanager後,還需要配置prometheus才能通過alertmanager告警。

vim /usr/local/prometheus/prometheus.yml                #更改 alerting 內容
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - 192.168.30.136:9093
      
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['192.168.30.135:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['192.168.30.135:9100','192.168.30.136:9100','192.168.30.137:9100']

  - job_name: 'alertmanager'
    static_configs:
    - targets: ['192.168.30.136:9093']
systemctl restart prometheus

訪問prometheus頁面,StatusTargets

在這裏插入圖片描述

可以看到,之前部署的alertmanager狀態是UP,說明運行正常。

alertmanager部署完成。但alertmanager還需要進一步配置通知路由和通知接收者。


AlertManager配置

alertmanager通過命令行標誌和配置文件進行配置。命令行標誌配置不可變的系統參數時,配置文件定義禁止規則,通知路由和通知接收器。

alertmanager的配置文件alertmanager.yml,它主要分以下幾個配置塊:

全局配置        global

通知模板        templates

路由配置        route

接收器配置      receivers

抑制配置        inhibit_rules
  • 全局配置 global

global指定在所有其他配置上下文中有效的參數。還用作其他配置部分的默認設置。

global:
  # 默認的SMTP頭字段
  [ smtp_from: <tmpl_string> ]
  
  # 默認的SMTP smarthost用於發送電子郵件,包括端口號
  # 端口號通常是25,對於TLS上的SMTP,端口號爲587
  # Example: smtp.example.org:587
  [ smtp_smarthost: <string> ]
  
  # 要標識給SMTP服務器的默認主機名
  [ smtp_hello: <string> | default = "localhost" ]
  
  # SMTP認證使用CRAM-MD5,登錄和普通。如果爲空,Alertmanager不會對SMTP服務器進行身份驗證
  [ smtp_auth_username: <string> ]
  
  # SMTP Auth using LOGIN and PLAIN.
  [ smtp_auth_password: <secret> ]
  
  # SMTP Auth using PLAIN.
  [ smtp_auth_identity: <string> ]
  
  # SMTP Auth using CRAM-MD5.
  [ smtp_auth_secret: <secret> ]
  
  # 默認的SMTP TLS要求
  # 注意,Go不支持到遠程SMTP端點的未加密連接
  [ smtp_require_tls: <bool> | default = true ]

  # 用於Slack通知的API URL
  [ slack_api_url: <secret> ]
  [ victorops_api_key: <secret> ]
  [ victorops_api_url: <string> | default = "https://alert.victorops.com/integrations/generic/20131114/alert/" ]
  [ pagerduty_url: <string> | default = "https://events.pagerduty.com/v2/enqueue" ]
  [ opsgenie_api_key: <secret> ]
  [ opsgenie_api_url: <string> | default = "https://api.opsgenie.com/" ]
  [ wechat_api_url: <string> | default = "https://qyapi.weixin.qq.com/cgi-bin/" ]
  [ wechat_api_secret: <secret> ]
  [ wechat_api_corp_id: <string> ]

  # 默認HTTP客戶端配置
  [ http_config: <http_config> ]

  # 如果告警不包括EndsAt,則ResolveTimeout是alertmanager使用的默認值,在此時間過後,如果告警沒有更新,則可以聲明警報已解除
  # 這對Prometheus的告警沒有影響,它們包括EndsAt
  [ resolve_timeout: <duration> | default = 5m ]
  • 通知模板 templates

templates指定了從其中讀取自定義通知模板定義的文件,最後一個文件可以使用一個通配符匹配器,如templates/*.tmpl

templates:
  [ - <filepath> ... ]
  • 路由配置 route

route定義了路由樹中的節點及其子節點。如果未設置,則其可選配置參數將從其父節點繼承。

每個告警都會在已配置的頂級路由處進入路由樹,該路由樹必須與所有告警匹配(即沒有任何已配置的匹配器),然後它會遍歷子節點。如果continue設置爲false,它將在第一個匹配的子項之後停止;如果continue設置爲true,則告警將繼續與後續的同級進行匹配。如果告警與節點的任何子節點都不匹配(不匹配的子節點或不存在子節點),則根據當前節點的配置參數來處理告警。

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'
  • 接收器配置 receivers

receivers是一個或多個通知集成的命名配置。建議通過webhook接收器實現自定義通知集成。

receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:5001/'
  • 抑制規則配置 inhibit_rules

當存在與另一組匹配器匹配的告警(源)時,抑制規則會使與一組匹配器匹配的告警(目標)“靜音”。目標和源告警的equal列表中的標籤名稱都必須具有相同的標籤值。

在語義上,缺少標籤和帶有空值的標籤是相同的。因此,如果equal源告警和目標告警都缺少列出的所有標籤名稱,則將應用抑制規則。

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
  • 默認配置示例:
vim /usr/local/alertmanager/alertmanager.yml
global:
  resolve_timeout: 5m

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:5001/'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

Grafana部署

grafana 是一款採用 go 語言編寫的開源應用,主要用於大規模指標數據的可視化展現,是網絡架構和應用分析中最流行的時序數據展示工具,目前已經支持絕大部分常用的時序數據庫。

官網:https://grafana.com

  • 安裝grafana:
vim /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
yum makecache fast

yum install -y grafana

systemctl daemon-reload

systemctl enable grafana-server && systemctl start grafana-server

netstat -lntp | grep 3000


訪問ip:3000,初始賬號密碼爲adminadmin,建議後面更改密碼。

在這裏插入圖片描述

在這裏插入圖片描述

grafana部署完成。grafana配置文件:/etc/grafana/grafana.ini

  • 導入prometheus數據源:

ConfigurationData SourcesPrometheusSelect,填入http://ip:9090,保存即可。

在這裏插入圖片描述

  • 導入dashboard:

官方dashboard模板:https://grafana.com/grafana/dashboards

選擇排行第一的中文模板:1 Node Exporter for Prometheus Dashboard CN v20200628,模板ID是8919。

ManageImport,填入模板ID,導入,

在這裏插入圖片描述

自定義dashboard名稱,選擇數據源Prometheus

在這裏插入圖片描述

在這裏插入圖片描述

至此,prometheus + grafana 部署完成。接下來詳細配置prometheus的監控與告警。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章