7-how-to-deploy-a-flask-app-with-docker-and-kubernetes.html

How to Deploy a Flask App with Docker and Kubernetes

Deploying web applications can be a daunting task, especially when you want to ensure scalability, reliability, and ease of management. Flask, a lightweight web framework for Python, combined with Docker and Kubernetes, provides an excellent solution for deploying web applications. In this article, we'll walk through the steps to deploy a Flask app using these technologies, making it easier for developers to manage applications in a containerized environment.

What is Flask?

Flask is a micro web framework for Python that allows developers to build web applications quickly and easily. Its simplicity and flexibility make it a popular choice for small to medium-sized applications. Flask is built on WSGI (Web Server Gateway Interface) and has a rich ecosystem of libraries and extensions.

What is Docker?

Docker is a platform that enables developers to automate the deployment of applications in lightweight, portable containers. Containers package an application and its dependencies together, ensuring that it runs consistently across different environments. This makes Docker a perfect fit for deploying Flask applications.

What is Kubernetes?

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It works seamlessly with Docker and provides features such as load balancing, self-healing, and rolling updates, making it an ideal choice for deploying Flask apps in production.

Use Cases for Flask, Docker, and Kubernetes

  • Microservices Architecture: Deploying Flask apps as microservices allows for independent scaling and management.
  • API Development: Flask is often used to build RESTful APIs, which can be containerized and managed with Kubernetes.
  • Rapid Prototyping: Using Docker, developers can quickly test and iterate on Flask applications.

Prerequisites

Before we begin, ensure you have the following installed:

  • Python and Flask
  • Docker
  • Kubernetes (Minikube for local development)
  • kubectl (Kubernetes command-line tool)

Step-by-Step Guide to Deploy a Flask App with Docker and Kubernetes

Step 1: Create a Simple Flask Application

Start by creating a simple Flask application. Create a new directory for your project and add a file called app.py.

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask with Docker and Kubernetes!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

Step 2: Create a Requirements File

Create a requirements.txt file in the same directory to specify the dependencies for your Flask application.

Flask==2.0.2

Step 3: Create a Dockerfile

Next, create a Dockerfile to define how your Flask app will be containerized.

# Dockerfile
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY app.py .

# Expose the port
EXPOSE 5000

# Command to run the application
CMD ["python", "app.py"]

Step 4: Build the Docker Image

Open your terminal, navigate to your project directory, and build the Docker image with the following command:

docker build -t flask-docker-app .

Step 5: Run the Docker Container

Run your Flask app in a Docker container:

docker run -p 5000:5000 flask-docker-app

You can now access your Flask app by visiting http://localhost:5000 in your web browser.

Step 6: Create Kubernetes Deployment and Service

To deploy your Flask app on Kubernetes, create a deployment.yaml file:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: flask
  template:
    metadata:
      labels:
        app: flask
    spec:
      containers:
      - name: flask
        image: flask-docker-app
        ports:
        - containerPort: 5000

Next, create a service.yaml file to expose your Flask app:

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: flask-service
spec:
  type: NodePort
  selector:
    app: flask
  ports:
    - port: 5000
      targetPort: 5000
      nodePort: 30000

Step 7: Deploy to Kubernetes

Now, you can deploy your Flask app to Kubernetes. First, ensure that your Kubernetes cluster (e.g., Minikube) is running, then apply the configuration files:

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

Step 8: Access Your Flask App

To access your deployed Flask app, run the following command to get the Minikube IP:

minikube ip

You can then access your app using the following URL:

http://<minikube-ip>:30000

Troubleshooting Tips

  • If your app isn't running as expected, check the logs with kubectl logs <pod-name>.
  • Ensure that your Docker image is built correctly and is available in your Kubernetes cluster.
  • Use kubectl get pods to check the status of your deployments.

Conclusion

Deploying a Flask app with Docker and Kubernetes is a powerful approach that enhances scalability and manageability. By following the steps outlined in this article, you can create an efficient deployment pipeline for your Flask applications. As you grow more comfortable with these tools, you'll find that they can significantly streamline your development and deployment processes. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.