Share via


容器深入解析中的資料轉換

本文說明如何在容器深入解析中實作資料轉換。 Azure 監視器中的轉換可讓您在資料擷取到 Log Analytics 工作區之前修改或篩選資料。 這可讓您執行的動作包含篩選從叢集收集的資料來節省成本,或處理傳入資料以協助資料查詢。

資料收集規則 (DCR)

轉換會在資料收集規則 (DCR) 中實作,此規則用來在 Azure 監視器中設定資料收集。 當您將叢集的容器深入解析上線時,系統就會為其建立 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 串流,這是一個群組串流,其中包含串流值中列出的所有串流。 如果您要使用轉換,您必須將此變更為個別串流。 任何其他成本預設設定都會使用個別串流。

下列程式碼片段顯示 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 PodNamespace == 'kube-system'"
    }
]

範例 DCR

下列範例顯示使用轉換的容器深入解析 DCR。 請使用這些範例作為起點,然後視需要進行自訂,以符合您的特定需求。

篩選特定命名空間

此範例會使用記錄查詢 source | where PodNamespace == '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": "/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 PodNamespace == '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": "/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"
            }
        ]
    }
}

下一步