Azure Kubernetes Service (AKS) Tutorial
Introduction
Azure Kubernetes Service (AKS) is a managed container orchestration service based on the open-source Kubernetes system, which is available on the Microsoft Azure public cloud. AKS simplifies the deployment and management of a Kubernetes cluster in Azure by offloading much of the operational overhead to Azure, such as health monitoring and maintenance.
Prerequisites
Before you start, ensure you have the following:
- An Azure account with an active subscription.
- Azure CLI installed on your local machine.
- Basic understanding of Kubernetes concepts.
Step 1: Install Azure CLI
You can install the Azure Command-Line Interface (CLI) on various operating systems. Here is how you can do it:
Windows:
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -ArgumentList '/I AzureCLI.msi /quiet' -Wait; Remove-Item .\AzureCLI.msi
macOS:
brew update && brew install azure-cli
Linux:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Step 2: Log in to Azure
Once the Azure CLI is installed, log in to your Azure account using the following command:
az login
This command will open a web browser and prompt you to enter your Azure account credentials. After successful authentication, you will see a list of your subscriptions in the CLI.
Step 3: Create a Resource Group
A resource group is a container that holds related resources for an Azure solution. Create a resource group using the following command:
az group create --name myResourceGroup --location eastus
Step 4: Create an AKS Cluster
Now, create an AKS cluster using the following command:
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
This command will take a few minutes to complete. It will create a Kubernetes cluster named myAKSCluster in the myResourceGroup resource group with one node. Additionally, it enables the monitoring add-on and generates SSH keys for the cluster.
Step 5: Connect to the AKS Cluster
Once the AKS cluster is created, you need to configure kubectl to connect to the cluster. Use the following command:
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
This command merges the cluster's configuration with your existing Kubernetes configuration file, allowing you to manage the cluster using kubectl.
Step 6: Deploy an Application
To deploy an application to your AKS cluster, create a deployment manifest file named azure-vote.yaml with the following content:
apiVersion: apps/v1 kind: Deployment metadata: name: azure-vote-back spec: replicas: 1 selector: matchLabels: app: azure-vote-back template: metadata: labels: app: azure-vote-back spec: containers: - name: azure-vote-back image: redis ports: - containerPort: 6379 --- apiVersion: apps/v1 kind: Deployment metadata: name: azure-vote-front spec: replicas: 1 selector: matchLabels: app: azure-vote-front template: metadata: labels: app: azure-vote-front spec: containers: - name: azure-vote-front image: mcr.microsoft.com/azuredocs/azure-vote-front:v1 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: azure-vote-back spec: ports: - port: 6379 selector: app: azure-vote-back --- apiVersion: v1 kind: Service metadata: name: azure-vote-front spec: type: LoadBalancer ports: - port: 80 selector: app: azure-vote-front
Apply this manifest file using the following command:
kubectl apply -f azure-vote.yaml
This command will create a Redis deployment, a front-end deployment, and their respective services.
Step 7: Verify the Deployment
Check the status of your deployments using the following commands:
kubectl get deployments
kubectl get services
For example, the output of kubectl get services should be similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE azure-vote-back ClusterIP 10.0.0.706379/TCP 5m azure-vote-front LoadBalancer 10.0.0.158 52.170.207.71 80:31448/TCP 5m
The External IP for azure-vote-front will allow you to access your application using a web browser.
Conclusion
Congratulations! You have successfully deployed an application to Azure Kubernetes Service (AKS). This tutorial covered the basics of setting up an AKS cluster, deploying an application, and verifying the deployment. For more advanced topics, refer to the official Azure documentation on AKS.