Deploying Containers on Azure

This document provides a comprehensive guide on deploying and managing containerized applications on Microsoft Azure. Azure offers a variety of services tailored to container workloads, from simple single-container deployments to complex orchestrations.

Choosing the Right Azure Container Service

The best service for deploying your containers depends on your specific needs, including scalability, management overhead, and existing infrastructure.

1. Azure Container Instances (ACI)

ACI is ideal for simple scenarios where you need to run a container without managing virtual machines or orchestrators. It's perfect for:

Key Features:

For detailed deployment steps using ACI, refer to the Azure Container Instances documentation.

2. Azure Kubernetes Service (AKS)

AKS is a managed Kubernetes service that simplifies deploying, managing, and scaling containerized applications. It's suitable for complex, production-grade applications that require:

Key Features:

For detailed deployment steps using AKS, refer to the Azure Kubernetes Service (AKS) documentation.

Deployment Scenarios and Examples

Deploying a Single Container with ACI

Deploying a single container with ACI is straightforward. You can use the Azure CLI, Azure Portal, or SDKs.

Tip: For quick deployments, the Azure CLI is an excellent choice.

Example using Azure CLI:


az container create \
    --resource-group myResourceGroup \
    --name mycontainer \
    --image mcr.microsoft.com/azuredocs/aci-helloworld \
    --dns-name-label myuniqueaci \
    --ports 80
        

This command creates a resource group (if it doesn't exist), deploys the aci-helloworld container image, assigns a DNS label, and exposes port 80.

Deploying an Application to AKS

Deploying to AKS typically involves defining your application's desired state using Kubernetes manifests (YAML files) and applying them to your AKS cluster.

Note: Ensure you have kubectl configured to connect to your AKS cluster.

Example Kubernetes Deployment Manifest (deployment.yaml):


apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-webapp-deployment
  labels:
    app: my-webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-webapp
  template:
    metadata:
      labels:
        app: my-webapp
    spec:
      containers:
      - name: my-webapp-container
        image: mcr.microsoft.com/azuredocs/aci-helloworld
        ports:
        - containerPort: 80
        

Applying the manifest:


kubectl apply -f deployment.yaml
        

This manifest defines a Deployment that will ensure 3 replicas of your application container are running.

Exposing your Application

After deploying, you'll likely want to expose your application to the internet or other services. This is done using Kubernetes Services.

Example Kubernetes Service Manifest (service.yaml):


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

Applying the manifest:


kubectl apply -f service.yaml
        

This manifest creates a Service of type LoadBalancer, which automatically provisions an Azure Load Balancer to expose your application to the internet.

Best Practices for Container Deployment

Warning: Never hardcode secrets directly into your container images or deployment manifests.

By understanding these services and following best practices, you can effectively deploy and manage your containerized applications on Azure, leveraging its robust infrastructure and managed services.