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
相关推荐
MicrosoftReactor3 天前
技术速递|Java on Azure Tooling 8月更新 - Java 体验在 Azure 容器应用程序正式发布
java·容器·azure
Azure DevOps3 天前
Azure DevOps Server:不能指派新增的用户
运维·microsoft·flask·azure·devops
Monee..3 天前
【azure-openai】批量翻译demo【python】【gradio】
python·azure·gradio
杰哥在此4 天前
Python知识点:使用Azure IoT Edge与Python进行边缘计算
python·物联网·面试·编程·azure
微软技术栈5 天前
Azure Data Box 80 TB 现已在中国区正式发布
microsoft·big data·azure
Vertira5 天前
Azure Kinect 人体跟踪关节
microsoft·azure
全云在线allcloudonline5 天前
Azure OpenAI检索增强微调:使用 GPT-4o 对 GPT-4o mini 进行微调,以适应特定领域的应用
microsoft·azure
黑客小七6 天前
云中红队系列 | 使用 Azure FrontDoor 混淆 C2 基础设施
经验分享·python·microsoft·面试·职场和发展·flask·azure
全云在线allcloudonline7 天前
安全、稳定、SLA高达99.9%:Azure OpenAI数据分离与隔离优势
microsoft·azure
MicrosoftReactor9 天前
技术速递|宣布 Azure Container Apps 上的 Java 体验正式推出
java·azure