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
相关推荐
珠峰下的沙砾2 天前
如何将 Azure Active Directory (Azure AD) 作为 SAML IdP 对接到 Keycloak
microsoft·flask·azure
weixin_307779134 天前
实现Azure Synapse Analytics安全地请求企业内部API返回数据
开发语言·python·云计算·azure
weixin_307779135 天前
实现Azure Function安全地请求企业内部API返回数据
开发语言·python·云计算·azure
weixin_307779139 天前
Azure Synapse Dedicated SQL pool里大型表对大型表分批合并数据的策略
数据仓库·sql·microsoft·azure
office大师姐17 天前
微软 SC-900 认证-考核Azure 和 Microsoft 365中的安全、合规和身份管理(SCI)概念
安全·microsoft·云计算·azure
{⌐■_■}20 天前
【Kubernetes】Kubernetes 在云平台(AWS EKS、GCP GKE、Azure AKS)的使用有何不同
kubernetes·azure·aws
csdn_aspnet21 天前
使用 .NET 9 和 Azure 构建云原生应用程序:有什么新功能?
microsoft·云原生·azure
Elastic 中国社区官方博客1 个月前
Elasticsearch:使用 Azure AI 文档智能解析 PDF 文本和表格数据
大数据·人工智能·elasticsearch·搜索引擎·pdf·全文检索·azure
office大师姐1 个月前
迈向云数据领域的第一步:Microsoft Azure DP-900认证指南
大数据·windows·microsoft·微软·azure
狐佑 怜1 个月前
Azure SDK 使用指南
microsoft·flask·azure