Azure AKS日志查询KQL表达式

背景需求

Azure(Global) AKS集群中,需要查询部署服务的历史日志,例如:我部署了服务A,但服务A的上一个版本Pod已经被杀掉由于版本的更新迭代,而我在命令行中只能看到当前版本的pod日志,无法通过kubectl logs 看到应用的历史日志(例如上一个版本的pod日志),但如果这时候需要看,那么就需要手动去写AKS的KQL语句来查询了

查询语句(Azure Global)

复制代码
##queryPodLogs for Azure Global AKS
let startTimestamp = ago(7d);
//namespace
let aksNameSpace = "shop";
// specific podName
let aksPodName = "shop-backend-dev-j9w";
// specific contaierName
let aksContainerName = "shop-backend-dev";
// queryStartTime(UTC)
// queryEndTime(UTC)
ContainerLogV2
| where TimeGenerated > startTimestamp
| where PodName contains aksPodName and PodNamespace contains aksNameSpace and ContainerName contains aksContainerName
| where TimeGenerated between(datetime("2024-04-27 00:00:00") .. datetime("2024-04-30 05:00:00")) 
| project TimeGenerated, PodNamespace, ContainerId, ContainerName, PodName, LogMessage, LogSource
| sort by TimeGenerated desc

查询注意事项

查询前,记得修改下图上红线所划出来的参数值,具体解释都有标注

Tips:

如果不行的话试下我在下面提供的另一条KQL语句,因为在我写KQL语句的这段时间,似乎Azure Monitor这个模块的数据表正在做迁移,最后我是试了上面的语句成功查询出来的,而Azure内部的朋友经过实验他是用以下语句,联表查询到的数据,但我用下面这个语句查出来就一直是空的,查不到任何数据,所以这个可能需要视情况而定。

复制代码
let startTimestamp = ago(7d);
let aksNameSpace = "shop";
let aksPodName = "shop-backend-dev-j9w";
// let aksContainerName = "shop-backend-dev";
KubePodInventory
| where TimeGenerated > startTimestamp
| project ContainerID, PodName=Name, Namespace, ContainerName
| where PodName contains aksPodName and Namespace contains aksNameSpace
// and ContainerName contains aksContainerName
| distinct ContainerID, PodName, ContainerName
| join
(
    ContainerLogV2
    | where TimeGenerated between(datetime("2024-04-29 00:00:00") .. datetime("2024-04-29 05:00:00"))  //修改时间 
)
on ContainerName
| project TimeGenerated, PodName, ContainerName, LogMessage, LogSource
| sort by TimeGenerated desc
相关推荐
编码者卢布3 天前
【Azure APIM】通过 API Management 公开API为 MCP Server 的试验 (二)
microsoft·flask·azure
编码者卢布4 天前
【Azure APIM】通过 API Management 公开现有 MCP Server 的试验 (一)
microsoft·flask·azure
立心者06 天前
Sdcb Chats .. 发布,彻底移除 Azure.AI.OpenAI 专用包
人工智能·flask·azure
韭菜炒鸡肝天7 天前
【App Service】在Azure环境中如何查看App Service实例当前的网络连接情况呢?
microsoft·flask·azure
国际云,接待11 天前
Azure Front Door WAF实战:托管规则、限速与日志查询
microsoft·网络安全·云计算·azure·front door
编码者卢布16 天前
【Azure Policy】Policy修正任务为Azure资源添加诊断日志报错问题的调查
microsoft·flask·azure
编码者卢布18 天前
【Azure APIM】APIM的诊断日志与Application Insights的日志是否可以串联为一个端到端的日志链路呢?
python·flask·azure
MicrosoftReactor22 天前
技术速递|基于 OpenClaw、MCP 和 Azure Container Apps 构建自主 Microsoft Teams 智能体
microsoft·agent·azure·teams·mcp
编码者卢布23 天前
【Azure Application Insights】公网白名单应用如何使用 Application Insights 可用性测试?
flask·azure·可用性测试
IT大白鼠1 个月前
CVE-2026-7531 Azure Linux MariaDB PQC 内存破坏漏洞详解
linux·mariadb·azure·cve-2026-7531