Azure Kubernetes Service (AKS) integration
This article is the reference for the Aspire Azure Kubernetes Service (AKS) hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model an AKS environment and its node pools in your AppHost project.
The Aspire AKS hosting integration deploys your application to a new or existing AKS cluster. By default, Aspire provisions the cluster, Azure Container Registry (ACR), cluster managed identities, and any Azure resources your app depends on. You can instead reference a cluster or registry that your team already manages.
Hosting integration
Section titled “Hosting integration”To get started with the Aspire AKS hosting integration, install the 📦 Aspire.Hosting.Azure.Kubernetes NuGet package in the AppHost project:
aspire add azure-kubernetesThe Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
Select an integration to add:
> azure-kubernetes (Aspire.Hosting.Azure.Kubernetes)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.Kubernetes@*<PackageReference Include="Aspire.Hosting.Azure.Kubernetes" Version="*" />The C# project examples also require the .NET hosting integration:
aspire add dotnetThe Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
Select an integration to add:
> dotnet (Aspire.Hosting.Dotnet)> Other results listed as selectable options...#:package Aspire.Hosting.Dotnet@*<PackageReference Include="Aspire.Hosting.Dotnet" Version="*" />Add AKS environment
Section titled “Add AKS environment”After installing the package, add an AKS environment to your AppHost using AddAzureKubernetesEnvironmentaddAzureKubernetesEnvironmentAddAzureKubernetesEnvironment:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const aks = await builder.addAzureKubernetesEnvironment('aks');
const api = await builder .addNodeApp('api', './api', 'src/index.ts') .withHttpEndpoint({ env: 'PORT' }) .withExternalHttpEndpoints();
await builder.build().run();var builder = DistributedApplication.CreateBuilder(args);
var aks = builder.AddAzureKubernetesEnvironment("aks");
var api = builder.AddDotnetProject("api", "../MyApi/MyApi.csproj");
builder.Build().Run();When an AKS environment is present, all compute resources are automatically deployed to AKS — no additional opt-in is required.
Use an existing AKS cluster
Section titled “Use an existing AKS cluster”Starting in Aspire 17.0, you can deploy to a pre-provisioned AKS cluster with the shared Azure existing-resource APIs:
const clusterName = await builder.addParameter('cluster-name');const resourceGroup = await builder.addParameter('cluster-resource-group');const subscription = await builder.addParameter('cluster-subscription');
const aks = await builder.addAzureKubernetesEnvironment('aks');await aks.asExistingInResourceGroup(clusterName, resourceGroup, subscription);var clusterName = builder.AddParameter("cluster-name");var resourceGroup = builder.AddParameter("cluster-resource-group");var subscription = builder.AddParameter("cluster-subscription");
var aks = builder.AddAzureKubernetesEnvironment("aks") .AsExistingInResourceGroup(clusterName, resourceGroup, subscription);When Aspire prompts during publishing or deployment, provide the existing cluster’s name, resource group, and subscription ID.
Use asExisting(clusterName) in TypeScript or AsExisting(clusterName, null) in C# when the cluster is in the application’s Azure resource group. publishAsExisting / PublishAsExisting and publishAsExistingInResourceGroup / PublishAsExistingInResourceGroup apply only when publishing or deploying.
An explicit existing-cluster resource group and subscription select the cluster independently of the resource’s Scope, which controls the module that deploys supporting infrastructure. A resource-group-only cluster reference inherits the module’s subscription. An unscoped reference inherits its resource group too. Credential retrieval uses the cluster resource ID returned by provisioning.
Existing cluster behavior
Section titled “Existing cluster behavior”- Aspire references the cluster without changing its tier, identities, node pools, networking, or add-ons. Cluster-creation settings don’t reconfigure it. Aspire doesn’t assign workloads to an automatically created
workloadnode pool. Explicit node-pool affinity must name a pool that already exists. - The cluster must have a kubelet managed identity for registry access. Workloads that use Azure workload identity also require OIDC and workload identity to be enabled beforehand.
- Any ingress add-ons that the application uses must already be installed and configured.
- For private clusters with the public DNS name disabled, the cluster FQDN output falls back to the private DNS name, or an empty string if neither is available. Deploying still requires network connectivity and DNS resolution from the deployment host.
- Aspire still provisions the default container registry unless you select another registry. It grants the cluster’s existing kubelet identity
AcrPullaccess without creating replacement cluster identities, and creates workload identity federated credentials when needed. The deploying identity needs permission to create these resources and role assignments, retrieve AKS credentials, and deploy Kubernetes workloads.
Use an existing container registry
Section titled “Use an existing container registry”Select an Azure Container Registry with the AKS-specific WithContainerRegistrywithContainerRegistryWithContainerRegistry API. This replaces the automatically created registry for image publishing and deployment.
The following examples reference a registry in another resource group in the deployment subscription:
const registry = await builder.addAzureContainerRegistry('registry');await registry.publishAsExisting('myregistry', { resourceGroup: 'registry-resource-group' });
const aks = await builder.addAzureKubernetesEnvironment('aks');await aks.withContainerRegistry(registry);var registry = builder.AddAzureContainerRegistry("registry") .PublishAsExisting("myregistry", "registry-resource-group");
var aks = builder.AddAzureKubernetesEnvironment("aks") .WithContainerRegistry(registry);For a registry in another subscription, replace the existing-resource call with:
await registry.publishAsExistingInResourceGroup( 'myregistry', 'registry-resource-group', '<registry-subscription-id>');registry.PublishAsExistingInResourceGroup( "myregistry", "registry-resource-group", "<registry-subscription-id>");Starting in Aspire 17.0, Aspire grants the AKS kubelet identity AcrPull at the selected registry’s scope, independently of the cluster’s resource group or subscription. This applies to both new and existing clusters. The deployment identity must have permission to create role assignments at that scope and push images to the registry.
AcrPull permits pulling images from every repository in the registry, including unrelated images in a shared registry. The registry must use RBAC Registry Permissions mode. Registries using RBAC Registry + ABAC Repository Permissions mode require different roles that this integration doesn’t configure automatically.
Workloads can potentially obtain kubelet identity tokens through the node’s Instance Metadata Service (IMDS). A separate kubelet identity and Workload Identity alone don’t prevent that access. Review the AKS IMDS restriction guidance, including its preview status and host-network and privileged-workload limitations.
Cluster identities and permissions
Section titled “Cluster identities and permissions”For clusters it provisions, Aspire 17.0 creates separate user-assigned managed identities for the control plane and kubelets. Both inherit the cluster’s resource-group and subscription scope, including scopes set through provisioning customization. Neither is a workload identity:
| Identity | Permissions |
|---|---|
| Kubelet | AcrPull on the selected registry. |
| Control plane | Managed Identity Operator on the kubelet identity, and Network Contributor on virtual networks selected through environment or node-pool WithSubnet calls. |
These role assignments are provisioned before the cluster in both the deployment pipeline and generated main.bicep. AKS grants the control-plane identity its normal infrastructure permissions in the AKS-managed node resource group. Aspire doesn’t grant Contributor on the application resource group or subscription. If provisioning customization references other external resources, such as route tables, network security groups, or private DNS zones, configure the corresponding control-plane permissions separately. See AKS managed identity role assignments.
Upgrade an Aspire-managed cluster
Section titled “Upgrade an Aspire-managed cluster”This migration applies when Aspire manages the cluster, not when you reference it with an existing-resource API.
Cluster tier
Section titled “Cluster tier”Starting in Aspire 17.0, clusters provisioned by Aspire use the Standard control-plane tier by default. Earlier versions defaulted to Free. Standard provides an uptime SLA and adds a cluster-management charge to the underlying Azure resource costs. See AKS pricing tiers for details. The tier doesn’t change node VM sizes or counts and doesn’t guarantee application availability.
To select Free for development or testing, customize the generated infrastructure:
Add the experimental provisioning integration to expose typed AKS infrastructure APIs:
aspire add Aspire.Hosting.Azure.Provisioning.ContainerServiceimport { ManagedClusterSkuTier } from './.aspire/modules/aspire.mjs';
const aks = await builder.addAzureKubernetesEnvironment('aks');await aks.configureInfrastructure(async infrastructure => { const cluster = await infrastructure.getContainerServiceManagedCluster(); const sku = await cluster.sku.get(); await sku.tier.set(ManagedClusterSkuTier.Free);});The provisioning integration is experimental and emits ASPIREAZUREPROVISIONING001.
using Azure.Provisioning.ContainerService;
var aks = builder.AddAzureKubernetesEnvironment("aks") .ConfigureInfrastructure(infrastructure => { var cluster = infrastructure.GetProvisionableResources() .OfType<ContainerServiceManagedCluster>() .Single();
cluster.Sku.Tier = ManagedClusterSkuTier.Free; });Configure the system node pool
Section titled “Configure the system node pool”Customize VM size, scaling, and OS SKU with WithSystemNodePoolwithSystemNodePoolWithSystemNodePool. Starting in Aspire 17.0, the configuration callback supports OS SKU selection:
const aks = await builder.addAzureKubernetesEnvironment('aks');await aks.withSystemNodePool(async pool => { pool.vmSize = 'Standard_D2as_v5'; pool.minCount = 1; pool.maxCount = 5; pool.osSku = 'AzureLinux';});var aks = builder.AddAzureKubernetesEnvironment("aks") .WithSystemNodePool(pool => { pool.VmSize = "Standard_D2as_v5"; pool.MinCount = 1; pool.MaxCount = 5; pool.OsSku = "AzureLinux"; });The callback receives an AksNodePoolConfig with defaults of Standard_D2s_v5 and an autoscaling range of 1-3 nodes. System pools require at least one node. Each call replaces the previous system pool configuration, including its OS SKU.
Leaving OsSku / osSku unset omits osSKU from generated Bicep and preserves the AKS default, including for automatically created pools. Values such as AzureLinux and Ubuntu select the OS SKU, not a specific image version. These APIs configure Linux node pools. Azure validates SKU availability and compatibility.
Add node pools
Section titled “Add node pools”Add additional node pools for workload isolation, GPU workloads, or specialized hardware requirements using AddNodePooladdNodePoolAddNodePool:
const aks = await builder.addAzureKubernetesEnvironment('aks');const gpuPool = await aks.addNodePool('gpupool', async pool => { pool.vmSize = 'Standard_NC6s_v3'; pool.minCount = 0; pool.maxCount = 5; pool.osSku = 'Ubuntu';});
const worker = await builder.addContainer('ml-worker', 'my-ml-image');await worker.withNodePool(gpuPool);var aks = builder.AddAzureKubernetesEnvironment("aks");var gpuPool = aks.AddNodePool("gpupool", pool =>{ pool.VmSize = "Standard_NC6s_v3"; pool.MinCount = 0; pool.MaxCount = 5; pool.OsSku = "Ubuntu";});
builder.AddContainer("ml-worker", "my-ml-image") .WithNodePool(gpuPool);Additional pools have the same default VM size and autoscaling range as system pools, but can scale to zero. Parameter-based C# calls such as aks.AddNodePool("gpupool", "Standard_NC6s_v3", minCount: 0, maxCount: 5) remain supported.
The C# callback receives the configuration stored in the app model, not a copy. Extensions can compose changes through the retained configuration or gpuPool.Resource.Config until infrastructure generation. The final values are validated and applied to generated Bicep at that point. Later mutations don’t change artifacts already generated.
Add a persistent volume
Section titled “Add a persistent volume”Call AddPersistentVolumeaddPersistentVolumeAddPersistentVolume directly on the AKS environment to model durable storage without reaching through to the underlying Kubernetes integration. The call forwards to the AKS environment’s Kubernetes publisher, so the same configuration methods and workload-binding APIs described in Persistent volumes on Kubernetes apply:
const aks = await builder.addAzureKubernetesEnvironment("aks");
const data = await aks.addPersistentVolume("data");await data.withCapacity("20Gi");
const api = await builder .addNodeApp('api', './api', 'src/index.ts') .withHttpEndpoint({ env: 'PORT' }) .withExternalHttpEndpoints() .withKubernetesPersistentVolumeMount(data, "/data");var aks = builder.AddAzureKubernetesEnvironment("aks");
var data = aks.AddPersistentVolume("data") .WithCapacity("20Gi");
builder.AddDotnetProject("api", "../Api/Api.csproj") .WithPersistentVolume(data, "/data");When you don’t set a storage class, the generated claim omits spec.storageClassName so the cluster’s default storage class provisions the disk. A standard AKS cluster dynamically provisions an Azure managed disk for such claims. To request Premium SSD storage explicitly, call WithStorageClass("managed-csi-premium") in C# or withStorageClass('managed-csi-premium') in TypeScript.
For the full set of configuration methods (storage class, capacity, access modes, annotations) and how to bind volumes to workloads, see Persistent volumes on Kubernetes.
Configure CPU and memory requests and limits
Section titled “Configure CPU and memory requests and limits”AKS uses the Kubernetes publishing path for application workloads. In a C# AppHost, use PublishAsKubernetesService to configure the generated pod container’s scheduling requests and runtime limits. See Configure CPU and memory requests and limits for the sample and Kubernetes quantity syntax.
Publishing and deployment
Section titled “Publishing and deployment”The AKS integration supports both aspire publish (generate Helm chart and Bicep artifacts) and aspire deploy (provision Azure infrastructure and deploy in a single command).
For a complete end-to-end walkthrough, see Deploy to AKS.