Operator是基於Kubernetes的資源和控制器之上構建的概念,用來擴展 Kubernetes API,特定的應用程序控制器,目前官方提供了幾種Operator的實現,而其中Prometheus Operator應用最為方便。核心原理是通過ServiceMonitor中間層,可以自定義資源管理。具體原理和安裝方式可以通過GitHub操作。本文主要討論使用Prometheus Operator郵件報警中容易出錯的細節。
1.Prometheus Operator發送郵件報警的原理:通過自定義PrometheusRule方式添加rule規則,然后通過修改alertmanager-main中的參數項匹配對於的rule,最終進行郵件報警功能。
2.自定義的rule文件都會在prometheus-k8s的pod中/etc/prometheus/rules/prometheus-k8s路徑自動生成配置文件進行調用,並且也可以在prometheus的Dashboard中的Alert頁面查詢到。
3.最核心的yaml文件就是安裝Prometheus Operator時提供的alertmanager-main這個service,會直接調用alertmanager-secret.yaml文件,所以更新alertmanager-secret.yaml就可以自定義發送的監控的對象和郵件。
4.alertmanager-secret.yaml本身是base64加密后的數據,可以通過
echo "XXX" | base64 -d
查看下具體的內容,我們能操作的內容是刪除原有的secret對象,然后讓alertmanager-main重新調用新的secret對象,alertmanager就是我們自定義的yaml文件。
kubectl delete secret alertmanager-main -n monitoring kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring
5.注意smtp_require_tls需要填寫false,不然發送不了郵件
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.exmail.qq.com:465'
smtp_from: 'leozhou@leozhou.com'
smtp_auth_username: 'leozhou@leozhou.com'
smtp_auth_password: 'XXX'
smtp_hello: 'leozhou.com'
smtp_require_tls: false
route:
group_by: ['job', 'severity']
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: default
routes:
- match:
alertname: CPUThrottlingHigh#alertname就是可以匹配到自定義rule中alertname
receiver: default
receivers:
- name: default
email_configs:
- to: 'leozhou@qq.com'
send_resolved: true

