Getting Started with Azure Kubernetes Service (AKS) Using Azure CLI

Kubernetes has become the industry standard for managing containerized applications, and Azure Kubernetes Service (AKS) offers a fully managed solution that simplifies the deployment and operation of Kubernetes clusters on Azure.

If you're looking to get hands-on with Azure Kubernetes Service, there's no better way to start than by using the Azure CLI. In this tutorial, you’ll learn how to provision a Kubernetes cluster, deploy an application, and expose it to the internet, all from your terminal.

To follow along, you'll need the following:

  • An active Azure subscription

  • Visual Studio Code installed

  • Azure CLI installed on your local machine (Install via Windows PowerShell)

  • Kubernetes command line(kubectl) installed on your local machine (Install via Windows PowerShell)

  • Basic familiarity with Azure and the terminal.

Are you ready to deploy your first cluster and expose a service to the internet? Let’s get started.

Step 1: Launch your VS code and install the the Kubernetes extension by clicking on the extension tool on the left pane. After successful installation, open a new folder for this Azure Kubernetes Project. You can name it “Kubernetes Project”. After creating the folder for the project, you can then open up your terminal on the application.

Step 2: From your terminal, run; kubectl version to confirm the version of the official Kubernetes command line you have. Next, run az login to log into your azure portal from the terminal. You will be able to select the Azure account you want to log into. You can then choose the subscription type you want and will be successfully logged in.

Step 3: Once you’re logged into your Azure account from the terminal, create a directory/folder to store your ssh key. Name the folder .ssh, To create this directory, run the command mkdir .ssh and run the command: ssh-keygen -f .ssh/mykey-ssh

This command generates your key fingerprint and save it securely in the .ssh directory you created.

Step 4: Your next step is to create a resource group. Create a resource group by running the command: az group create - -name hub - -location eastasia .

This command specifies the name of the resource group, hub, and the region you want it to be created in, eastasia. Note: You will be able to see these changes in your Azure portal. For example, you will be able to see the Resource group you created here using the command line in your Azure account since you’re logged into your azure account.

Step 5: Now its time to create your Azure Kubernetes Service, AKS. Before that, you have to register microsoft.containerservice on your Azure portal. To do that, go to your azure portal and search for subscription. Under the subscription you’re presently using, go to Resource providers and search for “microsoft.containerservice“, click it and ensure to register by clicking on the “register button“ at the top.

To create your Kubernetes cluster, from the Command line interface on VS code, run the command: az aks create - -name myaks - -resource-group hub --node-count 2 --nodepool-name saturn --node-resource-group mynode-rg --generate-ssh-key --ssh-key-value ~/.ssh/aks-ssh.pub --network-plugin azure --enable-cluster-autoscaler --min-count 2 --max-count 3 --zone 3.

This command creates your cluster named myaks, under the resource group you created earlier, hub. It creates two nodes in your kubernetes cluster, with the resource group mynode-rg. Generates the ssh key with Azure as network plugin. And also enable cluster autoscaler with the minimum count of two nodes and maximum count of three nodes.

You can confirm all of these changes on your Azure Portal. You will notice you have three resource groups when you search for “Resource groups“ from your azure portal. They are hub, which you created yourself at first from the command line (see step 3), mynode-rg, which you created when creating your AKS cluster, and Network-Watcher, this is usually generated automatically by default when creating a resource.

From your Node Resource group, you will be able to see Pubic IP address, Network Security Group, Virtual Machine Scale-sets, Virtual Network, Load Balancer, and so on, all in the east asia region where you placed it.

Step 6: From your terminal, run az aks get-credentials --name myaks --resource-group hub.
This command merges the your node-pool saturn as current context.

Step 7: From your terminal, you can check to confirm the details of the nodes you created by running: kubectl get nodes. You can also check all the namepaces you have by running: kubectl get namespace -A

You can create a namespace for a particular deployment you’d like to create by running: kubectl create namespace sphere.
This specifies that we’re creating a namespace named “sphere“ and we will be declaring that our deployment get thrown in that namespace when we create our deployment.

Step 8: Let’s create our deployment. To do that, run: kubectl create deployment nginx-deployment --image=nginx:latest --replicas=3 --port=80 -n sphere

This command specifies you want to create a deployment named “nginx-deployment“ with the image “nginx“ and have three replicas of the deployment. The deployment will also be in the “sphere“ namespace.

From the image below, you can see that your nginx-deployment has been created. To further confirm, run kubectl get deployment --namespace sphere

Step 9: Now you have to expose your deployment so others can access, run the command: kubectl expose deployment nginx-deployment --namespace=sphere --name=nginx-service --type=LoadBalancer --port=80 --protocol=TCP

This exposes the nginx-deployment in the namespace “sphere“ and gives it a name “nginx-service“. The type is a LoadBalancer with enables it to be exposed externally. To confirm if your deployment in the namespace “sphere“ has been successfully exposed, run: kubectl get service --namespace=sphere

Step 10: Now you can copy the External-IP and paste it in a browser to see if you can access your service. See Image of the result below:

Congratulations on getting your first Azure Kubernetes Service deployment up and running using just your terminal! By following these steps, you’ve not only provisioned a Kubernetes cluster but also successfully deployed and exposed your application to the internet using Azure CLI, a foundational skill for any aspiring cloud or DevOps engineer.

Getting comfortable with AKS takes practice, and there’s always more to explore, scaling, monitoring, security and more. But this is a solid start, and you should be proud of what you’ve built.

If you found this guide helpful, don’t forget to like, share, or leave a comment below. Feel free to ask questions in the comments or reach out to me directly, I’ll be happy to help!

4
Subscribe to my newsletter

Read articles from Damilola Linda Olowookere directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Damilola Linda Olowookere
Damilola Linda Olowookere