Azure Kubernetes Service (AKS) Documentation

Deploy a Multi-Container Application to Azure Kubernetes Service (AKS)

This tutorial guides you through deploying a multi-container application to an Azure Kubernetes Service (AKS) cluster. You'll learn how to define your application using Kubernetes manifests and deploy it using kubectl.

Prerequisites

Tip: Ensure your Azure CLI is logged in and your AKS cluster context is set correctly in kubectl. You can verify this with az account show and kubectl config current-context.

1. Prepare Your Application Images

For this tutorial, we'll use a simple two-container application: a web front-end and a Redis cache. You can use your own application images, but ensure they are accessible from your AKS cluster (e.g., pushed to Azure Container Registry or Docker Hub).

For demonstration, let's assume you have two Docker images:

If you are building your own images, you'll need Dockerfiles for each container. Once built, push them to a container registry.

2. Create Kubernetes Manifests

Kubernetes manifests are YAML files that describe the desired state of your application's resources.

2.1 Create a Deployment for the Redis Cache

This deployment will create a Pod running the Redis container.

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


apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-cache
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:alpine
        ports:
        - containerPort: 6379
            

2.2 Create a Service for the Redis Cache

A Service provides stable network access to your Redis Pods.

Create a file named redis-service.yaml with the following content:


apiVersion: v1
kind: Service
metadata:
  name: redis-cache-service
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
            

2.3 Create a Deployment for the Front-end Application

This deployment will create Pods running your web front-end. We'll configure it to connect to the Redis cache via its Service name.

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


apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-app
  labels:
    app: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: myacr.azurecr.io/frontend:v1 # Replace with your image
        ports:
        - containerPort: 80
        env:
        - name: REDIS_HOST
          value: "redis-cache-service" # The name of the Redis service
        - name: REDIS_PORT
          value: "6379"
            

2.4 Create a Service for the Front-end Application

This Service will expose your front-end application to be accessible from outside the cluster.

Create a file named frontend-service.yaml with the following content:


apiVersion: v1
kind: Service
metadata:
  name: frontend-service
spec:
  selector:
    app: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer # Exposes the service externally
            
Note: Using type: LoadBalancer for the front-end service will provision an Azure Load Balancer, incurring costs. For production, consider using Ingress controllers for more advanced traffic management.

3. Deploy the Application to AKS

Now, use kubectl to apply these manifests to your AKS cluster.

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

Verify the deployments and services:

kubectl get deployments
kubectl get services

4. Access Your Application

Once the front-end service is provisioned with a public IP address (this might take a few minutes), you can access your application.

Retrieve the external IP address of the front-end service:

kubectl get service frontend-service

Look for the EXTERNAL-IP value in the output. Open a web browser and navigate to that IP address.

5. Clean Up Resources

To avoid ongoing charges, delete the deployed resources when you're finished.

kubectl delete service frontend-service
kubectl delete deployment frontend-app
kubectl delete service redis-cache-service
kubectl delete deployment redis-cache

Next Steps

Congratulations! You have successfully deployed a multi-container application to AKS. Consider exploring: