1. Prometheus 安裝
源碼安裝:
# wget https://github.com/prometheus/prometheus/releases/download/v1.7.1/prometheus-1.7.1.linux-amd64.tar.gz
# tar zxvf prometheus-1.7.1.linux-amd64.tar.gz
# cd prometheus-1.7.1.linux-amd6
# ./prometheus -config.file=prometheus.yml
當然你還想知道更多的參數 可以使用 ./prometheus–help
容器方式安裝:
1).安裝容器
yum install docker
systemctl enable docker
systemctl start docker
2).建立目錄和prometheus配置檔案
mkdir /prom
vi /prom/ Prometheus.yml
global:
scrape_interval: 30s
evaluation_interval: 30s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['122.15.3.29:9090']
labels:
instance: prometheus
3).拉取prometheus鏡像
docker pull quay.io/prometheus/prometheus
4).啟動prometheus
docker run -d \
-p 9090:9090 \
--name prometheus \
-v /prom/prometheus.yml:/etc/prometheus/prometheus.yml\
quay.io/prometheus/prometheus \
-config.file=/etc/prometheus/prometheus.yml \
-storage.local.path=/prometheus \
-storage.local.memory-chunks=10000
5).參數介紹
該-d選項啟動在獨立模式下的Prometheus的容器,這意味着容器将在背景啟動,并不會按終止CTRL+C。
該–name選項給定容器一個名字。
該-p 9090:9090的選項公開Prometheus的web端口(9090),并使其通過主機系統的外部IP位址通路。
該-v […]選項挂載prometheus.yml從主機檔案系統的配置檔案到哪裡Prometheus希望它(容器内的位置/etc/prometheus/prometheus.yml)。
該-config.file選項相應地設定到Prometheus配置檔案的位置,在容器内。
所述-storage.local.path選項配置在容器内的名額的存儲位置。
最後, -storage.local.memory-chunks選項調整Prometheus的記憶體使用主機系統的非常小的RAM(隻有512MB)和少量的儲存時間序列的量在本教程中(略低于1000)。它訓示Prometheus隻保留10000樣品塊記憶體(每列約10塊),而不是1048576這個預設的是更多的記憶體的機器上運作時的Prometheus和儲存
6).web界面展示
http://monitor_host:9090
要預警,首先得有監控資料,這裡就簡單的采集prometheus自身的資料,配置如上容器啟動方式中,官方提供很多監控其它資料源的exporter 和直連的
https://prometheus.io/docs/instrumenting/exporters/
2. Alertmanager安裝
源碼安裝:
$ mkdir -p $GOPATH/src/github.com/prometheus
$ cd $GOPATH/src/github.com/prometheus
$ git clone https://github.com/prometheus/alertmanager.git
$ cd alertmanager
$ make build
$ ./alertmanager-config.file= alertmanager.yml
配置郵箱發送的簡單例子alertmanager.yml
global:
resolve_timeout: 5m
# The smarthost and SMTP sender used for mail notifications.
smtp_smarthost: smtp.gmail.com:587 #發送郵箱伺服器smtp.163.com:25(163的)
smtp_from: [email protected] #發送郵箱位址
smtp_auth_username: [email protected] #郵箱使用者名
smtp_auth_password: sender_password #密碼
route:
group_by: [alertname]
repeat_interval: 1h
receiver: live-monitoring
receivers:
- name: live-monitoring
email_configs:
- to: [email protected] #接收郵箱位址
這樣alertmanager 就算準備好了,在啟動prometheus的時候,加入即可,例:
可以加入多個,高可用
3. 在prometheus 中配置規則,讓其生效并發送郵件
Prometheus.yml 加入
rule_files:
- "prometheus/rules/test.rules"
在 test.rules 加入一條簡單的名額測試
# Alert for any instance that have a medianrequest latency >1s.
ALERT APIHighRequestLatency
IF api_http_request_latencies_second{quantile="0.5"} >1
FOR 1m
ANNOTATIONS {
summary = "High request latency on {{$labels.instance }}",
description = "{{ $labels.instance }} has amedian request latency above 1s (current value: {{ $value }}s)",
}
重新開機prometheus,等待一會兒,就可以收到報警郵件了