从 Kubernetes 群集启用 Prometheus 指标收集时,数据将发送到 Azure Monitor 工作区。 在某些情况下,你可能希望将不同的指标集发送到不同的 Azure Monitor 工作区。 例如,你可能有不同的团队负责在同一群集中运行的不同应用程序,并且每个团队都需要自己的 Azure Monitor 工作区。
概述
可以使用群集的 ConfigMap 和数据收集规则(DCR)组合来实现此功能。 在 ConfigMap 中创建抓取配置,用于定义每个需要收集的指标集合,并在每个配置中添加一个标签以唯一标识该集合。 然后创建一个 DCR,该 DCR 接受特定标签的数据,并将该数据路由到相应的 Azure Monitor 工作区。 下图对此策略进行了说明,本文其余部分对此进行了详细介绍。
注释
本文使用 ARM 模板创建所需的 Azure Monitor 对象,但可以使用任何其他有效方法,例如 CLI 或 PowerShell。
在 ConfigMap 中创建抓取配置
首先,定义要从群集收集的不同指标集。 使用 ama-metrics-prometheus-config 处的 ConfigMap 模板,并为您想要收集的每组指标添加一个抓取配置作业。 向每个抓取配置添加一个具有预定义标签 microsoft_metrics_account 的 relabel_configs 部分,以便提供唯一标识符。 每个 DCR 都使用此值来标识要路由的数据。
下面的示例展示了用值 MonitoringAccountLabel2 标识的抓取配置。
relabel_configs:
- target_label: microsoft_metrics_account
action: replace
replacement: "MonitoringAccountLabel2"
创建 DCR
定义 ConfigMaps 后,需要为每个抓取配置创建 DCR。每个 DCR 都将查找在 ConfigMap 中定义的标签,并将数据路由到相应的 Azure Monitor 工作区。 有多种方法可以编辑和创建 DCR,如 Azure Monitor 中的“创建数据收集规则”(DCR)中所述。 首先,可以编辑载入群集时创建的 DCR,然后将其用作其他人的模板。
若要标识要路由的数据,请使用 labelIncludeFilter DCR 节中的 prometheusForwarder 属性。 以下示例显示了一个使用标签 MonitoringAccountLabel1路由数据的 DCR。
"prometheusForwarder": [
{
"streams": [
"Microsoft-PrometheusMetrics"
],
"labelIncludeFilter": {
"microsoft_metrics_include_label": "MonitoringAccountLabel1"
},
"name": "PrometheusDataSource"
}
]
将 DCR 与群集相关联
创建新的 DCR 后,需要使用 Azure Monitor 中“管理数据收集规则关联”中所述的任何方法与群集相关联
示例:
请考虑一种方案,即要为在同一群集中运行的两个不同的应用程序分隔数据。 应用程序使用“MonitoringAccountLabel1”和“MonitoringAccountLabel2”进行标识。
抓取配置
以下 yaml 定义了两个不同的作业,每个作业从不同的应用程序收集指标。
每个作业应包含的唯一指标由source_labels和regex中的值标识,而replacement中的值则是向DCR标识作业的唯一标签。
scrape_configs:
- job_name: prometheus_ref_app_1
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
action: keep
regex: "prometheus-reference-app-1"
- target_label: microsoft_metrics_account
action: replace
replacement: "MonitoringAccountLabel1"
- job_name: prometheus_ref_app_2
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
action: keep
regex: "prometheus-reference-app-2"
- target_label: microsoft_metrics_account
action: replace
replacement: "MonitoringAccountLabel2"
DCRs
第一个 DCR 是载入群集时创建的 DCR 的编辑版本。 唯一的更改是添加应用程序 1 的标签筛选器。 由于 Log Analytics 工作区位于同一区域,因此不需要单独的数据收集终结点,并且两个 DCR 中使用相同的终结点。
DCR 1
{
"properties": {
"dataCollectionEndpointId": "/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-endpoint",
"dataSources": {
"prometheusForwarder": [
{
"streams": [
"Microsoft-PrometheusMetrics"
],
"labelIncludeFilter": {
"microsoft_metrics_include_label": "MonitoringAccountLabel1"
},
"name": "PrometheusDataSource"
}
]
},
"destinations": {
"monitoringAccounts": [
{
"accountResourceId": "/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-workspace-01",
"name": "MonitoringAccount"
}
]
},
"dataFlows": [
{
"streams": [
"Microsoft-PrometheusMetrics"
],
"destinations": [
"MonitoringAccount"
]
}
]
}
}
第二个 DCR 是第一个的复制品,但做出了两个更改。 标签筛选器将更新为与应用程序 2 匹配,目标工作区更改为其他工作区。
DCR 2
**DCR 1**
```json
{
"properties": {
"dataCollectionEndpointId": "/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-endpoint",
"dataSources": {
"prometheusForwarder": [
{
"streams": [
"Microsoft-PrometheusMetrics"
],
"labelIncludeFilter": {
"microsoft_metrics_include_label": "MonitoringAccountLabel2"
},
"name": "PrometheusDataSource"
}
]
},
"destinations": {
"monitoringAccounts": [
{
"accountResourceId": "/subscriptions/my-subscription/resourceGroups/my-resource-group/providers/Microsoft.Insights/dataCollectionEndpoints/my-workspace-02",
"name": "MonitoringAccount"
}
]
},
"dataFlows": [
{
"streams": [
"Microsoft-PrometheusMetrics"
],
"destinations": [
"MonitoringAccount"
]
}
]
}
}