How to Create an AKS Cluster and Enable Autoscaling Using Visual Studio Code
Introduction To Azure Kubernetes Service
Introduction
Azure Kubernetes Service (AKS) simplifies deploying a managed Kubernetes cluster in Azure by offloading the operational overhead to the cloud provider.You can effectively manage your AKS clusters, optimize your development process, and activate features like autoscaling to make sure your apps can handle variable loads thanks to Visual Studio Code's (VS Code) comprehensive integration. We'll take you through the process of using Visual Studio Code to establish an AKS cluster and configure autoscaling in this tutorial.
**
How to Create an AKS Cluster and Enable Autoscaling Using Visual Studio Code**
Introduction
Azure Kubernetes Service (AKS) simplifies deploying a managed Kubernetes cluster in Azure by offloading the operational overhead to the cloud provider. With the robust integration of Visual Studio Code (VS Code), you can efficiently manage your AKS clusters, streamline your development workflow, and enable features like autoscaling to ensure your applications can handle varying loads. In this guide, we'll walk you through the steps to create an AKS cluster and set up autoscaling using Visual Studio Code.
Prerequisites
Before you begin, ensure you have the following:
An active Azure subscription.
Visual Studio Code installed on your local machine.
Azure CLI installed and configured.
Install Minikube
- Kubectl installed.
Azure account extensions in VS Code:
Setup Your Environments: Installation Instructions
1.Azure account extensions in VS Code: Open the extensions view - Click on the Extensions icon in the Activity Bar on the side of the VS Code window (highlighted in red on the left side of the image) -In the search bar at the top of the Extensions view, type "kubernetes" to filter the extensions related to Kubernetes (highlighted in red) -Click the "Install" button (highlighted in red) to install the extension
Install Azure Account Extension: Repeat similar process as seen above
2.Install Minikube For Windows: Minikube is Local Kubernetes, focusing on making it easy to learn and develop for kubernetes. - Open your web browser and go to the URL: kubernetes.io/docs/home (highlighted in red at the top of the image). - Click on the "Documentation" tab in the top navigation bar (highlighted in red). - In the search bar on the left side of the page, type "install minikube" (highlighted in red).
2a.You will be prompted to this page, "Click On" "minikube start|minikube"
2b. You will be prompted to the Installation page - Click on the "Windows" button to get the installation instructions specific to Windows (highlighted in red). - Follow the steps outlined to download and run the installer for the latest release - Using PowerShell, you can install Minikube. Run the provided command in a PowerShell window (highlighted in red)
2c.Open PowerShell As An Administrator
2d.Run Command On Powershell - Copy & Paste command on the terminal - Web request is running as highlighted in red
Finished Running
2e. Copy the 2nd command from the kubernetes.io official website
Run 2nd command on Powershell
2f. Run command minikube
start to start and see if your minikube has been successfully installed
3.Install Kubectl: kubectl is the command-line tool for interacting with Kubernetes clusters. - Open your web browser and go to the URL: kubernetes.io/docs/home (highlighted in red at the top of the image). - Click on the "Documentation" tab in the top navigation bar (highlighted in red). - In the search bar on the left side of the page, type "install kubectl" (highlighted in red).
3a.Select Install Tools | Kubernetes
3b.Click On "Install Kubectl on Windows"
3c. Copy the command highlighted below and run it on powershell
Run on Powershell
3d: Copy the 2nd command
Run on Powershell
3e: Copy the Command highlighted in red
To automate verification as seen in the instruction, Run command and get "True" as the red arrow shows in the image
3f: Test to ensure the version of kubectl1
is the same as downloaded - Run command kubectl version --client
to verify
- Install Azure CLI for Windows - Click on link and follow instructions to install Azure CLI https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
Guidelines To Creating An AKS Cluster
Step 1: Create A Folder On VSC - Click On "File" and Select "Open Folder"
Step 1a: - Click on "New folder" to create a new folder and name it "kubernetes" or select an existing folder(highlighted in red). - Click the "Select Folder" button to open the chosen folder in Visual Studio Code (highlighted in red).
Step 1b: You will be prompted here "Click On" "Yes, I trust the authors"
Step 2: Open New Terminal - On the top right "Click On" "Terminal" and "Select" "New Terminal"
Step 2a: Log Into Azure From VSC Terminal - Run Command az login
Step 2b: Follow prompt and Pick an Account
Step 2c: Successfully Logged In
On The VSC Terminal it shows we are Logged In
Step 3: Create A Directory - Run command mkdir <name of directory>
to create a directory. In this case create .ssh
directory to store SSH keys (Secure Shell) and configuration files
Step 3a: Generate A New SSH Key Pair - Run command ssh-keygen -f .ssh/aks-ssh
ssh-keygen
: This is the command used to generate, manage, and convert authentication keys for SSH (Secure Shell).
-f .ssh/aks-ssh
: The -f
flag specifies the filename where the generated keys will be stored. In this case, the keys will be saved in the .ssh
directory with the filename aks-ssh
.
Step 3b: Input Passphrase - Type a secure passphrase and press Enter. This passphrase adds an additional layer of security, protecting your private key in case it is accessed by unauthorized users. - Confirm passphrase
Step 4: Create a Resource Group - Run command az group create --name ladyintech --location uksouth
- Replace
ladyintech
with your desired resource group name anduksouth
with your preferred region.
Step 4a: Run command az group list -o table
to list all resource groups in your Azure subscription and display the output in a table format.
Step 5: Create AKS Cluster - az aks create
: The base command to create a new AKS cluster.
--resource-group ladyintech
: Specifies the resource group (ladyntech
) where the AKS cluster will be created.--name MyAKSCluster
: Sets the name of the AKS cluster toMyAKSCluster
.--node-count 1
: Specifies the initial number of nodes in the node pool. Here, it starts with 1 node.--nodepool-name systemp
: Sets the name of the node pool tosystemp
.--node-resource-group myNodeRG
: Specifies a separate resource group (myNodeRG
) for the nodes.--ssh-key-value .ssh/
aks-ssh.pub
: Provides the public SSH key for accessing the nodes. This is the public key file generated earlier (.ssh/
aks-ssh.pub
).--network-plugin azure
: Uses the Azure CNI plugin for networking.--enable-cluster-autoscaler
: Enables the cluster autoscaler, which automatically adjusts the number of nodes in the node pool based on the cluster's needs.--min-count 1
: Sets the minimum number of nodes to 1.--max-count 2
: Sets the maximum number of nodes to 2.--zones 2
: Specifies the availability zone for the nodes. Using availability zones increases the reliability and availability of the nodes.
Step 5a: Successfully Created the AKS Cluster
Step 5b: Navigate to Azure Homepage - Click On All resources to view all the resources and dependencies created on VSC
Step 5c: The kubectl get node
command provides information about the nodes in your Kubernetes cluster. This includes the status, roles, age, and version of the nodes. It’s a useful command for monitoring the health and status of the cluster’s nodes
Step 6: Run command az aks get-credentials --resource-group <nameofresourcegroup> --name <nameofcluster>
to get the AKS cluster running
Step 7: The command kubectl get deployment --all-namespaces
is used to list all the deployments across all namespaces in a Kubernetes cluster
Step 7a: The kubectl get namespace -A
command lists all the namespaces in your Kubernetes cluster. This command is useful for getting a quick overview of all the namespaces that exist in your cluster.
Step 8: Create a Namespace - The kubectl create namespace frontend
command creates a new namespace called "frontend" in the Kubernetes cluster. Namespaces are used to organize and manage resources within the cluster, providing a way to divide cluster resources between multiple users (via resource quotas) and to separate environments (e.g., development, staging, production).
Step 8a: The namespace created has been added
Step 9: The kubectl create deployment nginx-deployment --image=nginx:latest --replicas=3 -n frontend
command creates a new deployment named nginx-deployment
in the frontend
namespace, using the latest nginx
image and running three replicas(pods) of the application.
Step 10: The kubectl create deployment nginx-deployment --image=nginx:latest --port=80
command creates a new deployment named nginx-deployment
with the latest NGINX image, and the container will listen on port 80.
Step 11: The kubectl get deployment --namespace default
command provides information about all the deployments in the default
namespace. This is useful for monitoring and managing deployments within a specific namespace.
Step 12: The kubectl expose deployment nginx-deployment --name=nginx-service --type=LoadBalancer --port=80 --protocol=TCP
command creates a new service named nginx-service
that exposes the nginx-deployment
to external traffic on port 80 using a TCP connection. This is useful for making the NGINX deployment accessible from outside the Kubernetes cluster.
Step 13: The kubectl get service
command provides information about all the services in the current namespace. This is useful for monitoring and managing services within a namespace. -NOTE: Copy your external-IP as highlighted in red and pasteit in your browser
Step 13a: Successfully viewed contents on your web browser
Conclusion
Enabling autoscaling and setting up an AKS cluster guarantees that your applications are robust and able to effectively manage changing workloads. You can easily manage your Kubernetes clusters with Visual Studio Code, from setup and deployment to scaling and creation. You have created a stable environment for creating and overseeing containerized apps in Azure Kubernetes Service by following this approach.