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
相关推荐
今天也要学习吖13 小时前
Azure TTS Importer:一键导入,将微软TTS语音接入你的阅读软件!
人工智能·学习·microsoft·ai·大模型·aigc·azure
爱知菜6 天前
Azure AI Search 探索总结
azure·rag·ai search
Leinwin13 天前
微软Azure AI Foundry正式上线GPT-5系列模型
人工智能·microsoft·azure
Elastic 中国社区官方博客18 天前
Elastic 9.1/8.19:默认启用 BBQ,ES|QL 支持跨集群搜索(CCS)正式版,JOINS 正式版,集成 Azure AI Foundry
大数据·elasticsearch·搜索引擎·全文检索·azure·elastic
云攀登者-望正茂19 天前
Azure DevOps — Kubernetes 上的自托管代理 — 第 5 部分
kubernetes·azure·devops
qq_3168377522 天前
String boot 接入 azure云TTS
python·flask·azure
Azure DevOps1 个月前
在Azure DevOps的工作项中使用markdown
运维·microsoft·flask·azure·devops
正宗咸豆花1 个月前
在 Azure 中配置 SMS 与 OTP
microsoft·flask·azure
Ashmcracker1 个月前
Azure DevOps 使用服务主体配置自托管代理 (Self-hosted Agent) 配置指南
microsoft·微软·云计算·azure·devops
IT考试认证1 个月前
微软 AI-900 Azure AI Fundamentals题库
微软·azure·ai-900·mcf