Azure Developers

Deploying an Application to Azure Kubernetes Service (AKS)

This tutorial will guide you through the process of deploying a sample application to an Azure Kubernetes Service (AKS) cluster. We'll cover creating a deployment, exposing it with a Service, and verifying the deployment.

Prerequisites

Step 1: Prepare Your Application

For this tutorial, we'll use a simple Nginx web server as our sample application. You'll need a Docker image for your application. You can use a public image like nginx or build your own.

If you want to use your own custom application, ensure it's containerized and available as a Docker image in a registry (like Azure Container Registry or Docker Hub).

Step 2: Create a Deployment

A Kubernetes Deployment provides declarative updates for Pods and ReplicaSets. It allows you to describe the desired state for your application, and the Deployment Controller will manage changes to bring the current state to the desired state.

Create a file named nginx-deployment.yaml with the following content:


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

Apply this deployment to your AKS cluster using kubectl:

kubectl apply -f nginx-deployment.yaml

Step 3: Verify the Deployment

Check the status of your deployment and the Pods it has created:

kubectl get deployments
kubectl get pods

You should see your nginx-deployment with 3 available replicas and 3 running Pods.

Step 4: Expose Your Application with a Service

A Kubernetes Service provides a stable IP address and DNS name for a set of Pods. It acts as a load balancer, directing traffic to the Pods that match its selector.

Create a file named nginx-service.yaml with the following content. We'll use a LoadBalancer type to get an external IP address:


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

Apply this service to your AKS cluster:

kubectl apply -f nginx-service.yaml

Step 5: Get the External IP Address

Once the Service is created, it will provision an Azure Load Balancer. You can get its external IP address using:

kubectl get services nginx-service

Wait for the EXTERNAL-IP column to show an IP address (it might take a few minutes).

Tip: If you're using a free Azure tier for your AKS cluster, provisioning a LoadBalancer service might incur costs. For testing, you can use a NodePort service type initially and access it via your node's IP and port.

Step 6: Access Your Application

Open a web browser and navigate to the EXTERNAL-IP address you obtained in the previous step. You should see the default Nginx welcome page.

Step 7: Clean Up Resources (Optional)

To remove the deployment and service, use the following commands:

kubectl delete deployment nginx-deployment
kubectl delete service nginx-service
Note: Deleting the service will also deprovision the Azure Load Balancer, releasing the external IP address.

Congratulations! You have successfully deployed an application to Azure Kubernetes Service and exposed it to the internet.