Provisioning a Managed AKS Cluster

The following steps provision an AKS cluster with a managed node pool, attachs the created Azure Virtual Network, and grant AKS cluster identity the right to pull images from ACR.

Adding Configuration

The pulumi config CLI command can save some values as configuration parameters. Run the following commands to set the names for some of values that may its reusable in multiple environments:

$pulumi config set k8sVersion 1.30.3
$pulumi config set nodeCount 3
$pulumi config set nodeSize Standard_A2_v2
$pulumi config set adminUser ingress-nginx
$pulumi config set ingressNamespace 1.30.3
$pulumi config set appNamespace apps
$pulumi config set letenscriptEmail <your_email>

Read Config Values

You modify the config.ts file and add the following code:

import * as pulumi from "@pulumi/pulumi";

// Create a configuration object
const pulumiConfig = new pulumi.Config();

// Access a configuration value and export for reusing
export const config = {
    location: pulumiConfig.get("azure-native:location"),
    k8sVersion: pulumiConfig.get("k8sVersion") || "1.30.3",
    nodeCount: pulumiConfig.getNumber("nodeCount") || 3,
    nodeSize: pulumiConfig.get("nodeSize") || "Standard_A2_v2",
    adminUserName: pulumiConfig.get("adminUser") || "aksadmin",
    ingressNamespace: pulumiConfig.get("ingressNamespace") || "ingress-nginx",
    appNamespace: pulumiConfig.get("appNamespace") || "apps",
    letenscriptEmail: pulumiConfig.get("letenscriptEmail") || "<your_email>"
}

Create a Azure Kubernetes Cluster

In order to AKS cluster, you create a new akscluster.ts file in the resources folder. And add the following code creates managed cluster:

Explanation of Key Parts in the Code:

  • AKS Cluster: The core of the setup, an AKS cluster is created with:

    • agentPoolProfiles: Defines the VM size, node count, and operating system for the worker nodes.

    • enableRBAC: Enables Kubernetes Role-Based Access Control (RBAC) for cluster management.

    • networkProfile: Specifies the use of the Azure CNI plugin for network connectivity between pods and Azure resources.

    • identity: Assigns a UserAssigned Managed Identity, used for integrating with other Azure services securely.

  • Kubeconfig: The Kubernetes configuration is exported as an output, allowing you to connect to the AKS cluster using tools like kubectl.

Grant the AKS Cluster Identity the AcrPull Role on ACR

Once the AKS cluster and ACR are created, the next step is to assign the AcrPull role to the AKS cluster's managed identity. This is done by creating a role assignment that links the AKS cluster’s managed identity to the ACR.

Now from the index.ts file, you modify the code that includes aks cluster.

Once you’re ready, deploy the AKS cluster by running:

After the cluster is deployed, Pulumi will output acr, the kubeconfig and resourceGroupName.

You can save the kubeconfig to a file and connect to your AKS cluster using kubectl:

You can now use the following command to interact with your Kubernetes cluster:

The output looks like that:

Scaling and Managing the AKS Cluster

You can manage the AKS cluster post-deployment in various ways:

  • Scaling Nodes: Modify the count in agentPoolProfiles to scale the number of worker nodes, then run pulumi upto apply the changes.

  • Auto-Scaling: You can enable auto-scaling by adding enableAutoScaling and specifying the minimum and maximum node counts in the agentPoolProfiles configuration.

  • Upgrades: AKS provides automated upgrades to Kubernetes versions. You can trigger upgrades via the Azure portal, CLI, or integrate it with Pulumi to automate version updates.

Last updated