Deploy Your Application to Azure Kubernetes Service (AKS)

A step-by-step guide to containerizing and orchestrating your application.

This tutorial will walk you through the process of deploying a sample web application to Azure Kubernetes Service (AKS). We'll cover building a Docker image, pushing it to a container registry, and then deploying it to your AKS cluster.

Prerequisites

1

Step 1: Prepare Your Application and Dockerfile

Ensure your application is ready to be containerized. For this tutorial, we'll assume a simple Node.js application. You'll need a Dockerfile in your application's root directory.

Here's an example Dockerfile for a Node.js app:

# Use an official Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install application dependencies
RUN npm install

# Bundle app source
COPY . .

# Make port 8080 available to the world outside this container
EXPOSE 8080

# Define environment variable
ENV NODE_ENV production

# Run app.js when the container launches
CMD [ "node", "server.js" ]
Make sure your application listens on the port specified in the EXPOSE instruction (e.g., 8080).
2

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

If you don't already have an AKS cluster, you'll need to create one. You can do this via the Azure portal or the Azure CLI.

Using Azure CLI:

# Log in to your Azure account
az login

# Set your subscription (replace with your subscription ID)
az account set --subscription "YOUR_SUBSCRIPTION_ID"

# Create a resource group (replace with your desired name and region)
az group create --name myAKSResourceGroup --location eastus

# Create an AKS cluster (replace with your desired cluster name)
az aks create --resource-group myAKSResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys

This command creates a cluster with one node. You can adjust --node-count as needed.

Cluster creation can take several minutes.
3

Step 3: Connect kubectl to Your AKS Cluster

Configure kubectl to connect to your AKS cluster.

# Get the cluster credentials
az aks get-credentials --resource-group myAKSResourceGroup --name myAKSCluster

Verify the connection:

kubectl get nodes
4

Step 4: Create an Azure Container Registry (ACR)

You need a place to store your Docker images. Azure Container Registry (ACR) is a managed, private Docker registry service.

# Create an ACR instance (replace with your desired ACR name, must be globally unique)
az acr create --resource-group myAKSResourceGroup --name myacrregistry --sku Basic --location eastus
5

Step 5: Log in to ACR and Build/Tag Your Docker Image

Log in to your ACR instance from Docker. Then, build your application's Docker image and tag it for ACR.

# Log in to your ACR registry
az acr login --name myacrregistry

# Build the Docker image (replace 'app-name' with a name for your app)
# Ensure you are in the directory containing your Dockerfile and application code
docker build -t myacrregistry.azurecr.io/app-name:v1 .

# Push the image to ACR
docker push myacrregistry.azurecr.io/app-name:v1
6

Step 6: Create Kubernetes Deployment and Service Manifests

We need to define how Kubernetes should run your container and how to expose it.

Deployment Manifest (deployment.yaml)

This manifest tells Kubernetes to run your container image and maintain a desired number of replicas.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 2 # Number of application instances
  selector:
    matchLabels:
      app: app-label
  template:
    metadata:
      labels:
        app: app-label
    spec:
      containers:
      - name: app-container
        image: myacrregistry.azurecr.io/app-name:v1 # Your ACR image
        ports:
        - containerPort: 8080 # The port your app listens on
      imagePullSecrets: # To pull from your private ACR
      - name: acrsecret

Service Manifest (service.yaml)

This manifest exposes your application to the outside world. We'll use a LoadBalancer type for external access.

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    app: app-label # Matches the labels in your Deployment
  ports:
    - protocol: TCP
      port: 80        # Port exposed by the service
      targetPort: 8080 # Port your container listens on
  type: LoadBalancer
7

Step 7: Create Image Pull Secret

Kubernetes needs credentials to pull images from your private ACR. We'll create a secret for this.

# Create a secret for ACR authentication
kubectl create secret docker-registry acrsecret --docker-server=myacrregistry.azurecr.io --docker-username=$(az acr credential show --name myacrregistry --query username --output tsv) --docker-password=$(az acr credential show --name myacrregistry --query passwords[0].value --output tsv) --namespace default
Replace default with the namespace if you are deploying to a different one.
8

Step 8: Apply Manifests to AKS

Use kubectl to deploy your application and expose it.

# Apply the deployment
kubectl apply -f deployment.yaml

# Apply the service
kubectl apply -f service.yaml
9

Step 9: Verify Deployment and Get External IP

Check the status of your deployment and get the external IP address assigned to your service.

# Check deployment status
kubectl get deployments

# Check pod status
kubectl get pods

# Get the external IP for your LoadBalancer service (this may take a few minutes)
kubectl get service app-service

Once the EXTERNAL-IP is assigned (it will change from <pending> to an IP address), you can access your application by navigating to that IP address in your web browser.

Load Balancer provisioning can take a few minutes.

Conclusion

Congratulations! You have successfully deployed your application to Azure Kubernetes Service. You can now manage your application, scale it, and apply advanced Kubernetes configurations.

For more advanced scenarios, explore: