8-deploying-a-flask-application-with-docker-and-kubernetes-on-google-cloud.html

Deploying a Flask Application with Docker and Kubernetes on Google Cloud

In today's cloud-centric world, deploying applications efficiently is crucial for developers and businesses alike. Flask, a lightweight web framework for Python, is a popular choice for building web applications due to its simplicity and flexibility. When combined with Docker and Kubernetes, you unlock powerful capabilities for deploying, scaling, and managing your applications. This article will guide you through the process of deploying a Flask application using Docker and Kubernetes on Google Cloud, covering definitions, use cases, and actionable insights.

Why Choose Flask, Docker, and Kubernetes?

Flask

Flask is a micro web framework designed for Python. Its lightweight nature allows developers to build web applications quickly while maintaining flexibility and scalability. Typical use cases for Flask include:

  • Building RESTful APIs
  • Developing simple web applications
  • Creating prototypes and MVPs

Docker

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Benefits of using Docker include:

  • Consistency across environments (development, testing, production)
  • Simplified dependency management
  • Isolation of applications

Kubernetes

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Its advantages include:

  • Automated scaling based on demand
  • Load balancing of application traffic
  • Self-healing capabilities for maintaining application uptime

Prerequisites

Before diving into the deployment process, ensure you have the following:

  • A Google Cloud account
  • Google Cloud SDK installed
  • Basic knowledge of Python and Flask
  • Familiarity with Docker and Kubernetes concepts

Step-by-Step Guide to Deploying a Flask Application

1. Create a Simple Flask Application

Let’s start by creating a simple Flask application. Create a new directory for your project and create a file named app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World!"

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

2. Create a Requirements File

Create a requirements.txt file to specify the dependencies for your Flask app:

Flask==2.0.2

3. Create a Dockerfile

Next, create a Dockerfile in the same directory to define your Docker image:

# Use the official Python image from Docker Hub
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

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

# Copy the rest of the application
COPY . .

# Expose the application port
EXPOSE 8080

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

4. Build and Run the Docker Image Locally

Build your Docker image:

docker build -t flask-app .

Run the Docker container:

docker run -p 8080:8080 flask-app

Visit http://localhost:8080 in your browser to see your Flask application running.

5. Push the Docker Image to Google Container Registry

First, authenticate your Google Cloud account:

gcloud auth login

Tag your Docker image for Google Container Registry:

docker tag flask-app gcr.io/YOUR_PROJECT_ID/flask-app

Push the image to Google Container Registry:

docker push gcr.io/YOUR_PROJECT_ID/flask-app

6. Create a Kubernetes Cluster

Create a Kubernetes cluster on Google Kubernetes Engine (GKE):

gcloud container clusters create flask-cluster --num-nodes=1

7. Deploy the Flask Application on Kubernetes

Create a deployment configuration file named 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: gcr.io/YOUR_PROJECT_ID/flask-app
        ports:
        - containerPort: 8080

Deploy your application:

kubectl apply -f deployment.yaml

8. Expose the Application to the Internet

Create a service configuration 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

9. Access Your Flask Application

To access your Flask application, retrieve the external IP address of the LoadBalancer service:

kubectl get services

Once you have the external IP, visit http://EXTERNAL_IP in your browser to see your Flask application in action!

Troubleshooting Tips

  • Container not starting: Check logs using kubectl logs <pod-name> to diagnose issues.
  • Service not reachable: Ensure that your firewall rules allow traffic on the specified ports.
  • Image not found: Verify that your Docker image was successfully pushed to Google Container Registry.

Conclusion

Deploying a Flask application with Docker and Kubernetes on Google Cloud allows for scalable, reliable, and efficient management of your web applications. By following this guide, you can leverage the power of containerization and orchestration to streamline your deployment process. Embrace these tools, and you'll find that managing your applications becomes much simpler and more efficient. 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.