Deploy to Azure Kubernetes Service (AKS) with Autoscaling
You will provision a managed AKS cluster, deploy a workload, and enable both cluster and horizontal pod autoscaling. By the end you will run Kubernetes in the cloud without operating the control plane yourself.
Learning Objectives
- Create an AKS cluster with the az CLI.
- Deploy a workload and expose it via a load balancer.
- Enable cluster autoscaler and a Horizontal Pod Autoscaler.
- Time: ~4 hours · Difficulty: Advanced · Prereqs: an Azure subscription and kubectl.
Architecture Overview
graph TD
CLI[az CLI] -->|create| AKS[AKS managed control plane]
AKS --> NP[Node pool autoscaler]
NP --> Pods[Workload pods]
LB[Azure Load Balancer] --> Pods
HPA[HPA] -->|scale on CPU| Pods
Environment Setup
You will need: the az CLI authenticated and kubectl.
Before you begin: AKS incurs cost; delete the resource group when finished.
Step-by-Step Execution
01
Create the cluster with autoscaling nodes
az aks create -g rg -n tyf-aks --node-count 2 --enable-cluster-autoscaler --min-count 2 --max-count 5 --generate-ssh-keysProvisions a managed cluster with an autoscaling node pool.
02
Get credentials and deploy
az aks get-credentials -g rg -n tyf-aks && kubectl create deployment web --image=nginx:stable03
Expose and add an HPA
$ kubectl expose deploy web --type=LoadBalancer --port=80 && kubectl autoscale deploy web --cpu-percent=60 --min=2 --max=10
service/web exposed
horizontalpodautoscaler.autoscaling/web autoscaled
Progress So Far
graph LR
A[01 Create AKS] -->|done| B[02 Deploy]
B -->|done| C[03 Expose + HPA]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
kubectl get svc web -w && kubectl get hpa webYou should get an external IP and an HPA reporting current vs target CPU. If reachable and scaling, your AKS deployment works. Remember to az group delete -n rg to stop charges.
Troubleshooting
- External IP pending: the Azure Load Balancer takes a minute to provision; keep watching.
- HPA shows unknown: install the metrics-server (built in on AKS, may need a moment).
- Quota errors: the region/subscription may cap node SKUs; pick a smaller VM size.
Extension Ideas
- Add Ingress + TLS as in Kubernetes Networking.
- Provision the cluster with Terraform (see IaC).
- Audit posture with CSPM.
Key Results
- Ran managed Kubernetes without operating the control plane.
- Enabled cluster autoscaling from 2 to 5 nodes.
- Added an HPA scaling pods from 2 to 10 on CPU.
- Exposed the app via an Azure Load Balancer.