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

如何使用.NET通过订阅ID列出集群?求Azure SDK最新版本示例

仅通过订阅ID列出Azure订阅内所有AKS集群(.NET最新SDK实现)

1. 安装所需NuGet包

使用最新的Azure资源管理器SDK,需安装以下两个核心包:

  • Azure.ResourceManager.ContainerService:AKS集群管理的核心依赖包
  • Azure.Identity:提供多场景通用的认证方式

可通过NuGet命令行安装:

Install-Package Azure.ResourceManager.ContainerService
Install-Package Azure.Identity

2. 完整代码示例

以下代码直接通过订阅ID枚举订阅内所有区域的AKS集群,无需指定资源组:

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.ContainerService;

// 替换为你的目标订阅ID
string targetSubscriptionId = "your-subscription-id";

// 初始化ArmClient,使用DefaultAzureCredential自动处理认证
// 支持本地开发(Azure CLI、Visual Studio登录)、Azure托管环境(MSI等)多种场景
ArmClient armClient = new ArmClient(new DefaultAzureCredential());

// 获取指定订阅的资源对象
SubscriptionResource subscription = await armClient
    .GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{targetSubscriptionId}"))
    .GetAsync();

// 获取订阅下所有AKS集群的集合
ContainerServiceManagedClusterCollection clusterCollection = subscription.GetContainerServiceManagedClusters();

// 遍历并输出集群基础信息
await foreach (ContainerServiceManagedClusterResource cluster in clusterCollection.GetAllAsync())
{
    Console.WriteLine($"集群名称: {cluster.Data.Name}");
    Console.WriteLine($"所在区域: {cluster.Data.Location}");
    Console.WriteLine($"资源ID: {cluster.Id}");
    Console.WriteLine("---");
}

关键说明

  • 新SDK采用资源模型设计,以ArmClient为核心入口,替代了旧版弃用的ContainerServiceClient
  • GetAllAsync()方法会自动遍历订阅内所有区域的AKS集群,无需手动指定资源组或区域
  • DefaultAzureCredential自动适配不同环境的认证逻辑,无需硬编码凭据,安全性更高

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:16:09