Step-by-Step Guide: Deploying a Web Application on Azure Kubernetes Service (AKS)


This guide will walk you through the entire process of deploying a web application on Azure Kubernetes Service, including creating a Docker image, pushing it to Azure Container Registry (ACR), and configuring AKS to pull the image.
Prerequisites
Azure account with active subscription
Azure CLI installed (installation guide)
Docker installed (installation guide)
kubectl installed (installation guide)
Your web application code ready for containerization
Step 1: Open project folder in Vscode.
1.1 On vscode Click on Open folder
1.2 Select your folder and Click on Select Folder button
Step 2: Setup Azure Resources.
2.1 Login into Azure using az login
2.1.1 Select the Email Account attached to your Azure account and click on Continue button
2.1.2 You will see a prompt that says "Select a subscription and tenant." Press the Enter key to proceed, and you will be logged into Azure successfully from the terminal.
Step 3: Create A Resource Group in Azure.
3.1 Run the command az group create --name (name of resource group) --location (Azure location availability zone).
i.e az group create --name subbyRG --location uksouth
Step 4: Create Azure Container registry
4.1 Run the command az acr careate --resource-group( resource group name) --name (name of your container registry) --sku (select your plan if its basic, standard or premium) --admin-enable true --location uksouth
i.e az acr create --resource-group subbyRG --name subbyregistry --sku Standard --admin-enabled true --location uksouth
4.1.2 Login to Azure portal to verify your Container registry.
Click on "Container registries." If it's not visible on your portal, use the search box to find "Container registries."
4.1.3 The subbyregistry has been successfully created under the resource group subbyRG in the specified location, uksouth.
4.1.4 Click on subbyregistry and copy the Login sever subbyregistry.azurecr.io, as it will be used when creating our Docker image and when pushing our image to Azure Container Registry.
Step 5: Create a Dockerfile and Build Docker image of our Web Application.
5.1 Create a file name Dockerfile
5.1.1 Write the Docker instruction inside the dockerfile using Nginx as the base image.
FROM nginx:latest
RUN rm -rf /usr/share/nginx/html/*
COPY . /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
5.1.2 Build docker image by running the Docker build command
docker build -t Login server/hostit:latest .
i.e docker build -t subbyregistry.azurecr.io/hostit:latest .
5.1.3 To verify the build, run the command docker image ls If the build is successful, you will see the image ID, creation time, size, and repository.
Step 6: Log in to the Azure Container Registry and push the Docker image to it.
6.1 Login into Azure Container Registry using the command az acr login --name subbyregistry
6.1.2 Run the command docker push (docker image):tag to push our image to Azure Container Regsistry.
1.e docker push subbyregistry.azurecr.io/hostit:latest
Step 7: Create ssh key
7. 1 Create a directory by running the command mkdir .ssh
7.1.2 Run the command ssh-keygen -f .ssh/subby-ssh to generate a public/private key. Note subby-ssh is the name given to my key
Run the command ssh-keygen -f .ssh/subby-ssh to generate a public/private SSH key pair. This key pair will be specified when creating your Kubernetes clusters to enable secure access. The name subby-ssh is the identifier for your key, allowing you to easily reference it later in your deployment process.
Step 7: Create Azure Kubernetes Service (AKS) Cluster
Run the command to create Azure Kubernetes Cluster
az aks create --resource-group BinmaRG --name binmaAks --node-count 2 --nodepool-name binmapool --node-resource-group binmanodeRG --generate-ssh-keys --ssh-key-value ~/.ssh/binma-ssh.pub --network-plugin azure --enable-cluster-autoscaler --min-count 2 --max-count 3 --zone 2
**Explanation of the “**az aks create” Command
This command creates an Azure Kubernetes Service (AKS) cluster with specific configurations. Let me break down each part:
Basic Cluster Information
--resource-group subbyRG: The cluster will be created in the existing resource group named "BinmaRG"
--name subbyAKS: The AKS cluster will be named "subbyAks"
Node Pool Configuration
--node-count 2: Initially creates 2 worker nodes
--nodepool-name subbypool: Names the node pool "subbypool"
--node-resource-group subbynodeRG: Places the node resources in a separate resource group called "subbynodeRG"
SSH Access
--generate-ssh-keys: Generates SSH keys if they don't already exist
--ssh-key-value ~/ .ssh/subby-sh.pub: Uses the specified public SSH key for node access
Networking
--network-plugin azure: Uses Azure's native networking plugin
Autoscaling
--enable-cluster-autoscaler: Enables automatic scaling of the node pool
--min-count 2: Minimum number of nodes is 2
--max-count 3: Maximum number of nodes is 3
Step 8: Deploy to Azure Kubernetes Service
8.1 Get AKS credentials by running the command az aks get-credentials --resource-group (resource group) --name (kubernetes cluster name)
i.e az aks get-credentials --resource-group subbyRG --name subbyAKS
This command retrieves the access credentials for an Azure Kubernetes Service (AKS) cluster and configures your local kubectl (Kubernetes command-line tool)
8.1.2 Create a Kubernetes deployment
Run the command kubectl create deployment (name deployment) --image=(docker image):tag --port=80
i.e kubectl create deployment subbydeployment --image=subbyregistry.azurecr.io/hostit:latest --port=80
8.1.3 Create image pull secret (to allow AKS to pull image from ACR)
Running the command az aks update --resource-group (resource group) --name (cluster) --attach-acr (Azure container registry)
az aks update --resource-group subbyRG --name subbyAks --attach-acr subbyregistry
8.1.4 Create service to expose our application to the internet using a load balancer by running the command kubectl expose deployment (deployment name) --name=(service name) --type=LoadBalancer --port=80 --protocol=TCP
kubectl expose deployment subbydeployment --name=subby-service --LoadBalancer --port=80 --protocol=TCP
8.1.5 Get External IP
Run the command kubectl get service to view our external IP address.
8.1.6 Paste external IP into the browser to view your application.
Conclusion
Deploying a web application on Azure Kubernetes Service (AKS) provides a scalable, reliable, and cloud-native solution for managing containerized applications. By following this step-by-step guide, you have successfully:
Set up an AKS cluster using Azure CLI or the Azure Portal.
Containerized your web application using Docker and pushed it to Azure Container Registry (ACR).
Deployed the application to AKS (Azure Kubernetes Service).
Exposed the application to the internet using a LoadBalancer.
Verified the deployment by accessing the application on web browser.
Subscribe to my newsletter
Read articles from SUBAIR NURUDEEN ADEWALE directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

SUBAIR NURUDEEN ADEWALE
SUBAIR NURUDEEN ADEWALE
Hello! I'm Subair Nurudeen Adewale, a cloud computing enthusiast and the founder of Nucloud Blog. I'm passionate about sharing knowledge and insights on cloud infrastructure, security, innovation, and the intersection of technology and design. As a cloud expert with a keen eye for user experience, I explore the latest trends and best practices in UI/UX, and how they can be applied to create intuitive, user-friendly, and seamless experiences for cloud-based applications and services. Follow Nucloud Blog for the latest updates and insights from the world of cloud computing and UI/UX design.