Deploy a Container to Azure Kubernetes Service (AKS)

Overview

This tutorial walks you through the end‑to‑end process of deploying a Docker container to an Azure Kubernetes Service (AKS) cluster. You'll learn how to:

  • Create an AKS cluster using the Azure CLI.
  • Build a Docker image locally.
  • Push the image to Azure Container Registry (ACR).
  • Create a Kubernetes deployment and expose it via a Service.

Prerequisites

  • Azure subscription (free trial works).
  • Azure CLI 2.0+ installed and logged in (az login).
  • Docker Desktop or Docker Engine installed.
  • kubectl installed (included with az aks install-cli).

Create an AKS Cluster

Run the following commands in a terminal. Adjust the resource group and cluster name to suit your naming conventions.

# Create a resource group
az group create --name myAKSResourceGroup --location eastus

# Create an AKS cluster (3 nodes, default VM size)
az aks create \
    --resource-group myAKSResourceGroup \
    --name myAKSCluster \
    --node-count 3 \
    --enable-addons monitoring \
    --generate-ssh-keys

# Get credentials for kubectl
az aks get-credentials --resource-group myAKSResourceGroup --name myAKSCluster

Verify the cluster is reachable:

kubectl get nodes

Build & Push Docker Image to Azure Container Registry

First, create an ACR instance:

az acr create \
    --resource-group myAKSResourceGroup \
    --name myacr$(date +%s) \
    --sku Basic

Log in to ACR and build your image (replace myapp with your app name):

# Log in to ACR
az acr login --name myacr$(date +%s)

# Build the Docker image
docker build -t myacr$(date +%s).azurecr.io/myapp:v1 .

# Push the image
docker push myacr$(date +%s).azurecr.io/myapp:v1

Make sure to replace myacr$(date +%s) with the actual registry name shown after creation.

Deploy to AKS

Create a Kubernetes deployment that pulls the image from ACR:

cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myacr$(date +%s).azurecr.io/myapp:v1
        ports:
        - containerPort: 80
EOF

kubectl apply -f deployment.yaml

Expose the deployment via a LoadBalancer service:

kubectl expose deployment myapp-deployment \
    --type=LoadBalancer \
    --port=80 \
    --target-port=80

Obtain the external IP address (may take a minute):

kubectl get service myapp-deployment

Visit the displayed EXTERNAL-IP in your browser to see the running container.

Cleanup Resources

When you're finished, delete the resources to avoid charges:

az group delete --name myAKSResourceGroup --yes --no-wait