当前位置: 代码迷 >> 综合 >> Prometheus 使用PrometheusRule定义告警规则
  详细解决方案

Prometheus 使用PrometheusRule定义告警规则

热度:68   发布时间:2023-09-30 10:30:13.0

在原生的方式上面需要手动创建普罗米修斯告警文件,并且在普罗米修斯当中声明式的加载。

在Prometheus operator当中,告警规则也变成了一个通过api声明方式创建了一个资源。 

[root@master prometheus-operator]# cat example-rule.yaml 
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:labels:prometheus: examplerole: alert-rulesname: prometheus-example-rules
spec:groups:- name: ./example.rulesrules:- alert: ExampleAlertexpr: vector(1)
[root@master prometheus-operator]# kubectl apply -f example-rule.yaml -n monitoring
prometheusrule.monitoring.coreos.com/prometheus-example-rules created
[root@master prometheus-operator]# kubectl get PrometheusRule -n monitoring --show-labels
NAME                       AGE     LABELS
prometheus-example-rules   2m25s   prometheus=example,role=alert-rules

创建完告警规则之后,还需要去普罗米修斯里面去关联一下告警规则

[root@master prometheus-operator]# cat prometheus-inst.yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:name: k8snamespace: monitoring
spec:serviceAccountName: prometheusserviceMonitorSelector:matchLabels:team: frontendruleSelector:matchLabels:role: alert-rulesprometheus: examplealerting:alertmanagers:- name: alertmanager-examplenamespace: monitoringport: webresources:requests:memory: 400Mi

Prometheus 使用PrometheusRule定义告警规则

Prometheus 使用PrometheusRule定义告警规则