AKS
Azure Kubernetes Service (AKS) Tutorial
Azure Kubernetes Service (AKS) is a managed Kubernetes service that simplifies deploying, managing, and scaling containerized applications in Azure. This guide covers creating an AKS cluster, deploying an application, and managing resources.
Table of Contents
- Introduction to AKS
- Prerequisites
- Creating an AKS Cluster
- Connecting to the AKS Cluster
- Deploying an Application to AKS
- Scaling and Updating the Application
- Monitoring and Logging
Introduction to AKS
AKS is a managed Kubernetes service that handles the Kubernetes control plane, allowing you to focus on deploying and managing applications without worrying about infrastructure.
Benefits of AKS
- Managed Control Plane: Azure manages Kubernetes master node operations.
- Scaling: Automatically scales nodes and pods based on demand.
- Integrated Security: Integrates with Azure Active Directory for secure access.
- Monitoring: Built-in Azure Monitor integration for tracking performance.
Prerequisites
- Azure CLI: Install the Azure CLI if you haven’t already. Download it here.
- Azure Subscription: Ensure you have an active Azure subscription.
Login to Azure
Log in to your Azure account using the Azure CLI:
az login
Creating an AKS Cluster
- Set Environment Variables (optional):
RESOURCE_GROUP=myResourceGroup
CLUSTER_NAME=myAKSCluster
LOCATION=eastus
- Create a Resource Group:
az group create --name $RESOURCE_GROUP --location $LOCATION
- Create the AKS Cluster:
az aks create --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --node-count 3 --enable-addons monitoring --generate-ssh-keys
This command creates a 3-node AKS cluster with monitoring enabled.
Connecting to the AKS Cluster
- Install
kubectl
(if not already installed):
az aks install-cli
- Connect to the Cluster:
az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME
- Verify Connection:
kubectl get nodes
Deploying an Application to AKS
- Create a Deployment:
kubectl create deployment my-app --image=nginx
- Expose the Deployment:
kubectl expose deployment my-app --port=80 --type=LoadBalancer
- Check the Service IP:
kubectl get service my-app
Scaling and Updating the Application
- Scale the Deployment:
kubectl scale deployment my-app --replicas=5
- Update the Deployment (e.g., change the image):
kubectl set image deployment/my-app nginx=nginx:1.19.0
- Check the Status of the Update:
kubectl rollout status deployment/my-app
Monitoring and Logging
Enabling AKS Monitoring
When creating the AKS cluster with the --enable-addons monitoring
flag, AKS integrates with Azure Monitor.
-
View Cluster Metrics: Go to the Azure Portal, navigate to your AKS cluster, and select Insights under Monitoring to view performance metrics and set alerts.
-
Access Logs: Logs for containers and nodes are available through Azure Monitor for troubleshooting and resource management.
Sample Azure Devops Yaml file
trigger:
- master # Adjust according to your branch name
variables:
imageName: nippy/myimage # The base name for the Docker image
imageTag: $(Build.BuildId) # Automatically uses the build ID as the image tag
jobs:
- job: BuildAndDeploy
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Docker@2
inputs:
containerRegistry: 'dockerhub' # Reference to your Docker Hub service connection
repository: '$(imageName)' # Image name to build and push
command: 'buildAndPush' # Command to build and push the Docker image
Dockerfile: '**/Dockerfile' # Path to your Dockerfile
tags: '$(imageTag)' # Tag for the Docker image, using the build ID
- script: |
ls -l # List files to verify the environment
echo "Building Kubernetes deployment manifest..."
sed "s|{{imageTag}}|$(imageTag)|g" deployment.yaml > deployment_replaced.yaml # Replace placeholder with actual image tag
cat deployment_replaced.yaml
displayName: 'Replace image tag in deployment.yaml'
- task: Kubernetes@1
inputs:
connectionType: 'Azure Resource Manager' # Using Azure Resource Manager for connection
azureSubscriptionEndpoint: 'Free Trial(1)(98391885-1dc7-4963-93d0-7590a267b3f7)' # Your Azure subscription
azureResourceGroup: 'azure-devops' # Resource group containing the AKS cluster
kubernetesCluster: 'dev09' # Name of the AKS cluster
namespace: 'default' # Kubernetes namespace to deploy into
command: 'apply' # Command to apply the configuration
useConfigurationFile: true # Indicate that a configuration file will be used
configuration: 'deployment_replaced.yaml' # The updated deployment file to apply
secretType: 'dockerRegistry' # Type of secret for authentication
containerRegistryType: 'Azure Container Registry' # Type of container registry being used