【问题标题】:Find logs of POD in AKS using Log Analytics Query使用 Log Analytics 查询在 AKS 中查找 POD 日志
【发布时间】:2021-03-11 09:57:37
【问题描述】:

有一个正在运行的 AKS 连接到 Azure 中的 Log Analytics。 我正在尝试使用以下查询 sn-p 查看命名 POD 的日志:

let KubePodLogs = (clustername:string, podnameprefix:string) {
    let ContainerIdList = KubePodInventory
    | where ClusterName =~ clustername
    | where Name startswith strcat(podnameprefix, "-")
    | where strlen(ContainerID)>0
    | distinct ContainerID;
    ContainerLog
    | where ContainerID in (ContainerIdList)
    | join (KubePodInventory | project ContainerID, Name, PodLabel, Namespace, Computer) on ContainerID
    | project TimeGenerated, Node=Computer, Namespace, PodName=Name1, PodLabel, ContainerID, LogEntry
};
KubePodLogs('aks-my-cluster', 'my-service') | order by TimeGenerated desc

上述查询确实返回了匹配 POD 的行,但并非所有实际可用的行。

尝试通过检查 POD 详细信息来获取部分查询的结果:

KubePodInventory
    | where ClusterName =~ 'aks-my-cluster'
    | where Name startswith 'my-service-'
    | where strlen(ContainerID)>0
    | distinct ContainerID;

给我一​​个容器 ID。现在将此容器 ID 输入另一个查询显示更多 结果然后是上面的组合查询。为什么?

ContainerLog 
| where ContainerID == "aec001...fc31"
| order by TimeGenerated desc 
| project TimeGenerated, ContainerID, LogEntry

我注意到的一件事是,后面的简单查询结果包含从 POD 的 JSON 格式输出解析的 LogEntry 字段的日志结果。在结果中,我可以将 LogEntry 扩展为与该 POD 日志输出的原始 JSON 数据相对应的更多字段。

即似乎组合查询(带有连接)跳过了那些 JSON LogEntry ContainerLog 条目,但为什么呢?

据我所知,组合查询不会以任何方式过滤 LogEntry 字段。

【问题讨论】:

  • 嗯,我刚刚注意到组合查询的结果显示所有行的相同 LogEntry 数据。也许与以下“项目”的“加入”是错误的?

标签: azure kubernetes azure-aks azure-log-analytics


【解决方案1】:

更改后的查询似乎会产生我期望的结果:

我将joinlookup 交换,并使用更多列来区分KubePodInventory 结果。

let KubePodLogs = (clustername:string, podnameprefix:string) {
    let ContainerIdList = KubePodInventory
    | where ClusterName =~ clustername
    | where Name startswith strcat(podnameprefix, "-")
    | where strlen(ContainerID)>0
    | distinct ContainerID, PodLabel, Namespace, PodIp, Name;
    ContainerLog
    | where ContainerID in (ContainerIdList)
    | lookup kind=leftouter (ContainerIdList) on ContainerID
    | project-away Image, ImageTag, Repository, Name, TimeOfCommand
    | project-rename PodName=Name1
};
KubePodLogs('aks-my-cluster', 'my-service') | order by TimeGenerated desc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-24
    • 1970-01-01
    • 2023-01-09
    • 2021-12-07
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多