Azure Kubernetes Service (AKS)

Empower your modern applications

Deploying a Web Application to Azure Kubernetes Service (AKS)

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

Prerequisites

1 Create an Azure Kubernetes Service (AKS) Cluster

First, we need an AKS cluster to deploy our application to. You can create one using the Azure CLI.

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 named myResourceGroup and an AKS cluster named myAKSCluster with one node. The --enable-addons monitoring flag enables the Azure Monitor for containers feature.

Once the cluster is created, configure kubectl to connect to the cluster:

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

2 Create a Dockerfile for Your Application

Assume you have a simple web application. Create a Dockerfile in your application's root directory. Here's an example for a Node.js app:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]

Make sure to replace this with your application's specific requirements.

3 Create an Azure Container Registry (ACR)

ACR is a private registry for storing Docker container images. We'll use it to store our application's image.

az acr create --resource-group myResourceGroup --name myacrregistry --sku Basic --location eastus

Replace myacrregistry with a unique name for your registry.

Login to your ACR:

az acr login --name myacrregistry

4 Build and Push the Docker Image

Tag your Docker image with your ACR login server name and then push it.

# Get your ACR login server name
ACR_LOGIN_SERVER=$(az acr show --name myacrregistry --query loginServer --output tsv)

# Tag the Docker image
docker build -t $ACR_LOGIN_SERVER/mywebapp:v1 .

# Push the Docker image to ACR
docker push $ACR_LOGIN_SERVER/mywebapp:v1

Make sure you are in the directory containing your Dockerfile when running the docker build command.

5 Create Kubernetes Deployment and Service Manifests

Create two YAML files: deployment.yaml and service.yaml.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebapp-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mywebapp
  template:
    metadata:
      labels:
        app: mywebapp
    spec:
      containers:
      - name: mywebapp
        image: $ACR_LOGIN_SERVER/mywebapp:v1
        ports:
        - containerPort: 3000

service.yaml

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

Remember to replace $ACR_LOGIN_SERVER in deployment.yaml with your actual ACR login server name if you didn't set it as an environment variable.

6 Deploy to AKS

Apply the Kubernetes manifests to your AKS cluster:

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

7 Access Your Web Application

Once the service is deployed, it will be assigned an external IP address. You can get this IP address using the following command:

kubectl get service mywebapp-service

Look for the EXTERNAL-IP field. It might take a few minutes for the IP address to be assigned. Once available, open a web browser and navigate to that IP address to see your deployed web application.

Clean Up Resources

To avoid ongoing charges, delete the resource group:

az group delete --name myResourceGroup --yes --no-wait
Explore Advanced Deployment Strategies