一、PrometheusRule是什么¶
PrometheusRule 是 Prometheus Operator 中的一个自定义资源(Custom Resource),它用于定义 Prometheus 在监控系统中的规则和警报规则。
Prometheus 是一种开源的监控系统,用于收集和存储时间序列数据,并提供丰富的查询语言(PromQL)用于对数据进行查询和分析。Prometheus 可以通过配置规则来定义警报条件,当满足某些预定义的条件时,将会触发警报通知。
在 Kubernetes 集群中,可以使用 Prometheus Operator 来简化 Prometheus 的管理和配置。Prometheus Operator 允许你通过 Kubernetes 的自定义资源来定义 Prometheus 的配置,其中就包括 PrometheusRule。
PrometheusRule 允许你在 Kubernetes 中定义 Prometheus 的规则和警报规则。每个 PrometheusRule 对象包含一个或多个规则组(RuleGroup),而每个规则组包含一组相关的规则。每个规则都定义了一个 PromQL 查询和相应的警报条件。
通过使用 PrometheusRule,你可以将 Prometheus 监控系统的配置与 Kubernetes 集群的其他部分进行解耦,使得配置和管理变得更加简单和灵活。
二、PrometheusRule配置文件解析¶
1.查看默认配置的告警策略
[root@k8s-master01 manifests]# kubectl get prometheusrule -n monitoring
NAME AGE
alertmanager-main-rules 3h2m
grafana-rules 3h2m
kube-prometheus-rules 3h2m
kube-state-metrics-rules 3h2m
kubernetes-monitoring-rules 3h2m
node-exporter-rules 3h2m
prometheus-k8s-prometheus-rules 3h2m
prometheus-operator-rules 3h2m
2.通过-oyaml 可以查看某个 rules 的详细配置,下面以node-exporter-rules作为配置文件解析
[root@k8s-master01 ~]# kubectl get prometheusrule -n monitoring node-exporter-rules -oyaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
...
...
spec:
groups:
- name: node-exporter
rules:
- alert: NodeFilesystemSpaceFillingUp #告警策略的名称
annotations: #告警注释信息,一般写为告警信息
description: Filesystem on {{ $labels.device }} at {{ $labels.instance }}
has only {{ printf "%.2f" $value }}% available space left and is filling
up.
runbook_url: https://runbooks.prometheus-operator.dev/runbooks/node/nodefilesystemspacefillingup
summary: Filesystem is predicted to run out of space within the next 24 hours.
expr: | #告警表达式
(
node_filesystem_avail_bytes{job="node-exporter",fstype!=""} / node_filesystem_size_bytes{job="node-exporter",fstype!=""} * 100 < 15
and
predict_linear(node_filesystem_avail_bytes{job="node-exporter",fstype!=""}[6h], 24*60*60) < 0
and
node_filesystem_readonly{job="node-exporter",fstype!=""} == 0
)
for: 1h #评估等待时间,告警持续多久才会发送告警数据
labels: #告警的标签,用于告警的路由
severity: warning
...
...
说明:Operator安装的Prometheus中的PrometheusRule和传统配置的Prometheus中的PrometheusRule并无区别,只是Operator是通过kind为PrometheusRule进行单独管理的
上面文件参数说明:
- groups:这个字段是一个规则组(RuleGroup),它可以包含一个或多个相关的告警规则。
- name: 这里定义了规则组的名称为 "node-exporter",用于标识这些规则是与节点导出器相关的。
- rules: 这里是规则组包含的规则列表
- alert:告警策略的名称
- annotations:告警注释信息,一般写为告警信息
- expr: 这是一个 PromQL 表达式,用于定义告警的触发条件。在这个示例中,它检查文件系统的可用空间是否小于 15%,同时使用线性预测检查文件系统是否在接下来的 24 小时内耗尽空间。还检查文件系统是否为只读
- for:评估等待时间,告警持续多久才会发送告警数据
- labels:告警的标签,用于告警的路由