How to Create an Azure Kubernetes Cluster and Deploy an Application Using Azure CLI
Tags: devops, azure, kubernetes, beginners
Published: 2023-05-19 01:08:40

## Introduction
Azure Kubernetes Service (AKS) cluster is a managed container orchestration platform provided by Microsoft Azure. It enables the deployment, scaling, and management of containerized applications using Kubernetes.

Kubernetes is an open-source container orchestration system that helps you manage and automate the deployment, scaling, and management of containerized applications.

AKS clusters simplify the deployment and management of applications by abstracting away the underlying infrastructure complexities. They offer benefits such as automatic scaling, load balancing, and high availability. AKS clusters provide a reliable and scalable environment for running containerized applications on Azure.

Azure CLI (Command-Line Interface) is a powerful command-line tool for managing Azure resources. It provides a convenient and efficient way to interact with Azure services, including the creation and management of AKS clusters. Azure CLI offers a consistent experience across platforms and can be easily scripted, allowing for automation and streamlined workflows.

## Pre-requisites
Before diving into creating an AKS cluster and deploying applications, ensure you have the following prerequisites in place:

1. [An Azure subscription:](https://azure.microsoft.com/en-us/free/) Sign up for an Azure account if you don't have one.
2. [Azure CLI installed](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli): Install Azure CLI on your local machine.
3. [Kubectl installed](https://kubernetes.io/docs/tasks/tools/)
4. Working knowledge of Kubernetes.
5. Authenticate Azure CLI with your Azure account by running the command below and following the prompts:

```
az login
```

## Setting up an AKS Cluster

**Step 1.** Create a resource group. A resource group is a logical container that holds related Azure resources.

```
az group create --name <resource-group-name> --location <azure-region>
```
![resource group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gppss6641ex7v5cwe4ns.jpg)

**Step 2.** Create the AKS cluster

```
az aks create --resource-group <resource-group-name> \
--name <aks-cluster-name> \
--node-count 2 \
--generate-ssh-keys
```

- `--resource-group`: Specifies the name of the resource group where the AKS cluster will be created. Replace <resource-group-name> with the desired name of the resource group.
- `--name`: Specifies the name of the AKS cluster to be created. Replace <aks-cluster-name> with the desired name of the AKS cluster.
- `--node-count`: Specifies the number of nodes (virtual machines) that should be provisioned in the AKS cluster. In this example, we have set the node count to 2, but you can adjust it based on your application requirements.
- `--generate-ssh-keys`: Generates SSH public and private keys to be used for accessing the cluster nodes. This parameter simplifies the process of managing SSH keys. Azure CLI will automatically generate the keys and configure them for you.


![create aks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asb4znksc0rswpw9iqwo.jpg)

![aks successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i6q6zxxofrsgqkkxsyn.jpg)

**Step 3.** Get cluster credentials

```
az aks get-credentials --name <aks-cluster-name> --resource-group <resource-group-name 
```
By executing this command, Azure CLI retrieves the necessary credentials, including the Kubernetes cluster endpoint and authentication token, and configures the Kubernetes context on your local machine. This allows you to interact with the AKS cluster using Kubernetes command-line tools (kubectl) or any other Kubernetes client.


![aks cluster credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2gqm35itqi9jazum4z2.jpg)

You can run `kubectl get nodes` to see the nodes in your cluster.

**Step 4.** Clone the azure voting app github repo [here](https://github.com/Azure-Samples/azure-voting-app-redis).
The repo already has the Kubernetes deployment and service files needed to deploy the application.

**Step 5.** Deploy the application. 
A Kubernetes manifest file containing all the deployments and services is in the azure-vote-all-in-one-redis.yml file. To deploy this manifest, run this command:

```
kubectl apply -f azure-vote-all-in-one-redis.yml
```

![kubectl apply](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfglxvks1tqrhb1l4vuy.jpg)

**Step 6.** Visit your deployed site
The azure voting frontend has a service type of LoadBalancer, in order to visit your newly deployed site run the following command to get the external IP address of the LoadBalancer:

```
kubectl get service --watch
```
The --watch flag is added because the external IP address takes a little while before it is added to the LoadBalancer so instead of running the command multiple times, just add the watch flag to watch for any changes.

![loadbalancer external ip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gnkj0o4au3c8obdk9vv.jpg)
Copy the external IP and paste in your web browser to see the azure voting app website.

![live site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9wjchxqyxz2wajbokh1.jpg)

Step 7. Clean up your resources
It is important to clean up resources when you no longer have use for them to avoid incurring charges. To delete your resources, simply delete the resource group with the command below:

```
az group delete --name <resource-group-name>
```

## Conclusion
Throughout this article, we covered key steps for creating an AKS cluster and deploying applications using Azure CLI. We created a resource group, an AKS cluster, and deployed the Azure voting application to our cluster.
 
By leveraging Azure CLI and the power of AKS clusters, you can efficiently deploy and manage your applications on Azure, taking advantage of the scalability and flexibility of Kubernetes. Happy exploring and deploying your applications on Azure