Azure Kubernetes Service (AKS) Deployment Tutorial

Introduction to AKS Deployment

This tutorial will guide you through the process of deploying a containerized application to Azure Kubernetes Service (AKS). You will learn how to create an AKS cluster, deploy your application, expose it as a service, and manage its lifecycle.

Kubernetes has become the de facto standard for container orchestration, and AKS provides a fully managed Kubernetes experience in Azure, simplifying the deployment and management of containerized applications.

1 Prerequisites

Before you begin, ensure you have the following:

  • An Azure account with an active subscription. If you don't have one, you can create a free account.
  • The Azure CLI installed and configured. You can find installation instructions here.
  • kubectl installed. You can install it using the Azure CLI:
    az aks install-cli
  • A containerized application ready to be deployed. For this tutorial, we'll assume you have a simple web application container image.
Note: Ensure your Azure CLI is logged in to your desired subscription using az login.

2 Create an AKS Cluster

First, we need to create an Azure Kubernetes Service cluster. This involves creating a resource group and then deploying the AKS cluster within it.

Create a Resource Group

Choose a region and a name for your resource group. For example:


az group create --name myResourceGroup --location eastus
                

Create the AKS Cluster

Now, create the AKS cluster. This command creates a cluster named myAKSCluster in the specified resource group. You can customize the node count and VM size as needed.


az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
                

This process can take several minutes to complete.

Connect kubectl to the Cluster

Once the cluster is created, you need to configure kubectl to connect to it. The following command retrieves the cluster's credentials:


az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
                

You can verify the connection by listing the nodes in your cluster:


kubectl get nodes
                

3 Deploy Your Application

To deploy your application, you'll create a Kubernetes Deployment object. This object defines how your application pods should be created and managed.

Create a Deployment Manifest (YAML)

Create a file named app-deployment.yaml with the following content. Replace your-dockerhub-username/your-app-image:latest with your actual container image.


apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: your-dockerhub-username/your-app-image:latest
        ports:
        - containerPort: 80
                
Tip: For a quick test, you can use a public image like mcr.microsoft.com/azuredocs/azure-vote-frontend:v1.

Apply the Deployment

Apply the deployment manifest to your AKS cluster:


kubectl apply -f app-deployment.yaml
                

Verify the Deployment

Check the status of your deployment and pods:


kubectl get deployments
kubectl get pods
                

4 Expose Your Application as a Service

To make your application accessible from outside the cluster, you need to create a Kubernetes Service. We'll create a LoadBalancer service, which provisions an Azure load balancer.

Create a Service Manifest (YAML)

Create a file named app-service.yaml with the following content:


apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
                

Apply the Service

Apply the service manifest to your AKS cluster:


kubectl apply -f app-service.yaml
                

Get the External IP Address

It will take a few minutes for the Azure load balancer to be provisioned and assigned an external IP address. You can check the status of your service:


kubectl get service my-app-service
                

Look for the EXTERNAL-IP value. Once it's assigned, you can access your application by navigating to that IP address in your web browser.

5 Manage and Update Your Deployment

Kubernetes provides robust mechanisms for managing and updating your applications.

Scaling Your Deployment

You can easily scale the number of application replicas:


kubectl scale deployment my-app-deployment --replicas=5
                

Verify the change:


kubectl get pods
                

Updating Your Application Image

To update your application to a new version, update the image tag in your deployment manifest and re-apply it. For instance, if you have a new image your-dockerhub-username/your-app-image:v2:

  1. Edit app-deployment.yaml and change the image to your-dockerhub-username/your-app-image:v2.
  2. Apply the updated manifest:
    
    kubectl apply -f app-deployment.yaml
                            

Kubernetes will perform a rolling update, gradually replacing old pods with new ones, ensuring minimal downtime.

Rolling Back a Deployment

If an update causes issues, you can easily roll back to a previous version:


kubectl rollout undo deployment my-app-deployment
                

You can view deployment history with kubectl rollout history deployment my-app-deployment.

6 Next Steps and Cleanup

Congratulations! You have successfully deployed and managed an application on Azure Kubernetes Service.

Consider exploring these advanced topics:

  • Ingress Controllers for more sophisticated traffic management.
  • Persistent Storage for stateful applications.
  • Monitoring and Logging with Azure Monitor for AKS.
  • CI/CD integration for automated deployments.

Cleanup Resources

To avoid ongoing charges, it's important to delete the resources you created. You can delete the entire resource group:


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