Step 1: Prerequisites
Before starting, ensure you have the following:
- Azure Subscription: Sign up for an Azure account at azure.com if you don’t have one.
- Azure CLI: Install the Azure Command-Line Interface (CLI) on your local machine. Download it from here and verify installation by running:
az --version
- kubectl: Install the Kubernetes command-line tool to interact with your cluster. Use the following command:
az aks install-cli
Verify with:
kubectl version --client
- Docker (Optional): If you plan to build and push container images, install Docker Desktop from docker.com.
- Code Editor: Use Visual Studio Code or any editor for writing configuration files.
Log in to Azure CLI:
az login
Follow the browser prompt to authenticate.
Step 2: Create a Resource Group
A resource group is a container that holds related Azure resources.
- Run the following command to create a resource group (replace
<resource-group-name>
and<location>
with your preferred name and Azure region, e.g.,eastus
):
az group create --name <resource-group-name> --location <location>
- Verify the resource group:
az group list --output table
Step 3: Create an AKS Cluster
Now, deploy an AKS cluster within the resource group.
- Use the
az aks create
command to create a cluster. Replace placeholders with your details:
az aks create \
--resource-group <resource-group-name> \
--name <cluster-name> \
--node-count 2 \
--node-vm-size Standard_D2s_v3 \
--generate-ssh-keys
--name
: Unique name for your cluster.--node-count
: Number of nodes (virtual machines) in the cluster (start with 2 for testing).--node-vm-size
: VM size (e.g.,Standard_D2s_v3
for 2 vCPUs, 8GB RAM).--generate-ssh-keys
: Automatically generates SSH keys for cluster access.
- This process may take 5-10 minutes. Monitor progress with:
az aks list --output table
Step 4: Connect to the AKS Cluster
Configure kubectl
to connect to your AKS cluster.
- Get credentials for the cluster:
az aks get-credentials --resource-group <resource-group-name> --name <cluster-name>
- Verify the connection:
kubectl get nodes
You should see a list of nodes in the Ready
state.
Step 5: Deploy a Sample Application
Let’s deploy a simple Nginx web server to test the cluster.
- Create a Deployment:
Create a file namednginx-deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
This defines a deployment with 2 replicas of the Nginx container.
- Apply the deployment:
kubectl apply -f nginx-deployment.yaml
- Expose the Deployment:
Create a service to expose the Nginx app externally:
Createnginx-service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply it:
kubectl apply -f nginx-service.yaml
- Get the external IP:
kubectl get services
Look for the EXTERNAL-IP
under nginx-service
. Once assigned, open this IP in a browser to see the Nginx welcome page.
Step 6: Scale the Application
AKS makes scaling easy.
- Scale the deployment to 3 replicas:
kubectl scale deployment nginx-deployment --replicas=3
- Verify the new replica count:
kubectl get pods
Step 7: Update the Application
Update the Nginx version to test rolling updates.
- Edit
nginx-deployment.yaml
and change the image tonginx:1.19
:
image: nginx:1.19
- Apply the update:
kubectl apply -f nginx-deployment.yaml
- Monitor the rollout status:
kubectl rollout status deployment/nginx-deployment
Step 8: Monitor and Manage the Cluster
Use Azure Monitor and Kubernetes tools to manage your cluster.
- Enable Monitoring (Optional):
During cluster creation, you can enable Azure Monitor with:
az aks create --enable-addons monitoring --resource-group <resource-group-name> --name <cluster-name> ...
Access it via the Azure Portal under your AKS resource.
- Check Cluster Health:
kubectl get nodes
kubectl get pods --all-namespaces
- Delete Resources (When Done):
To avoid charges, delete the cluster and resource group:
az aks delete --resource-group <resource-group-name> --name <cluster-name> --yes
az group delete --name <resource-group-name> --yes
Step 9: Advanced Configuration (Optional)
For production use, consider these enhancements:
- Auto-scaling: Enable the cluster autoscaler:
az aks update --resource-group <resource-group-name> --name <cluster-name> --enable-cluster-autoscaler --min-count 1 --max-count 5
- RBAC and Azure AD Integration: Secure access with role-based access control (RBAC) and Azure Active Directory (AAD). Configure during cluster creation with appropriate flags.
- Private Clusters: Create a private AKS cluster for enhanced security:
az aks create --resource-group <resource-group-name> --name <cluster-name> --enable-private-cluster ...
Step 10: Troubleshooting and Best Practices
- Common Issues:
- If
kubectl get nodes
fails, re-runaz aks get-credentials
. - Ensure your Azure CLI is updated:
az upgrade
. - Best Practices:
- Use namespaces to organize resources:
kubectl create namespace <name>
. - Implement resource limits in deployments to avoid overutilization.
- Regularly back up critical data and configurations.
By following these steps, you’ve successfully set up an AKS cluster, deployed a sample application, and explored basic management tasks. AKS simplifies Kubernetes management by handling control plane maintenance, upgrades, and scaling, allowing you to focus on application development. For production environments, integrate CI/CD pipelines (e.g., Azure DevOps), secure your cluster with network policies, and leverage Azure’s monitoring tools. This tutorial provides a foundation—continue exploring AKS documentation here for advanced features like multi-node pools and GPU support.