Skip to content
All posts

Deploying Azure Container Instances

A Step-by-Step Guide to Deploying and Scaling Containerized Applications on Microsoft Azure

shutterstock_90515209

Introduction

Containerization has revolutionized the way developers build, package, and deploy applications. With containers, you can ensure consistency, portability, and isolation across different environments and platforms. Microsoft Azure, a leading cloud service provider, offers a range of services to help you deploy and manage containerized applications, including Azure Container Instances (ACI), Azure Kubernetes Service (AKS), and Azure Container Registry (ACR). In this blog post, we will walk you through the process of deploying containerized applications to Azure using these services and explore best practices for scaling and monitoring your applications in the cloud.

Prerequisites

Before deploying containerized applications to Azure, you should have:

  1. A basic understanding of containers, Docker, and container orchestration.
  2. An Azure account and the necessary permissions to create and manage resources.
  3. The Azure CLI installed on your local machine or access to the Azure Cloud Shell.
  4. Docker installed on your local machine to create and manage container images.

 

Step 1: Create a Container Image

The first step in deploying a containerized application to Azure is to create a container image. A container image is a snapshot of your application and its dependencies, packaged together in a single unit. You can create a container image using Docker and a Dockerfile, which is a script that contains instructions for building the image.

Here's a simple example of a Dockerfile for a Node.js application:

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "start"]

To build the container image, navigate to the directory containing the Dockerfile and run the following command:

docker build -t <image_name> .

Replace <image_name> with a name for your container image.

Step 2: Store the Container Image in a Registry

After creating the container image, you need to store it in a container registry, such as Azure Container Registry (ACR) or Docker Hub. A container registry is a centralized repository for storing and managing container images. In this guide, we will use Azure Container Registry.

To create an Azure Container Registry, run the following command:

az acr create --resource-group <resource_group> --name <registry_name> \
--sku Basic --location <location>

 

Replace <resource_group> with the name of your Azure resource group, <registry_name> with a unique name for your registry, and <location> with the desired Azure region.

Next, authenticate your Docker client with the ACR registry:

az acr login --name <registry_name>

Tag your local container image with the registry's login server and push the image to the registry:

docker tag <image_name> <registry_name>.azurecr.io/<image_name>
docker push <registry_name>.azurecr.io/<image_name>

Step 3: Deploy the Containerized Application

With the container image stored in the registry, you can now deploy the application to Azure using either Azure Container Instances (ACI) or Azure Kubernetes Service (AKS), depending on your requirements and the complexity of your application.

 

Option 1: Deploying to Azure Container Instances (ACI)

 

ACI is a serverless container orchestration service that allows you to quickly deploy and manage containers without having to manage the underlying infrastructure. ACI is ideal for scenarios where you need a fast, cost-effective solution for running single-container applications.

To deploy a container to ACI, run the following command:

az container create --resource-group <resource_group> \ 
--name <container_name> --image <registry_name>.azurecr.io/<image_name> \ 
--dns-name-label <dns_name_label> --ports <port> \ 
--registry-login-server <registry_name>.azurecr.io \ 
--registry-username <registry_username> --registry-password <registry_password>

Replace <resource_group> with the name of your Azure resource group, <container_name> with a name for your container, <registry_name> with the name of your container registry, <image_name> with the name of your container image, <dns_name_label> with a unique DNS name for your container, <port> with the port number your application listens on, and <registry_username> and <registry_password> with the credentials for your container registry.

After the deployment is complete, you can access your containerized application using the assigned fully qualified domain name (FQDN).

 

Option 2: Deploying to Azure Kubernetes Service (AKS)

 

AKS is a managed Kubernetes service that simplifies the deployment, management, and scaling of containerized applications. AKS is ideal for scenarios where you need to manage and orchestrate multi-container applications.

To deploy a container to AKS, follow these steps:

1. Create an AKS cluster:

