Deploy the k8s-spot-termination-handler helm chart on Azure Kubernetes Service (AKS)
TypeScriptIn order to deploy the
k8s-spot-termination-handler
Helm chart on Azure Kubernetes Service (AKS), we'll follow these steps:- Create an AKS cluster if you don't already have one.
- Install the Helm chart for the spot termination handler onto the AKS cluster.
Let's start by setting up an AKS cluster. The following Pulumi program demonstrates how to set up a basic AKS cluster on Azure. We will use the
azure-native
plugin, which interacts with Azure resources natively.This program performs the following actions:
- Creates a new resource group to contain our AKS cluster.
- Sets up an AKS cluster within that resource group.
- Once the cluster is available, we'll configure
kubectl
to connect to the new AKS cluster.
After ensuring the cluster is up and running, we install the
k8s-spot-termination-handler
Helm chart. This chart helps to handle the graceful termination of Kubernetes nodes running on spot instances when a termination notice is received. When a spot instance receives a termination notice, the termination handler ensures that the Kubernetes control plane is aware of the impending termination, so that workloads can be rescheduled elsewhere.Here's the complete Pulumi TypeScript program that performs these steps:
import * as pulumi from "@pulumi/pulumi"; import * as azure_native from "@pulumi/azure-native"; import * as k8s from "@pulumi/kubernetes"; // Step 1: Create an Azure Resource Group const resourceGroup = new azure_native.resources.ResourceGroup("myResourceGroup"); // Step 2: Create an AKS cluster const aksCluster = new azure_native.containerservice.ManagedCluster("myAksCluster", { resourceGroupName: resourceGroup.name, agentPoolProfiles: [{ count: 3, // The desired number of agents (VMs) to run your workloads maxPods: 110, mode: "System", name: "agentpool", osType: "Linux", vmSize: "Standard_DS2_v2", type: "VirtualMachineScaleSets", // For the Spot instances, set appropriate properties here }], dnsPrefix: "myakscluster", // A DNS prefix that can be used to reach the AKS cluster kubernetesVersion: "1.20.9", // Use the desired Kubernetes version linuxProfile: { adminUsername: "adminuser", ssh: { publicKeys: [{ keyData: "ssh-rsa YOUR_SSH_PUBLIC_KEY" }], // Replace with your SSH public key }, }, enableRBAC: true, nodeResourceGroup: `MC_${resourceGroup.name}_myAksCluster_${resourceGroup.location}`, identity: { type: "SystemAssigned", }, }); // Step 3: Set up a KubeConfig to interact with the AKS cluster const kubeConfig = pulumi.all([aksCluster.name, resourceGroup.name]).apply(([clusterName, rgName]) => azure_native.containerservice.listManagedClusterUserCredentials({ resourceName: clusterName, resourceGroupName: rgName, }).then(creds => { const encoded = creds.kubeconfigs[0].value; return Buffer.from(encoded, 'base64').toString(); }) ); // Create a Kubernetes provider instance that uses our kubeconfig const k8sProvider = new k8s.Provider("k8sProvider", { kubeconfig: kubeConfig, }); // Step 4: Install the Helm chart for the k8s-spot-termination-handler const spotTerminationHandler = new k8s.helm.v3.Chart("k8s-spot-termination-handler", { chart: "k8s-spot-termination-handler", version: "1.1.0", // Specify the version of the Helm chart here fetchOpts: { repo: "https://kubernetes-charts-incubator.storage.googleapis.com/", // Ensure to use the correct repository }, }, { provider: k8sProvider }); // Export the kubeConfig to a file export const kubeConfigOutput = pulumi.secret(kubeConfig);
Make sure to replace
YOUR_SSH_PUBLIC_KEY
with the actual public key you want to use for your AKS cluster.Here's what each section does:
- Step 1: We define a resource group for our AKS cluster.
- Step 2: We then declare an AKS cluster with a specified agent pool profile. The
vmSize
and other properties should be adjusted according to your needs, including setting the properties for spot instances (not demonstrated here). - Step 3: We generate kubeconfig for our AKS cluster. This will allow us to interact with our Kubernetes cluster using
kubectl
or any other Kubernetes management tool that uses kubeconfig files. - Step 4: Finally, we declare a Helm chart resource. This resource will install the
k8s-spot-termination-handler
chart from the defined Helm chart repository onto our AKS cluster using the kubeconfig we created earlier.
You need to have Pulumi CLI installed and properly set up, and make sure that you have access to an Azure subscription and have the necessary permissions to create resources.
To apply this Pulumi code, you would normally run
pulumi up
in the directory where this file is located, and Pulumi would handle the rest, from provisioning the resources to configuring them as specified in the code.