天天看點

Prometheus+Alertmanager配置郵件報警

(1)安裝Alertmanager

安裝在k8s-node2伺服器上

[root@k8s-node2 ~]# tar -zxvf alertmanager-0.21.0.linux-amd64.tar.gz

alertmanager-0.21.0.linux-amd64/

alertmanager-0.21.0.linux-amd64/alertmanager

alertmanager-0.21.0.linux-amd64/amtool

alertmanager-0.21.0.linux-amd64/NOTICE

alertmanager-0.21.0.linux-amd64/LICENSE

alertmanager-0.21.0.linux-amd64/alertmanager.yml

[root@k8s-node2 ~]# ls

alertmanager-0.21.0.linux-amd64  alertmanager-0.21.0.linux-amd64.tar.gz  anaconda-ks.cfg  disk_free.sh  node_exporter-1.0.1.linux-amd64.tar.gz

[root@k8s-node2 ~]# cp alertmanager-0.21.0.linux-amd64/alertmanager /usr/local/bin/

[root@k8s-node2 ~]# cp alertmanager-0.21.0.linux-amd64/amtool /usr/local/bin/

[root@k8s-node2 ~]# alertmanager  --version

Prometheus+Alertmanager配置郵件報警

(2)配置Alertmanager

[root@k8s-node2 ~]# mkdir -pv /etc/alertmanager

mkdir: 已建立目錄 "/etc/alertmanager"

[root@k8s-node2 ~]# cp alertmanager-0.21.0.linux-amd64/alertmanager.yml  /etc/alertmanager/

[root@k8s-node2 ~]# cat /etc/alertmanager/alertmanager.yml 預設配置檔案

Prometheus+Alertmanager配置郵件報警

(3)重新配置Alertmanager

[root@k8s-node2 alertmanager]# cp alertmanager.yml alertmanager.yml.bak

[root@k8s-node2 alertmanager]# ls

alertmanager.yml  alertmanager.yml.bak

[root@k8s-node2 ~]# cat /etc/alertmanager/alertmanager.yml

[root@k8s-node2 alertmanager]# more alertmanager.yml

global:

  smtp_smarthost: 'smtp.qq.com:25'

  smtp_from: '[email protected]'

  smtp_auth_username: '[email protected]'

  smtp_auth_password: '******'

  smtp_require_tls: false 

route:

  receiver: mail   

receivers:

- name: 'mail'

  email_configs:

  - to: '[email protected]'

[root@k8s-node2 alertmanager]# 

(4)啟動Alertmanager

[root@k8s-node2 alertmanager]# alertmanager --config.file alertmanager.yml

Prometheus+Alertmanager配置郵件報警
Prometheus+Alertmanager配置郵件報警

(5)在Prometheus上添加Alertmanage 

# Alertmanager configuration

alerting:

  alertmanagers:

  - static_configs:

    - targets:

       - 192.168.20.43:9093

(6)在Prometheus上添加對Alertmanage監控

 - job_name: 'alertmanager'

    static_configs:

    - targets: ['192.168.20.43:9093']

Prometheus+Alertmanager配置郵件報警

(7)在Prometheus添加告警規則

groups:

- name: node-up

  rules:

  - alert: node-up

    expr: up{job="k8s-node1"} == 0

    for: 15s

    labels:

      severity: 1

      team: node

    annotations:

      summary: "{{ $labels.instance }} 已停止運作超過 15s!"

(8)把告警規則加入Prometheus配置檔案

rule_files:

   - "rules/*_rules.yml"

   - "rules/*_alerts.yml"

(9) 檢視配置規則

Prometheus+Alertmanager配置郵件報警

(10) 測試:停止k8s-node1節點,是否有報警

Prometheus+Alertmanager配置郵件報警
Prometheus+Alertmanager配置郵件報警

提示Pending(1)

Prometheus+Alertmanager配置郵件報警

(11) 檢視報警Alertmanager

Prometheus+Alertmanager配置郵件報警

(12) 檢視郵件報警

Prometheus+Alertmanager配置郵件報警

Prometheus+Grafana+Alertmanager +郵件告警配置完成

↓↓ 點選"閱讀原文" 【加入DevOps運維團】

相關閱讀:

1、兩種方式部署Prometheus

2、部署Prometheus+Grafana

請分享到朋友圈掃碼關注

Prometheus+Alertmanager配置郵件報警