2.6. Creating alerting rules
You can create alerting rules, which will fire alerts based on values of chosen metrics.
Viewing and managing your rules and alerts is not yet integrated into the web console. A cluster administrator can instead use the Alertmanager UI or the Thanos Ruler. See the respective sections for instructions.
Prerequisites
-
Log in as a user that has the
monitoring-rules-edit
role for the namespace where you want to create the alerting rule.
Procedure
-
Create a YAML file for alerting rules. In this example, it is called
example-app-alerting-rule.yaml
. Fill the file with the configuration for the alerting rules:
注意When you create an alerting rule, a namespace label is enforced on it if a rule with the same name exists in another namespace.
apiVersion: monitoring.coreos.com/v1 kind: PrometheusRule metadata: name: example-alert namespace: ns1 spec: groups: - name: example rules: - alert: VersionAlert expr: version{job="prometheus-example-app"} == 0
This configuration creates an alerting rule named
example-alert
, which fires an alert when theversion
metric exposed by the sample service becomes0
.重要For every namespace, you can use metrics of that namespace and cluster metrics, but not metrics of another namespace.
For example, an alerting rule for
ns1
can have metrics fromns1
and cluster metrics, such as the CPU and memory metrics. However, the rule cannot include metrics fromns2
.Additionally, you cannot create alerting rules for the
openshift-*
core OpenShift namespaces. OpenShift Container Platform Monitoring by default provides a set of alerting rules for these namespaces.Apply the configuration file to the cluster:
$ oc apply -f example-app-alerting-rule.yaml
It will take some time to create the alerting rules.