[英]How to trigger alert in Kubernetes using Prometheus Alert Manager
我在集群中安裝了kube-prometheus( https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus )。 它包含一些默認警報,例如“ CoreDNSdown等”。 如何創建我自己的警報?
有人可以提供示例示例來創建警報,該警報將向我的gmail帳戶發送電子郵件嗎?
當Docker容器容器位於Error或CarshLoopBackOff kubernetes中時,我遵循此警報 。 但是我無法使它工作。
要將警報發送到您的gmail帳戶,您需要在一個名為alertmanager.yaml的文件中設置alertmanager配置:
cat <<EOF > alertmanager.yml
route:
group_by: [Alertname]
# Send all notifications to me.
receiver: email-me
receivers:
- name: email-me
email_configs:
- to: $GMAIL_ACCOUNT
from: $GMAIL_ACCOUNT
smarthost: smtp.gmail.com:587
auth_username: "$GMAIL_ACCOUNT"
auth_identity: "$GMAIL_ACCOUNT"
auth_password: "$GMAIL_AUTH_TOKEN"
EOF
現在,當你使用KUBE-普羅米修斯所以你將有一個名為秘密alertmanager-main
是對默認配置alertmanager
。 您需要使用以下命令使用新配置再次創建一個secret alertmanager-main
:
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring
現在,您可以將alertmanager設置為在收到來自普羅米修斯的警報時發送電子郵件。
現在,您需要設置一個警報,以發送郵件。 您可以設置DeadManSwitch警報,該警報在每種情況下都會觸發,並用於檢查警報管道
groups:
- name: meta
rules:
- alert: DeadMansSwitch
expr: vector(1)
labels:
severity: critical
annotations:
description: This is a DeadMansSwitch meant to ensure that the entire Alerting
pipeline is functional.
summary: Alerting DeadMansSwitch
之后,將觸發DeadManSwitch
警報,並應將電子郵件發送到您的郵件中。
參考鏈接:
編輯:
Deadmanswitch警報應進入您的普羅米修斯正在讀取的配置映射中。 我將在這里分享我的普羅米修斯的相關快照:
"spec": {
"alerting": {
"alertmanagers": [
{
"name": "alertmanager-main",
"namespace": "monitoring",
"port": "web"
}
]
},
"baseImage": "quay.io/prometheus/prometheus",
"replicas": 2,
"resources": {
"requests": {
"memory": "400Mi"
}
},
"ruleSelector": {
"matchLabels": {
"prometheus": "prafull",
"role": "alert-rules"
}
},
上面的配置是我的prometheus.json文件的名稱,該文件具有要使用的ruleSelector
的名稱,以及ruleSelector
,它將基於prometheus
和role
標簽選擇規則。 所以我的規則配置映射如下:
kind: ConfigMap
apiVersion: v1
metadata:
name: prometheus-rules
namespace: monitoring
labels:
role: alert-rules
prometheus: prafull
data:
alert-rules.yaml: |+
groups:
- name: alerting_rules
rules:
- alert: LoadAverage15m
expr: node_load15 >= 0.50
labels:
severity: major
annotations:
summary: "Instance {{ $labels.instance }} - high load average"
description: "{{ $labels.instance }} (measured by {{ $labels.job }}) has high load average ({{ $value }}) over 15 minutes."
在上面的配置圖中替換DeadManSwitch
。
如果您使用的是kube-promehtheus,則默認情況下它具有alertmanager-main secret和prometheus kind設置。
步驟1:您必須刪除alertmanager-main機密
kubectl delete secret alertmanager-main -n monitoring
第2步:如Praful所述,用新的變化創造秘密
cat <<EOF > alertmanager.yaml
route:
group_by: [Alertname]
# Send all notifications to me.
receiver: email-me
receivers:
- name: email-me
email_configs:
- to: $GMAIL_ACCOUNT
from: $GMAIL_ACCOUNT
smarthost: smtp.gmail.com:587
auth_username: "$GMAIL_ACCOUNT"
auth_identity: "$GMAIL_ACCOUNT"
auth_password: "$GMAIL_AUTH_TOKEN"
EOF
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring
第三步:您必須添加新的普羅米修斯規則
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
creationTimestamp: null
labels:
prometheus: k8s
role: alert-rules
name: prometheus-podfail-rules
spec:
groups:
- name: ./podfail.rules
rules:
- alert: PodFailAlert
expr: sum(kube_pod_container_status_restarts_total{container="ffmpeggpu"}) BY (container) > 10
注意:角色應該是角色:在規則選擇器prometheus類型中指定的alert-rules,要檢查使用情況
kubectl get prometheus k8s -n monitoring -o yaml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.