Deploy a Python Flask application on Azure Kubernetes Service (AKS)

To deploy a Python Flask application on Azure Kubernetes Service (AKS), you'll need to follow these steps:


Prerequisites:

  1. Azure Account: You should have an Azure account. If not, you can create one at Azure Portal.

  2. Azure CLI: Install and set up Azure CLI on your local machine. Follow the instructions here.

  3. Kubernetes CLI (kubectl): Install Kubernetes CLI on your local machine. Instructions can be found here.

  4. Docker: Install Docker on your local machine for containerizing the Flask application. Instructions are available here.

  5. Flask Application: You need a Python Flask application ready for deployment. If you don't have one, you can create a simple app like this:

    # app.py
    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello, World!'
    
    if __name__ == "__main__":
        app.run(debug=True, host='0.0.0.0')

Steps:

1. Create and configure the Azure Kubernetes Service (AKS) cluster:

  • Open Azure CLI and log in to your Azure account:

    az login
  • Create a resource group:

    az group create --name MyResourceGroup --location eastus
  • Create the AKS cluster:

    az aks create --resource-group MyResourceGroup --name MyAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
    
  • Configure kubectl to use the new AKS cluster:

    az aks get-credentials --resource-group MyResourceGroup --name MyAKSCluster

2. Containerize the Flask Application with Docker:

  • Create a Dockerfile for your Flask app in the same directory as app.py:

    # Dockerfile
    FROM python:3.8-slim
    
    # Set environment variables
    ENV FLASK_APP=app.py
    ENV FLASK_RUN_HOST=0.0.0.0
    
    # Install dependencies
    WORKDIR /app
    COPY . /app
    RUN pip install --no-cache-dir -r requirements.txt
    
    # Expose port 5000
    EXPOSE 5000
    
    # Start Flask app
    CMD ["flask", "run"]
  • Create a requirements.txt file with necessary Python dependencies:

    Flask==2.0.1
  • Build the Docker image:

    docker build -t flask-app .
  • Test the Docker image locally (optional):

    docker run -p 5000:5000 flask-app

3. Push Docker Image to Azure Container Registry (ACR):

  • Create an Azure Container Registry (ACR):

    az acr create --resource-group MyResourceGroup --name MyContainerRegistry --sku Basic
    
  • Log in to your ACR:

    az acr login --name MyContainerRegistry
  • Tag your Docker image to match the ACR repository:

    docker tag flask-app MyContainerRegistry.azurecr.io/flask-app:v1
  • Push the Docker image to ACR:

    docker push MyContainerRegistry.azurecr.io/flask-app:v1

4. Deploy the Flask Application to AKS:

  • Create a Kubernetes deployment file (flask-deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: flask-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: flask
      template:
        metadata:
          labels:
            app: flask
        spec:
          containers:
          - name: flask
            image: MyContainerRegistry.azurecr.io/flask-app:v1
            ports:
            - containerPort: 5000
  • Create a service to expose your Flask application (flask-service.yaml):

    apiVersion: v1
    kind: Service
    metadata:
      name: flask-service
    spec:
      selector:
        app: flask
      ports:
        - protocol: TCP
          port: 80
          targetPort: 5000
      type: LoadBalancer
  • Apply the Kubernetes deployment and service files:

    kubectl apply -f flask-deployment.yaml
    kubectl apply -f flask-service.yaml

5. Access the Flask Application:

  • Once the AKS cluster is running and the service is created, check the external IP of your service:

    kubectl get services
  • The external IP will appear under the EXTERNAL-IP column for flask-service. Once it is available, you can visit the IP in your browser to access your Flask app.

Optional Steps:

  • Monitor the logs using kubectl:

    kubectl logs <pod-name>
  • Scaling the application (for more replicas):

    kubectl scale deployment flask-deployment --replicas=3

This completes the process of deploying a Python Flask application to Azure Kubernetes Service. 

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete