容器见解中的数据转换
本文描述了如何在容器见解中实现数据转换。 Azure Monitor 中的转换允许在数据进入 Log Analytics 工作区之前修改或筛选数据。 它们允许执行诸如筛选从群集收集的数据以节省成本或处理传入数据以帮助进行数据查询之类的操作。
数据收集规则 (DCR)
转换在数据收集规则 (DCR) 中实现,用于在 Azure Monitor 中配置数据收集。 加入群集的容器见解时,会为其创建 DCR,名称为 MSCI-<cluster-region>-<\cluster-name>。 可以从 Azure 门户的“监视器”菜单中的“数据收集规则”中查看此 DCR。 若要创建转换,必须修改此 DCR,或者使用包含转换的自定义 DCR 加入群集。
下表描述了编辑 DCR 的不同方法,而本文的其余部分提供了转换容器见解数据所需执行的编辑详细信息。
方法 | 说明 |
---|---|
新建群集 | 使用现有 ARM 模板将 AKS 群集加入到容器见解。 修改模板中 DCR 的 dataFlows 部分以包含转换,类似于下面的示例之一。 |
现有 DCR | 群集加入容器见解后,使用编辑数据收集规则中的进程编辑其 DCR 以包含转换。 |
数据源
DCR 的 dataSources 部分定义了 DCR 将处理的不同类型的传入数据。 对于容器见解,这包括 ContainerInsights
扩展,该扩展包括一个或多个以前缀 Microsoft- 开头的预定义 streams
。
DCR 中容器见解流的列表取决于为群集选择的成本预设。 如果你收集所有表,则 DCR 将使用 Microsoft-ContainerInsights-Group-Default
流,该流是包含 Stream 值中列出的所有流的组流。 如果要使用转换,则必须将其更改为单独的流。 任何其他成本预设设置都将使用单独的流。
下面的片段显示了 Microsoft-ContainerInsights-Group-Default
流。 有关单个流的示例,请参阅 DCR 示例。
"dataSources": {
"extensions": [
{
"name": "ContainerInsightsExtension",
"extensionName": "ContainerInsights",
"extensionSettings": { },
"streams": [
"Microsoft-ContainerInsights-Group-Default"
]
}
]
}
数据流
DCR 的 dataFlows 部分将流与目标进行匹配。 不需要转换的流可以组合在一个条目中,该条目只包括工作区目标。 为需要转换(包括工作区目标和 transformKql
属性)的流创建单独条目。
下面的片段显示了带有转换的单一流的 dataFlows
部分。 有关单一 DCR 中的多个数据流,请参阅示例 DCR。
"dataFlows": [
{
"streams": [
"Microsoft-ContainerLogV2"
],
"destinations": [
"ciworkspace"
],
"transformKql": "source | where Namespace == 'kube-system'"
}
]
示例 DCR
以下示例显示了使用转换的容器见解的 DCR。 以这些示例为起点,然后根据需要进行自定义,以满足特定要求。
特定命名空间的筛选器
此示例使用日志查询 source | where Namespace == 'kube-system'
来收集 ContainerLogsV2
中单一命名空间的数据。 可以将此查询中的 kube-system
替换为另一个命名空间,或者将 where
子句替换为其他筛选器,以匹配要收集的特定数据。 其他流分组到单独的数据流中,并且没有应用任何转换。
{
"properties": {
"dataSources": {
"syslog": [],
"extensions": [
{
"name": "ContainerInsightsExtension",
"extensionName": "ContainerInsights",
"extensionSettings": { },
"streams": [
"Microsoft-ContainerLog",
"Microsoft-ContainerLogV2",
"Microsoft-KubeEvents",
"Microsoft-KubePodInventory",
"Microsoft-KubeNodeInventory",
"Microsoft-KubePVInventory",
"Microsoft-KubeServices",
"Microsoft-KubeMonAgentEvents",
"Microsoft-InsightsMetrics",
"Microsoft-ContainerInventory",
"Microsoft-ContainerNodeInventory",
"Microsoft-Perf"
]
}
]
},
"destinations": {
"logAnalytics": [
{
"workspaceResourceId": "",
"workspaceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace",
"name": "ciworkspace"
}
]
},
"dataFlows": [
{
"streams": [
"Microsoft-ContainerLog",
"Microsoft-KubeEvents",
"Microsoft-KubePodInventory",
"Microsoft-KubeNodeInventory",
"Microsoft-KubePVInventory",
"Microsoft-KubeServices",
"Microsoft-KubeMonAgentEvents",
"Microsoft-InsightsMetrics",
"Microsoft-ContainerNodeInventory",
"Microsoft-Perf"
],
"destinations": [
"ciworkspace"
]
},
{
"streams": [
"Microsoft-ContainerLogV2"
],
"destinations": [
"ciworkspace"
],
"transformKql": "source | where Namespace == 'kube-system'"
}
]
}
}
将列添加到表
此示例使用日志查询 source | extend new_CF = ContainerName
将数据发送到添加到 ContainerLogV2
表中的自定义列。 此转换要求使用添加或删除自定义列中所述的过程将自定义列添加到表中。 其他流分组到单独的数据流中,并且没有应用任何转换。
{
"properties": {
"dataSources": {
"syslog": [],
"extensions": [
{
"extensionName": "ContainerInsights",
"extensionSettings": { },
"name": "ContainerInsightsExtension",
"streams": [
"Microsoft-ContainerLog",
"Microsoft-ContainerLogV2",
"Microsoft-KubeEvents",
"Microsoft-KubePodInventory",
"Microsoft-KubeNodeInventory",
"Microsoft-KubePVInventory",
"Microsoft-KubeServices",
"Microsoft-KubeMonAgentEvents",
"Microsoft-InsightsMetrics",
"Microsoft-ContainerInventory",
"Microsoft-ContainerNodeInventory",
"Microsoft-Perf"
]
}
]
},
"destinations": {
"logAnalytics": [
{
"workspaceResourceId": "",
"workspaceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-workspace",
"name": "ciworkspace"
}
]
},
"dataFlows": [
{
"streams": [
"Microsoft-ContainerLog",
"Microsoft-KubeEvents",
"Microsoft-KubePodInventory",
"Microsoft-KubeNodeInventory",
"Microsoft-KubePVInventory",
"Microsoft-KubeServices",
"Microsoft-KubeMonAgentEvents",
"Microsoft-InsightsMetrics",
"Microsoft-ContainerNodeInventory",
"Microsoft-Perf"
],
"destinations": [
"ciworkspace"
]
},
{
"streams": [
"Microsoft-ContainerLogV2"
],
"destinations": [
"ciworkspace"
],
"transformKql": "source\n | extend new_CF = ContainerName"
}
]
}
}