Azure Documentation

Deploying Applications to Azure Kubernetes Service (AKS)

This tutorial guides you through the process of deploying a sample application to an Azure Kubernetes Service (AKS) cluster. We'll cover creating an AKS cluster, building a container image, and deploying it using Kubernetes manifests.

Prerequisites

Before you begin, ensure you have the following installed:

You'll also need an Azure subscription. If you don't have one, you can sign up for a free account.

Step 1: Create an Azure Kubernetes Service (AKS) Cluster

First, let's create an AKS cluster. Replace myResourceGroup and myAKSCluster with your desired names.


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

This command creates a resource group and then an AKS cluster within that group. The cluster will have one node pool with one node.

Step 2: Connect kubectl to your AKS Cluster

Once the cluster is created, configure kubectl to connect to your cluster. Replace myResourceGroup and myAKSCluster if you used different names.


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

Verify the connection by listing the nodes in your cluster:


kubectl get nodes
                

Step 3: Prepare Your Application

For this tutorial, we'll use a simple Nginx web server. First, create a directory for your application files:


mkdir aks-nginx-app
cd aks-nginx-app
                

Create a file named nginx.conf with the following content:


http {
    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
}
                

Create an index.html file:


<!DOCTYPE html>
<html>
<head>
    <title>Welcome to AKS!</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
        h1 { color: #0078d4; }
    </style>
</head>
<body>
    <h1>Hello from Azure Kubernetes Service!</h1>
    <p>This is a sample application deployed via Kubernetes.</p>
</body>
</html>
                

Step 4: Build and Push a Docker Image

Create a Dockerfile in the same directory:


FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
                

Build the Docker image. Replace youracrname with the name of your Azure Container Registry (ACR) or Docker Hub username.


docker build -t youracrname/aks-nginx-app:v1 .
                

Log in to your container registry. If you're using Docker Hub, use docker login. If using ACR, follow the ACR authentication guide.

Push the image:


docker push youracrname/aks-nginx-app:v1
                

Step 5: Create Kubernetes Deployment and Service Manifests

Create a file named aks-nginx-deployment.yaml:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: aks-nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: aks-nginx
  template:
    metadata:
      labels:
        app: aks-nginx
    spec:
      containers:
      - name: aks-nginx
        image: youracrname/aks-nginx-app:v1
        ports:
        - containerPort: 80
                

Create a file named aks-nginx-service.yaml:


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

Note: Ensure you replace youracrname/aks-nginx-app:v1 with your actual image name.

Step 6: Deploy to AKS

Apply the Kubernetes manifests to your AKS cluster:


kubectl apply -f aks-nginx-deployment.yaml
kubectl apply -f aks-nginx-service.yaml
                

Check the status of your deployment and pods:


kubectl get deployments
kubectl get pods
                

Check the status of your service and get the external IP address:


kubectl get service aks-nginx-service
                

It might take a few minutes for the external IP address to be assigned. Once available, you can access your application by navigating to that IP in your web browser.

Next Steps: Explore more advanced deployment strategies, such as using Helm for packaging applications, or setting up CI/CD pipelines for automated deployments.

Clean Up Resources

To avoid ongoing charges, you can delete the resource group containing your AKS cluster and associated resources:


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