0%

prometheus-operator使用【自定义PrometheusRule】

PrometheusRule介绍

PrometheusRule是安装prometheus-operator时默认安装的自定义资源对象(CRD),用来管理Prometheus上的告警规则,后面增删改查规则都可以通过这个资源对象查询。

例如查询默认加入的规则,通过下面的命令可以查询。

[root@k8s-master prometheus-operator]# kubectl get PrometheusRule -n monitoring
NAME                                                       AGE
prometheus-operator-alertmanager.rules                     28m
prometheus-operator-etcd                                   28m
prometheus-operator-general.rules                          28m
prometheus-operator-k8s.rules                              28m
prometheus-operator-kube-apiserver.rules                   28m
prometheus-operator-kube-prometheus-node-recording.rules   28m
prometheus-operator-kube-scheduler.rules                   28m
prometheus-operator-kubernetes-absent                      28m
prometheus-operator-kubernetes-apps                        28m
prometheus-operator-kubernetes-resources                   28m
prometheus-operator-kubernetes-storage                     28m
prometheus-operator-kubernetes-system                      28m
prometheus-operator-kubernetes-system-apiserver            28m
prometheus-operator-kubernetes-system-controller-manager   28m
prometheus-operator-kubernetes-system-kubelet              28m
prometheus-operator-kubernetes-system-scheduler            28m
prometheus-operator-node-exporter                          28m
prometheus-operator-node-exporter.rules                    28m
prometheus-operator-node-network                           28m
prometheus-operator-node-time                              28m
prometheus-operator-node.rules                             28m
prometheus-operator-prometheus                             28m
prometheus-operator-prometheus-operator                    28m

也可以在Prometheus -> Dashboard -> Status -> Rules中查看
prometheusRules

Prometheus怎么识别这个资源对象

简单来说,类似标签选择器,定义的PrometheusRule资源对象,需要带有一些Labels,具体哪些可以参考默认生成的PrometheusRule,然后新建的也给加上。

prometheusRules

参考链接:https://www.qikqiak.com/post/prometheus-operator-custom-alert/

所有的Rules都有对应的文件,默认生成在prometheus容器内

/etc/prometheus/rules/prometheus-prometheus-operator-prometheus-rulefiles-0/

目录下,新增一个PrometheusRule资源,也会在该目录下自动生成一个YAML文件

因此我们可以不用管理配置文件,只需要管理PrometheusRuleprometheus-operator使得prometheus监控更加K8s.

创建新的PrometheusRule资源

myrule.yaml

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: myrule
  labels:
    app: prometheus-operator
    chart: prometheus-operator-8.2.4
    heritage: Tiller
    release: prometheus-operator
spec:
  groups:
  - name: my-node-time
    rules:
    - alert: myClockSkewDetected
      annotations:
        message: myClock skew detected on node-exporter {{`{{ $labels.namespace }}`}}/{{`{{ $labels.pod }}`}}. Ensure NTP is configured correctly on this host.
      expr: abs(node_timex_offset_seconds{job="node-exporter"}) > 0.03
      for: 2m
      labels:
        severity: warning

注意:上面配置中具体的规则只是拷贝了一个默认规则中的内容。

使用命令或者在dashboard中贴上以上YAML,即可创建。

kubectl create -f myrule.yaml

创建完之后,可以通过命令查看

kubectl get PrometheusRule -n monitoring

也可以在Prometheus -> Dashboard中查看,最后在Prometheus容器Rule目录下确认是否新生成一个myrule.yaml的配置文件。