5

我在集群中设置了 kube-prometheus(https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus)。它包含一些默认警报,例如“CoreDNSdown 等”。如何创建自己的警报?

任何人都可以为我提供示例示例来创建将向我的 gmail 帐户发送电子邮件的警报吗?

当 docker 容器 pod 处于 Error 或 CarshLoopBackOff kubernetes 时,我遵循了此警报。但我无法让它工作。

4

2 回答 2

5

要将警报发送到您的 gmail 帐户,您需要在文件中设置 alertmanager 配置,例如 alertmanager.yaml:

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-prometheus 时,您将拥有一个名为的秘密alertmanager-main,它是alertmanager. alertmanager-main您需要使用以下命令使用新配置再次创建密钥:

kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring

现在你的 alertmanager 设置为在收到来自 prometheus 的警报时发送电子邮件。

现在您需要设置一个警报,您的邮件将在该警报上发送。您可以设置在每种情况下都会触发的 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警报将被触发,并应向您的邮箱发送电子邮件。

参考链接:

https://coreos.com/tectonic/docs/latest/tectonic-prometheus-operator/user-guides/configuring-prometheus-alertmanager.html

编辑:

deadmanswitch 警报应该放在 prometheus 正在读取的配置映射中。我将在这里分享我的普罗米修斯的相关快照:

"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 文件,它具有要使用的 alertmanager 的名称,它将ruleSelector根据prometheusrole标签选择规则。所以我有我的规则配置图,例如:

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上面的配置映射。

于 2018-11-30T13:30:41.157 回答
0

如果您使用的是 kube-promehtheus,默认情况下它具有 alertmanager-main secrete 和 prometheus kind 设置。

第 1 步:您必须删除 alertmanager-main secret

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

注意:角色应该是角色:alert-rules,在规则选择器 prometheus 种类中指定,检查使用

kubectl get prometheus k8s -n monitoring -o yaml
于 2018-12-02T16:23:21.237 回答