Deploying an Application to Azure Kubernetes Service (AKS)

This article guides you through the process of deploying a sample application to your Azure Kubernetes Service (AKS) cluster. We'll cover creating the necessary Kubernetes manifests and applying them to your cluster.

Prerequisites

Step 1: Create a Kubernetes Deployment

A Kubernetes Deployment manages the desired state of your application. It ensures that a specified number of replicas of your application pods are running at all times. We'll create a YAML file named deployment.yaml.

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

This manifest defines a Deployment named my-nginx-deployment that will run 2 replicas of a pod using the nginx:latest container image. The container will expose port 80.

Step 2: Apply the Deployment to AKS

Use kubectl to apply the deployment manifest to your AKS cluster.

kubectl apply -f deployment.yaml

You should see output similar to:

deployment.apps/my-nginx-deployment created

You can verify the deployment status with:

kubectl get deployments

And check the pods created by the deployment:

kubectl get pods -l app=nginx

Step 3: Create a Kubernetes Service

A Kubernetes Service provides a stable network endpoint to access your application pods. This is crucial because pods are ephemeral and their IP addresses can change. We'll create a Service to expose our Nginx deployment. Create a YAML file named service.yaml.

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

This manifest defines a Service named my-nginx-service.

Step 4: Apply the Service to AKS

Apply the Service manifest to your AKS cluster.

kubectl apply -f service.yaml

You should see output similar to:

service/my-nginx-service created

It may take a few minutes for Azure to provision an external IP address for the LoadBalancer. You can check the status of the Service and its external IP with:

kubectl get service my-nginx-service

Look for the EXTERNAL-IP field. Once it populates with an IP address, you can access your Nginx application by navigating to that IP address in your web browser.

Note: For production environments, consider using Azure Application Gateway or Azure Front Door for more advanced ingress capabilities and traffic management instead of a direct LoadBalancer Service.

Step 5: Access Your Application

Once the external IP address is assigned to the Service, open a web browser and navigate to http://. You should see the default Nginx welcome page.

Clean Up

To remove the deployed application and its associated resources, you can delete the Service and then the Deployment:

kubectl delete service my-nginx-service
kubectl delete deployment my-nginx-deployment

This will release the external IP address and remove the pods and ReplicaSet created by the deployment.

Next Steps

Congratulations! You have successfully deployed a simple application to AKS. From here, you can explore more advanced deployment strategies:

Tip: For more complex applications with multiple services, consider using a GitOps approach with tools like Azure DevOps, GitHub Actions, or Argo CD to automate your deployments.