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