Deploy a Python Flask application on Azure Kubernetes Service (AKS) using Azure DevOps CI/CD pipelines

Deploying a Python Flask application on Azure Kubernetes Service (AKS) using Azure DevOps CI/CD pipelines involves several steps, from setting up your infrastructure to automating your deployment process. 

Here is the general architecture diagram that we will be deploying

Here's a detailed guide:


Step 1: Prerequisites

Ensure you have the following:

  1. Azure Resources:

    • AKS cluster
    • Azure Container Registry (ACR)
  2. Development Setup:

    • Python Flask application
    • Dockerfile in the root directory of your Flask application
    • Kubernetes manifests (deployment.yaml, service.yaml)
  3. Azure DevOps:

    • An Azure DevOps organization and project
    • Service connection for Azure
  4. Tools:

    • Azure CLI
    • kubectl
    • Docker

Step 2: Set Up the Flask Application

  1. Create Flask Application:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return "Hello, Azure AKS!"
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
  2. Create Dockerfile:

    # Use the official Python image
    FROM python:3.9-slim
    
    # Set the working directory
    WORKDIR /app
    
    # Copy the requirements file
    COPY requirements.txt .
    
    # Install dependencies
    RUN pip install -r requirements.txt
    
    # Copy application files
    COPY . .
    
    # Expose the application port
    EXPOSE 5000
    
    # Run the Flask application
    CMD ["python", "app.py"]
  3. Requirements File (requirements.txt):

    flask

Step 3: Kubernetes Manifests

  1. Deployment YAML (deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: flask-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: flask-app
      template:
        metadata:
          labels:
            app: flask-app
        spec:
          containers:
          - name: flask-app
            image: <your_acr_name>.azurecr.io/flask-app:latest
            ports:
            - containerPort: 5000
  2. Service YAML (service.yaml):

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

Step 4: Push Docker Image to Azure Container Registry

  1. Login to ACR:

    az acr login --name <your_acr_name>
  2. Build and Push Docker Image:

    docker build -t <your_acr_name>.azurecr.io/flask-app:latest .
    docker push <your_acr_name>.azurecr.io/flask-app:latest

Step 5: Configure Azure DevOps CI/CD Pipeline

Create a Service Connection

  1. In Azure DevOps, navigate to Project Settings > Service connections.
  2. Create a new Azure Resource Manager connection with Contributor access to your resource group.

Create a CI Pipeline (Build Pipeline)

  1. Navigate to Pipelines > New Pipeline.

  2. Select your repository and use the following YAML:

    trigger:
      - main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: '3.x'
          addToPath: true
    
      - script: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
        displayName: 'Install dependencies'
    
      - script: |
          docker build -t $(ACR_NAME).azurecr.io/flask-app:$(Build.BuildId) .
          docker push $(ACR_NAME).azurecr.io/flask-app:$(Build.BuildId)
        displayName: 'Build and Push Docker Image'
        env:
          ACR_NAME: <your_acr_name>
          DOCKER_PASSWORD: $(DOCKER_PASSWORD)
          DOCKER_USERNAME: $(DOCKER_USERNAME)
  3. Save and run the pipeline.

Create a CD Pipeline (Release Pipeline)

  1. Navigate to Releases > New Release Pipeline.

  2. Define a stage to deploy to AKS.

  3. Add an Azure CLI Task with the following script:

    az aks get-credentials --resource-group <resource_group> --name <aks_cluster_name>
    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
    
  4. Use artifacts from the CI pipeline.


Step 6: Test the Deployment

  1. Find the external IP of the LoadBalancer service:

    kubectl get service flask-service
  2. Access your application in the browser:

    http://<external-ip>

Optional: Automate with Helm

Use Helm charts to package your application for easier deployment and upgrades.


🌟 Master Microsoft Azure with Microsoft Azure in Action! ðŸŒŸ

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! ðŸ‘‡
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example