Quickly launch an Azure AKS Cluster
Over the next few weeks, I am planning on doing some work which requires me to spin up and teardown several small Kubernetes clusters.
As I have access to an Azure Subscription and Microsoft have made the process of launching a cluster quite a painless experience I thought it would make sense to write a quick blog post to cover launching, configuring and tearing down an Azure AKS Cluster.
To start with I prefer to set a few environment variables on the command-line for things such and the resource name which are repeated through of the commands I need to run;
$ AKSLOCATION=uksouth$ AKSRG=quick-cluster-rg $ AKSCLUSTER=quick-cluster-aks $ AKSNUMNODES=2Once that the environment variables have been configured, using the same terminal window, we can create the resource group using the following command;
$ az group create --name $AKSRG --location $AKSLOCATIONIt should only take a few seconds to create the group. Now that the resource group is present we can then launch the AKS cluster, to do this use the following command;
$ az aks create \ --resource-group $AKSRG \ --name $AKSCLUSTER \ --node-count $AKSNUMNODES \ --enable-addons monitoring \ --generate-ssh-keysAs you may have guessed this launches and configures a cluster with the number of nodes which we defined in the $AKSNUMNODES environment variable. The process takes several minutes.
Once complete, the next step is to configure our local client to communicate with the newly launched cluster. The first thing you may need to do is install the client itself, luckily Microsoft has made this a simple process, and you can just run the following command;
$ az aks install-cliOnce installed you can run kubectl version to get confirmation that the agent is installed, don’t worry if you see an error about connecting to the cluster, we haven’t gotten that far yet.
Now with the client installed we need to grab the credentials needed to connect to our ASK cluster, again, this is quite a simple process, just run;
$ az aks get-credentials --resource-group $AKSRG --name $AKSCLUSTEROnce the connection details have been imported running the following command;
$ kubectl get nodesShould show that you have two nodes in your cluster;
Also, you should be able to see the cluster in the Azure Portal;
To tear down the cluster, return to the command-line and if needed re-enter just the AKSRG and AKSCLUSTER environment variables;
$ AKSRG=quick-cluster-rg$ AKSCLUSTER=quick-cluster-aksNow Running the command below will prompt if you want to delete the cluster;
$ az aks delete \ --resource-group $AKSRG \ --name $AKSCLUSTERIt is possible to add --yes to the command to skip the prompt; however, it is always best to check just in case you end up removing the wrong cluster;
The final Azure resource to remove will the resource group, to do this run;
$ az group delete --name $AKSRGNow that the Azure resources have been removed that just leaves us with our local configuration to tidy up. You can remove the cluster and context from your local configuration by running;
$ kubectl config delete-cluster $AKSCLUSTER $ kubectl config delete-context $AKSCLUSTERTo check that have been removed you can run;
$ kubectl config get-contexts$ kubectl config get-clustersThere you have it, with three commands you quickly spin up an X node Kubernetes cluster within Azure with little effort.
Related Posts

Rotating Azure DevOps SSH Keys: How to Update Your Git Remotes and SSH Config
Learn how to rotate expired Azure DevOps SSH keys, update your SSH configuration, and fix Git remote authentication quickly and securely.

Generating an Azure Storage Account SAS token using Azure Logic and Function apps
Learn how to generate an Azure Storage Account SAS token from a Logic App using an Azure Function. This step-by-step guide covers deploying a PowerShell-based Function App, creating the SAS token generation function, and integrating it into your Logic App workflow. Overcome common challenges and enhance your Azure development skills with this practical tutorial.

Infrastructure as Code for Beginners is out now
My new book, Infrastructure as Code for Beginners, has been released and is available to buy now.




