Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 57 ยท Containers

Azure Kubernetes Service

Understand AKS: managed control plane, node pools, kubectl, Deployments, Services and Ingress, cluster upgrades, and when to pick AKS over Container Apps.

Kubernetes is the most widely used system for running many containers across many machines. You describe what you want ("run three copies of this image and expose them on port 80"), and Kubernetes keeps reality matching that description: it restarts crashed containers, spreads them over machines and rolls out new versions step by step. The price is complexity. Running the cluster itself, with its API server, its data store and its certificates, is a job in its own right.

Azure Kubernetes Service (AKS) removes that job. Azure operates the cluster's brain for you, and you manage only the machines your workloads run on, plus the workloads themselves. This lesson explains how an AKS cluster is built, the handful of kubectl commands you need first, the three objects almost every app uses, how upgrades work, and how to decide between AKS and Azure Container Apps. Creating a cluster needs an account, so those commands and manifests are read-only. The runnable examples are JavaScript stand-ins for the central idea of Kubernetes: a controller that compares desired state with actual state, again and again.

Control plane and node pools

An AKS cluster has two halves.

The control plane is run by Azure. It contains the Kubernetes API server (the front door for every command), the scheduler (which decides where each pod runs), the controllers (the loops that keep things matching your description) and the etcd data store (the cluster's memory). You cannot log into these machines. In the free tier there is no charge for the control plane itself; paid tiers add a financially backed uptime SLA and more options. You always pay for the nodes.

The nodes are ordinary virtual machines that run your containers. They are grouped into node pools, where every node in a pool has the same size and settings. Node pools are backed by virtual machine scale sets (see Scaling and Autoscale).

  • A system node pool hosts critical cluster components such as DNS. Every cluster needs at least one.
  • User node pools host your applications. You might have one with ordinary VMs, one with GPU machines and one with cheap spot VMs, each scaling separately.
flowchart TB
  subgraph CP["Control plane (managed by Azure)"]
    API["API server"]
    SCH["Scheduler"]
    CTL["Controllers"]
    ETCD["etcd"]
  end
  subgraph SYS["System node pool"]
    S1["node: CoreDNS, metrics"]
  end
  subgraph USR["User node pool"]
    U1["node: your pods"]
    U2["node: your pods"]
  end
  API --> SYS
  API --> USR

Azure runs the top box; you size and pay for the node pools below it.

Each node pool can use the cluster autoscaler, which adds nodes when pods cannot be scheduled for lack of room and removes nodes that sit mostly empty. Create a small cluster like this (read-only):

Terminal
az aks create \
  --resource-group containers-rg \
  --name shop-aks \
  --node-count 2 \
  --node-vm-size Standard_D2s_v5 \
  --enable-managed-identity \
  --attach-acr contososhopacr \
  --generate-ssh-keys

az aks nodepool add \
  --resource-group containers-rg \
  --cluster-name shop-aks \
  --name apps \
  --node-count 2 \
  --enable-cluster-autoscaler --min-count 2 --max-count 6

--attach-acr gives the cluster permission to pull from your registry (see Azure Container Registry). Azure also creates a second, automatically named resource group for the cluster's virtual machines, disks and load balancers; avoid editing those resources by hand.

kubectl basics

kubectl is the command-line client for the Kubernetes API. Azure CLI downloads the credentials into your local configuration:

Terminal
az aks get-credentials --resource-group containers-rg --name shop-aks

kubectl get nodes
kubectl get pods --all-namespaces
kubectl apply -f deployment.yaml
kubectl describe pod web-7d9f6c5b8-abcde
kubectl logs deployment/web
kubectl scale deployment/web --replicas=5
kubectl rollout status deployment/web
kubectl rollout undo deployment/web

apply is the central command: you hand Kubernetes a file that describes the desired state, and it works out what to change. Running it twice with the same file changes nothing, which is the same idempotent habit you will meet in Bicep and ARM Templates.

Deployment, Service and Ingress

Three objects cover most web apps. Together they are one YAML file with documents separated by --- (read-only here).

A Deployment says which image to run and how many copies (replicas). Each copy is a pod, the smallest unit Kubernetes schedules.

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: contososhopacr.azurecr.io/shop/web:1.0
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              memory: 256Mi

A Service gives a stable address and load-balances across all ready pods that carry a matching label, because pod IP addresses change whenever pods are replaced.

YAML
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

With the default type (ClusterIP) the Service is reachable only inside the cluster. Type LoadBalancer makes Azure create a public load balancer for it (compare Load Balancer, Application Gateway and Front Door).

An Ingress routes HTTP requests by host name and path to Services, so many apps can share one public address. AKS offers a managed NGINX-based add-on called application routing for this:

YAML
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
spec:
  ingressClassName: webapprouting.kubernetes.azure.com
  rules:
    - host: shop.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 80

The reconcile loop: a stand-in

The real controllers are part of Kubernetes and cannot run here. The stand-in below keeps the idea. A Deployment object holds the desired replica count and image. On every "tick" a controller observes the pods, compares them with the desired state and makes one round of changes. A new pod takes one tick to become ready. You will see it replace a pod that was killed, roll out a new image with maxSurge: 1 and maxUnavailable: 0 (so capacity never drops), and then scale up.

JS A Deployment controller reconciling pods

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the rolling-update rows: a new pod appears, becomes ready on the next tick, and only then does an old pod disappear. The pod count never drops below three ready pods. This is also why a readiness probe matters in the real YAML: Kubernetes only counts a pod as ready, and only sends it traffic, once the probe passes.

Selecting pods by label: a stand-in

A Service is nothing more than "find the ready pods whose labels match, and spread requests over them". The second stand-in does exactly that, with round-robin choice, and shows what happens when a pod is not ready or its label does not match.

JS A Service picking pods by label

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Upgrades

Kubernetes releases a new minor version regularly, and each version is supported for a limited time, so a cluster must be upgraded. In AKS you upgrade the control plane and the node pools; you may move only one minor version at a time.

Terminal
az aks get-upgrades --resource-group containers-rg --name shop-aks --output table
az aks upgrade --resource-group containers-rg --name shop-aks \
  --kubernetes-version <next-version>

During a node pool upgrade AKS adds a surge node, cordons and drains an old node (pods move elsewhere), replaces it with an upgraded one, and repeats. Your Deployments need several replicas and readiness probes to survive that, plus a PodDisruptionBudget if you want to limit how many pods may be down at once. You can also set an auto-upgrade channel so patch or minor upgrades happen on their own, and a maintenance window so they happen at quiet hours. Test upgrades in a non-production cluster first.

AKS or Container Apps?

Container Apps AKS
Kubernetes API Hidden Full access (kubectl, Helm, operators)
Scaling to zero Built in Possible with add-ons; not the default
Operations Azure handles nearly everything You manage node pools, upgrades, networking and add-ons
Best for HTTP APIs, workers, event-driven apps, small teams Large platforms, custom controllers, complex networking, teams that already know Kubernetes

A good rule: start with Container Apps or App Service, and move to AKS when you hit a limit you can name, such as needing a specific operator or very fine scheduling control. Choosing a Compute Service compares all the options.

Operating a cluster: stop and start, zones, tiers and diagnostics

Stopping a cluster you are not using

A development cluster that nobody uses at night still pays for its nodes. az aks stop shuts down the control plane and all node pools while keeping the cluster's configuration; az aks start brings it back, and your workloads restart from their stored definitions. Compute charges stop while it is stopped, but disks, public IPs and load balancers still cost money, and a stopped cluster cannot be reached, so do not use it for anything that must stay up. Use it for dev and test clusters, ideally on a schedule.

Terminal
az aks stop  --resource-group rg-aks --name aks-demo
az aks show  --resource-group rg-aks --name aks-demo --query powerState.code -o tsv
az aks start --resource-group rg-aks --name aks-demo

Availability zones and tiers

Spreading nodes across availability zones keeps the application running if one datacenter fails (see Reliability and Disaster Recovery). Choose zones when you create a node pool; pods are then spread over the zones, but only if your Deployments say so (use topology spread constraints or multiple replicas with anti-affinity). Disks are zonal, so a pod with a persistent volume can only run in that disk's zone unless you use zone-redundant storage.

Terminal
az aks nodepool add --resource-group rg-aks --cluster-name aks-demo \
  --name userpool --node-count 3 --zones 1 2 3

The cluster tier controls the control-plane commitment: Free (no financially backed SLA, for experiments), Standard (a financially backed uptime SLA, higher with zones, more control-plane capacity) and Premium (adds long-term support for a Kubernetes version). Check the current definitions on the pricing page, but the rule is simple: production clusters should not be on Free.

Diagnosing a sick workload

Work from the pod outward with kubectl; the status tells you where to look.

Terminal
kubectl get pods -o wide
kubectl describe pod web-7d9f           # events at the bottom explain scheduling and pull errors
kubectl logs web-7d9f --previous        # logs of the crashed previous container
kubectl get events --sort-by=.lastTimestamp
kubectl top nodes && kubectl top pods   # resource use (needs metrics server, included in AKS)
kubectl debug node/aks-nodepool1-123 -it --image=busybox

In the portal, a cluster's Diagnose and solve problems page runs automated checks for common failures (node problems, upgrades, networking, DNS), and Container insights and Prometheus-based monitoring give metrics and logs (see Azure Monitor, Log Analytics and Application Insights). Clusters outside Azure can be projected into Azure with Arc-enabled Kubernetes so one set of policies and dashboards covers them (see Azure Arc, Lighthouse and Hybrid Management).

A pod-status triage: a stand-in

JS First step for common pod states

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In AKS, who operates the API server and etcd?

    The control plane is managed by Azure. You manage the node pools and your workloads.
  2. 2. What does a Kubernetes Service provide?

    Pod addresses change as pods are replaced. A Service gives a stable name and IP and balances across the ready pods that match its selector.
  3. 3. Why is maxUnavailable: 0 combined with maxSurge: 1 useful in a rolling update?

    With no unavailable pods allowed and one extra pod allowed, Kubernetes adds a new pod, waits until it is ready, then removes an old one.
  4. 4. Which node pool must every AKS cluster have?

    System node pools host critical components such as DNS. User node pools are optional extras for your applications.
  5. 5. When is Azure Container Apps usually a better choice than AKS?

    Container Apps hides Kubernetes and handles scaling and upgrades. AKS is the right tool when you need the full Kubernetes feature set.

Summary

  • AKS gives you a Kubernetes cluster whose control plane (API server, scheduler, controllers, etcd) is run by Azure; you run node pools of VMs.
  • A system node pool hosts cluster components; user node pools host your apps and can autoscale.
  • az aks get-credentials configures kubectl; kubectl apply hands Kubernetes a desired state.
  • A Deployment keeps N pods running and rolls out new versions gradually; a Service load-balances across ready pods by label; an Ingress routes HTTP by host and path.
  • Kubernetes works by controllers that repeatedly compare desired and actual state.
  • Upgrade one minor version at a time, with several replicas, probes and ideally a maintenance window.
  • Prefer Container Apps or App Service until you need what only full Kubernetes offers.

Next: Bicep and ARM Templates.