Azure Kubernetes Service

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

This tutorial guides you through deploying a simple multi-container application to an Azure Kubernetes Service (AKS) cluster. We'll use a sample application consisting of a web frontend and a backend API, packaged as Docker images.

Prerequisites

Step 1: Prepare the Application

For this tutorial, we'll use a pre-built example application. This application consists of two containers:

  • A simple web application (e.g., Node.js or Python) that displays messages.
  • A backend API (e.g., a basic REST service) that provides the messages to the web application.

You can find sample multi-container applications in the official Kubernetes documentation or GitHub repositories. For this example, let's assume you have two Docker images ready, for instance:

  • myacr.azurecr.io/webapp:v1
  • myacr.azurecr.io/api:v1

If you are building your own, ensure they are built and pushed to a container registry accessible by AKS (like Azure Container Registry).

Step 2: Create Kubernetes Manifests

Kubernetes uses YAML files to define the desired state of your cluster. We need to create manifests for:

  • Deployments: To manage stateless applications (your web app and API).
  • Services: To expose your applications and enable communication between them.

Deployment for the Backend API

Create a file named api-deployment.yaml:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: api
        image: myacr.azurecr.io/api:v1
        ports:
        - containerPort: 8080
        env:
        - name: MESSAGE
          value: "Hello from the API!"
                

Service for the Backend API

Create a file named api-service.yaml:


apiVersion: v1
kind: Service
metadata:
  name: api-service
spec:
  selector:
    app: api
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP
                

Deployment for the Web Frontend

Create a file named webapp-deployment.yaml:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: myacr.azurecr.io/webapp:v1
        ports:
        - containerPort: 80
        env:
        - name: API_URL
          value: "http://api-service:80" # Service name for internal communication
                

Service for the Web Frontend

Create a file named webapp-service.yaml:


apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer # To expose the web application externally
                

Step 3: Apply the Manifests to AKS

Now, use kubectl to apply these YAML files to your AKS cluster.


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

This command will create the specified deployments and services in your cluster. It might take a few minutes for the resources to be provisioned and the pods to start running.

Step 4: Verify the Deployment

Check the status of your deployments and pods:


kubectl get deployments
kubectl get pods
                

You should see both api-deployment and webapp-deployment with the desired number of replicas running.

Check the status of your services:


kubectl get services
                

Look for the webapp-service. It will have an EXTERNAL-IP assigned once the LoadBalancer is provisioned by Azure. This might take a few minutes.

Step 5: Access the Application

Once the webapp-service has an external IP address, you can access your multi-container application by opening that IP address in your web browser.

If the external IP is still pending, you can retry the kubectl get services command after a few minutes.

Remember to replace myacr.azurecr.io/api:v1 and myacr.azurecr.io/webapp:v1 with your actual container image names and tags.

Cleaning Up

To remove the deployed resources, run the following commands:


kubectl delete service webapp-service
kubectl delete deployment webapp-deployment
kubectl delete service api-service
kubectl delete deployment api-deployment
            

This tutorial covered the basics of deploying a multi-container application. For more complex scenarios, consider using Helm charts for packaging and managing your Kubernetes applications.