You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure Resource Graph查询指定AKS集群的所有命名空间

可行,实现方法如下

前提条件

你的3个AKS集群必须已启用Container Insights(容器见解),只有这样Kubernetes的命名空间等内部资源元数据才会被收集到Azure Log Analytics工作区,后续才能通过查询获取这些数据。


方案1:直接使用Log Analytics查询(推荐)

这是获取AKS命名空间最直接的方式,查询语句如下:

k8s.namespaces
| where ClusterName in ("aks-cluster-1", "aks-cluster-2", "aks-cluster-3") // 替换为你的AKS集群名称
| project ClusterName, Name, NamespaceCreationTime = todatetime(CreatedAt)
| sort by ClusterName, Name

方案2:使用Azure Resource Graph查询

如果你需要通过Resource Graph统一查询,可以通过关联Log Analytics工作区的方式实现,查询语句如下:

externaldata(ClusterName:string, Name:string, CreatedAt:string)
[h@"https://<log-analytics-workspace-id>.ods.opinsights.azure.com/api/v1/rest/query" 
    with (query="k8s.namespaces | where ClusterName in ('aks-cluster-1', 'aks-cluster-2', 'aks-cluster-3') | project ClusterName, Name, CreatedAt")]
| project ClusterName, NamespaceName = Name, NamespaceCreationTime = todatetime(CreatedAt)
| sort by ClusterName, NamespaceName

注意:需要将<log-analytics-workspace-id>替换为你的Log Analytics工作区ID,并且确保执行查询的账号拥有该工作区的读取权限。


补充说明

  • 如果你的AKS集群尚未启用Container Insights,可以通过Azure门户、Azure CLI或ARM模板快速启用,启用后需要等待10-15分钟左右,Kubernetes资源数据才会开始同步到Log Analytics。
  • 若要查询所有AKS集群的命名空间,只需去掉where ClusterName in (...)条件即可。

内容的提问来源于stack exchange,提问作者vcb0127

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:26:06