Deploying a Flask App with Docker and Kubernetes on Google Cloud
In today’s tech landscape, deploying applications efficiently and reliably is paramount. Flask, a micro web framework for Python, is a popular choice for developing web applications due to its simplicity and flexibility. When combined with Docker and Kubernetes, you can take your Flask app to the next level, ensuring scalability and ease of management. In this article, we’ll guide you through deploying a Flask application using Docker and Kubernetes on Google Cloud Platform (GCP), providing you with a comprehensive understanding of the process along the way.
What is Flask?
Flask is a lightweight framework that allows developers to create web applications quickly and with minimal setup. It’s designed to make getting started quick and easy, with the ability to scale up to complex applications. Flask is particularly loved for its simplicity and the freedom it gives developers to structure their applications as they see fit.
What is Docker?
Docker is a platform that allows you to automate the deployment of applications inside lightweight containers. These containers package your application with all its dependencies, making it easy to run in any environment without worrying about compatibility issues.
What is Kubernetes?
Kubernetes is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. It’s particularly useful for managing large-scale applications with multiple services and instances.
Why Use Google Cloud?
Google Cloud provides a robust infrastructure for deploying applications using Docker and Kubernetes. With Google Kubernetes Engine (GKE), you can quickly set up and manage Kubernetes clusters, allowing for efficient scaling and management of your applications.
Use Cases for Flask with Docker and Kubernetes
- Microservices Architecture: Easily deploy and scale individual components of your application.
- Development Environment: Create consistent development environments across teams.
- Continuous Integration/Continuous Deployment (CI/CD): Streamline your deployment pipeline.
Step-by-Step Guide to Deploying a Flask App
Step 1: Create a Simple Flask Application
First, let’s create a basic Flask application. Create a new directory for your project and add a file named app.py
.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Step 2: Create a Dockerfile
Next, we need to containerize the Flask application. Create a file named Dockerfile
in the same directory as app.py
.
# Use the official Python image from the Docker Hub
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 .
# Set the command to run the application
CMD ["python", "app.py"]
Step 3: Create a requirements.txt File
In the same directory, create a requirements.txt
file to specify your Flask dependency.
Flask==2.0.1
Step 4: Build the Docker Image
Open your terminal, navigate to your project directory, and run the following command to build your Docker image.
docker build -t flask-app .
Step 5: Run the Docker Container Locally
To test your application locally, run the following command:
docker run -p 8080:8080 flask-app
You can now visit http://localhost:8080
in your web browser to see your Flask app in action.
Step 6: Push Docker Image to Google Container Registry
- Authenticate with Google Cloud:
bash
gcloud auth login
- Tag your Docker image:
bash
docker tag flask-app gcr.io/YOUR_PROJECT_ID/flask-app
- Push the image:
bash
docker push gcr.io/YOUR_PROJECT_ID/flask-app
Step 7: Create a Kubernetes Deployment
Now that your image is available in Google Container Registry, it’s time to deploy it using Kubernetes. Create a file named deployment.yaml
.
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
spec:
replicas: 3
selector:
matchLabels:
app: flask-app
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app
image: gcr.io/YOUR_PROJECT_ID/flask-app
ports:
- containerPort: 8080
Step 8: Deploy to GKE
- Create a GKE Cluster:
bash
gcloud container clusters create flask-cluster --num-nodes=3
- Deploy your application:
bash
kubectl apply -f deployment.yaml
Step 9: Expose Your Application
To make your application accessible, expose it using a service. Create a file named service.yaml
.
apiVersion: v1
kind: Service
metadata:
name: flask-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: flask-app
Apply the service configuration:
kubectl apply -f service.yaml
Step 10: Access Your Application
After a few moments, run the following command to get the external IP of your service:
kubectl get services
Visit the external IP address in your browser to see your Flask app live!
Conclusion
Deploying a Flask application using Docker and Kubernetes on Google Cloud allows you to leverage the power of containerization and orchestration. This approach not only simplifies the deployment process but also enhances scalability and manageability. By following the steps outlined in this article, you can set up your own Flask application in the cloud, ensuring that it’s ready to handle traffic efficiently. Whether you’re building microservices or managing a full-stack application, this setup provides a solid foundation for modern web development.