Prometheus 中的规则在收集数据的过程中对数据进行处理,既可以预先计算并存储在时间序列数据库中的值,也可以针对收集的指标中预定义的条件发送警报。 Azure Monitor Prometheus(Managed Prometheus)的托管服务提供了每种类型的规则的预定义集,并支持使用 Azure 门户创建和管理自定义规则。
先决条件
规则组类型
Prometheus 规则组是由告警规则和记录规则组成的集合,这些规则会一并进行评估。 每个规则都必须是单个规则组的成员。 规则组定义组中所有规则的范围及其评估频率。
有两种类型的 Prometheus 规则。
| 类型 |
Description |
| Alert |
通过警报规则 ,可以根据 Prometheus 查询语言(PromQL)查询的结果创建 Azure Monitor 警报。 由托管 Prometheus 警报规则触发的警报,其处理方式和触发通知的方式与其他 Azure Monitor 警报类似。 |
| Recording |
记录规则可预先计算经常需要的或计算量大的表达式,并将其结果存储为一组新时序。 通过记录规则创建的时序将作为新的 Prometheus 指标引入回 Azure Monitor 工作区。 |
托管 Prometheus 规则组遵循 开源 Prometheus 规则组的结构和术语。 Azure支持规则名称、表达式、标签和注释。
托管 Prometheus 规则组不同于开源 Prometheus 规则组。 托管的 Prometheus 规则组被作为 Azure 资源进行管理,并包含资源管理所需的相关信息,例如 Azure 规则组应位于哪个订阅和资源组。 警报规则包括专用属性,例如警报严重性、作组关联和警报自动解决配置,允许像其他 Azure Monitor 警报一样处理警报。
规则组的范围
Managed Prometheus 中规则组的范围定义组中规则适用的资源。 不能将单个规则直接应用于 Kubernetes 群集。 下表描述了不同的规则组范围。
| Scope |
Description |
| 工作区中的所有群集 |
组中启用的所有规则将应用于当前连接到 Azure Monitor 工作区的所有群集。 |
| 特定群集 - 群集名称 |
组中所有启用的规则将仅应用于所选群集。 |
| 特定群集 - 查询中的群集名称 |
组中所有启用的规则都适用于名称中具有指定文本的群集。 |
查看 Prometheus 规则组
可以通过多种方式在 Azure 门户中查看 Prometheus 规则组及其规则。
Azure Monitor工作区中的规则
从Azure门户中的Azure Monitor工作区中选择Rule 组以查看该工作区中的所有规则组。 展开任何规则组以查看该组中的规则列表。 选择任意组或规则以查看其详细信息。
所有规则
在Azure门户的“监视”菜单中的“警报”页中,选择“Prometheus 规则组”以查看你有权访问的订阅中的所有规则组。
此视图显示规则组所在的工作区、规则组是否已启用,以及如果规则组仅限于特定集群范围,相应的集群。 使用屏幕顶部的筛选器按各种属性缩小规则组列表的范围。 若要删除多个规则组,请选择它们,然后选择“ 删除”。 例如,此操作可用于清理删除群集后不再需要的规则组。
小窍门
从 Kubernetes 群集的 “警报 ”页访问此相同视图。 此操作将初始筛选器设置为限定为该群集的规则组。
创建 Prometheus 规则组和规则
打开前面所述的 “所有规则 ”视图,然后选择“ + 创建”。
Scope
| 设置 |
Description |
| Azure Monitor 工作区 |
规则组将从中查询数据的 Azure Monitor 工作区。 无法更改现有规则组的值。 |
| 位置 |
所选 Azure Monitor 工作区的位置。 |
| 集群 |
指定规则组是应用于工作区中的所有群集还是特定群集。 选择特定群集或输入与群集名称匹配的文本。 |
详细信息
| 设置 |
Description |
| Subscription |
在该订阅中将创建规则组资源。 无法更改现有规则组的值。 |
| 资源组 |
将在其中创建规则组资源的资源组。 无法更改现有规则组的值。 |
| Name |
规则组资源的名称。 此名称在所选资源组中必须是唯一的。 无法更改现有规则组的值。 |
| Description |
规则组的说明。 |
| 评估每个 |
组内规则被评估的频率。 默认值为 1 分钟。 |
| 已启用 |
启用或禁用规则组。 仍会创建禁用的规则组,但仅当启用组时,才会运行规则。 |
| 标签 |
规则的可选标签键/值对。 这些标签将添加到规则创建的指标中。 |
Rules
选择 “添加录制规则 ”或 “添加警报规则 ”,将规则添加到组。 每种类型的规则都有不同的设置,如下所示。
记录规则
| 设置 |
Description |
| Name |
录制规则的名称。 此名称用于由规则创建的指标。 |
| 已启用 |
说明规则是启用还是禁用。 将创建禁用的规则,但在启用之前不会评估这些规则。 |
| Expression |
定义规则的 PromQL 表达式。 选择 “运行查询 ”以查看在预览图表中可视化的表达式查询的结果。 修改预览时间范围以放大或缩小表达式结果历史记录。 |
警报规则
| 设置 |
Description |
| Name |
警报规则的名称。 此名称用作该规则触发的警报的名称。 |
| Severity |
此规则触发的警报的严重性值。 |
| Expression |
定义规则的 PromQL 表达式。 选择 “运行查询 ”以查看在预览图表中可视化的表达式查询的结果。 修改预览时间范围以放大或缩小表达式结果历史记录。 |
| 等待 |
警报表达式首次变为 true 到警报触发之间的时间段。 |
| 标签 |
规则的可选标签键/值对。 这些标签会被添加到由规则触发的警报中。 |
| Annotations |
规则的可选批注键/值对。 这些注释将添加到规则触发的警报中。 |
| 行动小组 |
定义响应已触发警报的动作组。 |
| 已启用 |
说明规则是启用还是禁用。 将创建禁用的规则,但在启用之前不会评估这些规则。 |
| 自动解决警报 |
如果规则条件在 自动解析时间 期间不再为真,则自动解决警报。 |
在 “作用域 ”选项卡上:
从订阅中可用的工作区列表中选择 Azure Monitor 工作区。 本组规则会从工作区查询数据。
若要将规则组限制为群集范围,请选择 “特定群集 ”选项:
- 从已连接到所选 Azure Monitor 工作区的群集列表中选择群集。
- 系统会为你输入默认的“群集名称”值。 仅当使用 cluster_alias更改群集标签值时,才更改此值。
选择 “下一步 ”以配置规则组详细信息。
使用 az alerts-management prometheus-rule-group create 命令创建新的 Prometheus 规则组。
az alerts-management prometheus-rule-group create \
--name my-rule-group --resource-group my-resource-group --location chinanorth2 --enabled \
--description "Sample Prometheus resource group" --interval PT10M \
--scopes "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/testrg/providers/microsoft.monitor/accounts/testaccount" \
--rules [{"record":"test","expression":"test","labels":{"team":"prod"}},{"alert":"Billing_Processing_Very_Slow","expression":"test","enabled":"true","severity":2,"for":"PT5M","labels":{"team":"prod"},"annotations":{"annotationName1":"annotationValue1"},"resolveConfiguration":{"autoResolved":"true","timeToResolve":"PT10M"},"actions":[{"actionGroupId":"/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testrg/providers/microsoft.insights/actionGroups/test-action-group-name1","actionProperties":{"key11":"value11","key12":"value12"}},{"actionGroupId":"/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testrg/providers/microsoft.insights/actionGroups/test-action-group-name2","actionProperties":{"key21":"value21","key22":"value22"}}]}]
若要使用 PowerShell 创建 Prometheus 规则组,请使用 new-azprometheusrulegroup cmdlet。
$rule1 = New-AzPrometheusRuleObject -Record "job_type:billing_jobs_duration_seconds:99p5m"
$action = New-AzPrometheusRuleGroupActionObject -ActionGroupId /subscriptions/fffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/MyresourceGroup/providers/microsoft.insights/actiongroups/MyActionGroup -ActionProperty @{"key1" = "value1"}
$Timespan = New-TimeSpan -Minutes 15
$rule2 = New-AzPrometheusRuleObject -Alert Billing_Processing_Very_Slow -Expression "job_type:billing_jobs_duration_seconds:99p5m > 30" -Enabled $false -Severity 3 -For $Timespan -Label @{"team"="prod"} -Annotation @{"annotation" = "value"} -ResolveConfigurationAutoResolved $true -ResolveConfigurationTimeToResolve $Timespan -Action $action
$rules = @($rule1, $rule2)
$scope = "/subscriptions/fffffffff-ffff-ffff-ffff-ffffffffffff/resourcegroups/MyresourceGroup/providers/microsoft.monitor/accounts/MyAccounts"
New-AzPrometheusRuleGroup -ResourceGroupName MyresourceGroup -RuleGroupName MyRuleGroup -Location chinanorth2 -Rule $rules -Scope $scope -Enabled
以下示例模板创建 Prometheus 规则组,包括一个记录规则和一个警报规则。 此模板将创建类型为 Microsoft.AlertsManagement/prometheusRuleGroups 的资源。 此组的范围仅限于单个 AKS 群集。 规则按组内显示的顺序运行。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"name": "sampleRuleGroup",
"type": "Microsoft.AlertsManagement/prometheusRuleGroups",
"apiVersion": "2023-03-01",
"location": "chinanorth2",
"properties": {
"description": "Sample Prometheus Rule Group",
"scopes": [
"/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.monitor/accounts/<azure-monitor-workspace-name>",
"/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.containerservice/managedclusters/<myClusterName>"
],
"enabled": true,
"clusterName": "<myClusterName>",
"interval": "PT1M",
"rules": [
{
"record": "instance:node_cpu_utilisation:rate5m",
"expression": "1 - avg without (cpu) (sum without (mode)(rate(node_cpu_seconds_total{job=\"node\", mode=~\"idle|iowait|steal\"}[5m])))",
"labels": {
"workload_type": "job"
},
"enabled": true
},
{
"alert": "KubeCPUQuotaOvercommit",
"expression": "sum(min without(resource) (kube_resourcequota{job=\"kube-state-metrics\", type=\"hard\", resource=~\"(cpu|requests.cpu)\"})) / sum(kube_node_status_allocatable{resource=\"cpu\", job=\"kube-state-metrics\"}) > 1.5",
"for": "PT5M",
"labels": {
"team": "prod"
},
"annotations": {
"description": "Cluster has overcommitted CPU resource requests for Namespaces.",
"runbook_url": "https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubecpuquotaovercommit",
"summary": "Cluster has overcommitted CPU resource requests."
},
"enabled": true,
"severity": 3,
"resolveConfiguration": {
"autoResolved": true,
"timeToResolve": "PT10M"
},
"actions": [
{
"actionGroupId": "/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.insights/actiongroups/<action-group-name>"
}
]
}
]
}
}
]
}
规则组包含以下属性。
| Name |
Required |
类型 |
Description |
name |
True |
字符串 |
Prometheus 规则组名称。 |
type |
True |
字符串 |
Microsoft.AlertsManagement/prometheusRuleGroups |
apiVersion |
True |
字符串 |
2023-03-01 |
location |
True |
字符串 |
资源位置不受支持的区域。 |
properties.description |
False |
字符串 |
规则组说明。 |
properties.scopes |
True |
string[] |
必须包含目标 Azure Monitor 工作区 ID。 可以选择再包含一个群集 ID。 |
properties.enabled |
False |
布尔 |
启用/禁用该组。 默认值为 true。 |
properties.clusterName |
False |
字符串 |
必须与添加到从目标群集中抓取的指标的 cluster 标签匹配。 默认情况下,设置为显示在 scopes[] 中的群集 ID 的最后部分(资源名称)。 |
properties.interval |
False |
字符串 |
组评估间隔。 默认值 = PT1M。 |
记录规则属性 (ARM 模板)
rules 部分包含以下记录规则属性。
| Name |
Required |
类型 |
Description |
record |
True |
字符串 |
记录规则名。 此名称用于新的时间序列。 |
expression |
True |
字符串 |
用于计算新时序值的 PromQL 表达式。 |
labels |
False |
对象 |
可选的 Prometheus 规则标签键/值对。 这些标签将添加到记录的时序中。 |
enabled |
False |
boolean |
启用/禁用组。 默认值为 true。 |
警报规则属性 (ARM 模板)
rules 部分包含以下警报规则属性。
| Name |
Required |
类型 |
Description |
alert |
False |
字符串 |
警报规则名称。 |
expression |
True |
字符串 |
要计算的 PromQL 表达式。 |
for |
False |
字符串 |
警报触发超时。 值 = PT1M, PT5M等等。 |
labels |
False |
对象 |
Prometheus 警报规则标签。 这些标签将添加到此规则触发的警报中。 |
rules.annotations |
False |
对象 |
要添加到警报的批注键/值对。 |
enabled |
False |
布尔 |
启用/禁用组。 默认值为 true。 |
rules.severity |
False |
整数 |
警报严重性。 0-4,默认值为 3 (信息性)。 |
rules.resolveConfiguration.autoResolved |
False |
布尔 |
启用后,当条件不再为 true 时,会自动解除警报。 默认值 = true。 |
rules.resolveConfiguration.timeToResolve |
False |
字符串 |
警报自动解除超时。 默认值 = PT5M。 |
rules.actions[].actionGroupId |
False |
字符串 |
一个或多个操作组资源 ID。 当警报被触发时,每个都会被激活。 |
以下示例模板创建 Prometheus 规则组,包括一个记录规则和一个警报规则。 此模板将创建类型为 Microsoft.AlertsManagement/prometheusRuleGroups 的资源。 此组的范围仅限于单个 AKS 群集。 规则按组内显示的顺序运行。
resource sampleRuleGroup 'Microsoft.AlertsManagement/prometheusRuleGroups@2023-03-01' = {
name: 'sampleRuleGroup'
location: 'northcentralus'
properties: {
description: 'Sample Prometheus Rule Group'
scopes: [
'/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.monitor/accounts/<azure-monitor-workspace-name>'
'/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.containerservice/managedclusters/<myClusterName>'
]
enabled: true
clusterName: '<myClusterName>'
interval: 'PT1M'
rules: [
{
record: 'instance:node_cpu_utilisation:rate5m'
expression: '1 - avg without (cpu) (sum without (mode)(rate(node_cpu_seconds_total{job="node", mode=~"idle|iowait|steal"}[5m])))'
labels: {
workload_type: 'job'
}
enabled: true
}
{
alert: 'KubeCPUQuotaOvercommit'
expression: 'sum(min without(resource) (kube_resourcequota{job="kube-state-metrics", type="hard", resource=~"(cpu|requests.cpu)"})) / sum(kube_node_status_allocatable{resource="cpu", job="kube-state-metrics"}) > 1.5'
for: 'PT5M'
labels: {
team: 'prod'
}
annotations: {
description: 'Cluster has overcommitted CPU resource requests for Namespaces.'
runbook_url: 'https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubecpuquotaovercommit'
summary: 'Cluster has overcommitted CPU resource requests.'
}
enabled: true
severity: 3
resolveConfiguration: {
autoResolved: true
timeToResolve: 'PT10M'
}
actions: [
{
actionGroupId: '/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.insights/actiongroups/<action-group-name>'
}
]
}
]
}
}
规则组包含以下属性。
| Name |
Required |
类型 |
Description |
name |
True |
字符串 |
Prometheus 规则组名称。 |
type |
True |
字符串 |
Microsoft.AlertsManagement/prometheusRuleGroups |
apiVersion |
True |
字符串 |
2023-03-01 |
location |
True |
字符串 |
资源位置不受支持的区域。 |
properties.description |
False |
字符串 |
规则组说明。 |
properties.scopes |
True |
string[] |
必须包含目标 Azure Monitor 工作区 ID。 可以选择再包含一个群集 ID。 |
properties.enabled |
False |
布尔 |
启用/禁用该组。 默认值为 true。 |
properties.clusterName |
False |
字符串 |
必须与添加到从目标群集中抓取的指标的 cluster 标签匹配。 默认情况下,设置为显示在 scopes[] 中的群集 ID 的最后部分(资源名称)。 |
properties.interval |
False |
字符串 |
组评估间隔。 默认值 = PT1M。 |
记录规则属性(Bicep)
rules 部分包含以下记录规则属性。
| Name |
Required |
类型 |
Description |
record |
True |
字符串 |
记录规则名。 此名称用于新的时间序列。 |
expression |
True |
字符串 |
用于计算新时序值的 PromQL 表达式。 |
labels |
False |
对象 |
可选的 Prometheus 规则标签键/值对。 这些标签将添加到记录的时序中。 |
enabled |
False |
boolean |
启用/禁用组。 默认值为 true。 |
警报规则属性(Bicep)
rules 部分包含以下警报规则属性。
| Name |
Required |
类型 |
Description |
alert |
False |
字符串 |
警报规则名称。 |
expression |
True |
字符串 |
要计算的 PromQL 表达式。 |
for |
False |
字符串 |
警报触发超时。 值 = PT1M, PT5M等等。 |
labels |
False |
对象 |
Prometheus 警报规则标签。 这些标签将添加到此规则触发的警报中。 |
rules.annotations |
False |
对象 |
要添加到警报的批注键/值对。 |
enabled |
False |
布尔 |
启用/禁用组。 默认值为 true。 |
rules.severity |
False |
整数 |
警报严重性。 0-4,默认值为 3 (信息性)。 |
rules.resolveConfiguration.autoResolved |
False |
布尔 |
启用后,当条件不再为 true 时,会自动解除警报。 默认值 = true。 |
rules.resolveConfiguration.timeToResolve |
False |
字符串 |
警报自动解除超时。 默认值 = PT5M。 |
rules.actions[].actionGroupId |
False |
字符串 |
一个或多个操作组资源 ID。 当警报被触发时,每个都会被激活。 |
将 Prometheus 规则文件转换为托管的 Prometheus 规则组
如果有 YAML 格式的 Prometheus 规则配置文件 ,请使用 az-prom-rules-converter 实用工具 将其转换为托管 Prometheus 规则组的 ARM 模板。 规则文件可以包含一个或多个规则组的定义。
除了规则文件,该实用工具还需要创建托管 Prometheus 规则组所需的其他属性,包括订阅、资源组、位置、目标Azure Monitor工作区、目标群集 ID 和名称和操作组。 该实用工具使用用于部署 ARM 模板的任何标准方法创建一个模板文件。
将规则限制为特定群集
(可选)通过将群集范围添加到规则组或使用规则组 clusterName 属性,将规则组中的规则限制为查询源自单个特定群集的数据。
如果 Azure Monitor 工作区包含来自多个群集的大量数据,请将规则限制为单个群集。 在这种情况下,对所有数据运行同一组规则可能会导致性能问题或限流问题。 使用群集范围创建多个规则组,每个组都配置了相同的规则,每个组涵盖不同的群集。
若要 使用 ARM 模板将规则组限制为群集范围,请将群集的 Azure 资源 ID 值添加到规则组 scopes[] 列表中。 范围列表仍必须包含 Azure Monitor 工作区资源 ID。 支持将以下群集资源类型作为群集范围:
- Azure Kubernetes 服务群集 (
Microsoft.ContainerService/managedClusters)
- 已启用 Azure Arc 的 Kubernetes 群集 (
Microsoft.kubernetes/connectedClusters)
- Azure 连接设备(
Microsoft.ResourceConnector/appliances)
除了群集 ID 之外,还配置 clusterName 规则组的属性。 该 clusterName 属性必须与从特定群集中擦除时添加到指标的 cluster 标签匹配。 默认情况下,此标签设置为群集 ID 的最后一部分(即资源名称)。 如果使用群集擦除 ConfigMap 中的 cluster_alias 设置更改了此标签,则必须在规则组 clusterName 属性中包含更新的值。 如果擦除使用默认 cluster 标签值,则 clusterName 属性是可选的。
以下示例展示了如何将规则组配置为限制查询特定群集:
{
"name": "sampleRuleGroup",
"type": "Microsoft.AlertsManagement/prometheusRuleGroups",
"apiVersion": "2023-03-01",
"location": "chinanorth2",
"properties": {
"description": "Sample Prometheus Rule Group limited to a specific cluster",
"scopes": [
"/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.monitor/accounts/<azure-monitor-workspace-name>",
"/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/microsoft.containerservice/managedclusters/<myClusterName>"
],
"clusterName": "<myClusterName>",
"rules": [
{
...
}
]
}
}
如果未为规则组指定群集 ID 范围和 clusterName 属性,则组中的规则将从工作区中的所有群集查询数据。
Note
对于警报规则,表达式查询通常仅返回满足表达式条件的时序。 如果未显示预览图表,并且收到消息“查询未返回结果”,则可能是在预览时间范围内未满足条件。
完成创建规则组
在“标记”选项卡上,设置要添加到规则组资源的任何所需的 Azure 资源标记。
在“ 查看 + 创建 ”选项卡上,门户会验证规则组,并告知你任何问题。 在此选项卡上,选择 “查看自动化模板 ”选项,并下载要创建的组的模板。
验证通过并查看设置后,选择“ 创建”。
跟踪规则组部署,以确保其顺利完成,或在发生任何错误时收到通知。
禁用和启用规则组
若要启用或禁用规则,请在 Azure 门户中选择规则组。 可选择“启用”或“禁用”来更改其状态。
相关内容