az aks create --resource-group <resource_group> --name <cluster_name> \ 
--node-count <node_count> --generate-ssh-keys

Replace <resource_group> with the name of your Azure resource group, <cluster_name> with a name for your AKS cluster, and <node_count> with the desired number of nodes in the cluster.

2. Configure kubectl to connect to your AKS cluster:

az aks get-credentials --resource-group <resource_group> --name <cluster_name>

3. Create a Kubernetes manifest file (e.g., deployment.yaml) that describes your containerized application, including the image location, resource requirements, and desired number of replicas:


apiVersion: apps/v1
kind: Deployment
metadata:
 name: <container_name>
spec:
 replicas: <replica_count>
 selector:
  matchLabels:
   app: <container_name>
 template:
  metadata:
   labels:
    app: <container_name>
  spec:
   containers:
   - name: <container_name>
    image: <registry_name>.azurecr.io/<image_name>
    ports:
    - containerPort: <port>
   imagePullSecrets:
   - name: <acr_pull_secret>

Replace <container_name> with a name for your container, <registry_name> with the name of your container registry, <image_name> with the name of your container image, <replica_count> with the desired number of replicas, <port> with the port number your application listens on, and <acr_pull_secret> with the name of the secret for pulling the container image from the registry.

4. Create a secret for pulling the container image from the registry:


kubectl create secret docker-registry <acr_pull_secret> \ 
--docker-server=<registry_name>.azurecr.io --docker-username=<registry_username> \ 
--docker-password=<registry_password> --docker-email=<email>

 

5. Deploy the containerized application:

kubectl apply -f deployment.yaml

 

After the deployment is complete, you can access your containerized application using a Kubernetes service, ingress, or load balancer, depending on your requirements.

Step 4: Scaling and Monitoring Your Application

Once your containerized application is deployed to Azure, you can scale and monitor it to ensure optimal performance and availability.

To scale your application, you can use the horizontal pod autoscaler (HPA) in AKS or the --cpu and --memory options in ACI. The HPA in AKS automatically adjusts the number of replicas based on CPU and memory utilization, while the --cpu and --memory options in ACI allow you to allocate resources to your container.

For monitoring your application, you can use Azure Monitor, which provides a comprehensive solution for collecting, analyzing, and acting on telemetry from your applications and the underlying infrastructure. Azure Monitor integrates seamlessly with ACI and AKS, enabling you to monitor container performance, diagnose issues, and set up alerts based on custom metrics and log data.

To enable monitoring for your containerized application in ACI, add the --log-analytics-workspace and --log-analytics-workspace-key options to the az container create command:

az container create --resource-group <resource_group> --name <container_name> \ 
--image <registry_name>.azurecr.io/<image_name> --dns-name-label <dns_name_label> \ 
--ports <port> --registry-login-server <registry_name>.azurecr.io \ 
--registry-username <registry_username> --registry-password <registry_password> \ 
--log-analytics-workspace <workspace_id> \ 
--log-analytics-workspace-key <workspace_key>

 

Replace <workspace_id> and <workspace_key> with the ID and key of your Log Analytics workspace.

To enable monitoring for your containerized application in AKS, follow the official documentation to enable Azure Monitor for containers.

Conclusion

Deploying containerized applications to Azure allows you to take advantage of the scalability, flexibility, and cost-efficiency of the cloud. By using Azure Container Instances, Azure Kubernetes Service, and Azure Container Registry, you can quickly and easily deploy, scale, and manage your containerized applications in Azure. With Azure Monitor, you can also keep a close eye on the performance and health of your applications, ensuring that they run smoothly and reliably in the cloud.

As containerization continues to gain popularity in the world of application development, leveraging the power of Azure's container orchestration services can help you streamline your development and deployment processes, optimize resource utilization, and deliver a better experience for your users.

Ready to revolutionize your business with Azure Compute Services? Don't wait! Get started now and experience unmatched scalability and performance. Click here to begin your cloud journey.