Deploying Flask Apps with Kubernetes on Google Cloud

As applications grow in complexity and user base, developers seek scalable and resilient deployment strategies. Kubernetes, the industry-standard container orchestration platform, combined with Google Kubernetes Engine (GKE), provides a powerful solution for deploying Python Flask apps in production environments.

In this blog, we’ll walk through how to containerize and deploy a Flask application using Kubernetes on Google Cloud Platform (GCP) — giving you flexibility, scalability, and fault tolerance.


Why Use Kubernetes for Flask Apps?

Scalability: Automatically scale your app based on traffic

Resilience: Self-healing pods replace crashed containers

Rolling updates: Deploy new versions without downtime

Seamless integration with CI/CD tools and Google services


Architecture Overview

Flask App: Dockerized and container-ready

Docker: To package the app

Kubernetes: To manage container deployment

GKE (Google Kubernetes Engine): Hosted Kubernetes service

GCR (Google Container Registry): To store Docker images


Step 1: Prepare Your Flask App

Create a simple app.py:

python


from flask import Flask

app = Flask(__name__)


@app.route('/')

def hello():

    return "Hello from Flask on GKE!"

Create requirements.txt:


ini


flask==2.0.3

gunicorn==20.1.0

Step 2: Dockerize the App

Create a Dockerfile:


Dockerfile


FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD ["gunicorn", "-b", "0.0.0.0:8080", "app:app"]

Build and tag the image:


bash

Copy

Edit

docker build -t gcr.io/YOUR_PROJECT_ID/flask-gke-app .

Push to Google Container Registry:


bash

Copy

Edit

gcloud auth configure-docker

docker push gcr.io/YOUR_PROJECT_ID/flask-gke-app


Step 3: Set Up Google Kubernetes Engine (GKE)

Enable Kubernetes Engine API in your GCP project.


Create a Kubernetes cluster:


bash

Copy

Edit

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

gcloud container clusters get-credentials flask-cluster


Step 4: Create Kubernetes Deployment and Service

deployment.yaml


yaml

Copy

Edit

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-container

        image: gcr.io/YOUR_PROJECT_ID/flask-gke-app

        ports:

        - containerPort: 8080

service.yaml


yaml

Copy

Edit

apiVersion: v1

kind: Service

metadata:

  name: flask-service

spec:

  type: LoadBalancer

  selector:

    app: flask-app

  ports:

    - protocol: TCP

      port: 80

      targetPort: 8080

Apply the configs:


bash

Copy

Edit

kubectl apply -f deployment.yaml

kubectl apply -f service.yaml


Step 5: Access Your Flask App

After a few minutes, run:


bash

Copy

Edit

kubectl get service flask-service

You’ll see an external IP under the EXTERNAL-IP column. Open it in a browser to view your Flask app running on GKE.


Step 6: Monitor and Scale

Scale up:


bash

Copy

Edit

kubectl scale deployment flask-app --replicas=5

View logs:


bash

Copy

Edit

kubectl logs -l app=flask-app

Dashboard (optional):


bash

Copy

Edit

gcloud components install kubectl

kubectl proxy

Conclusion

Deploying Flask apps on Kubernetes with GKE gives you full control over scaling, high availability, and container orchestration. While it introduces some complexity over traditional PaaS solutions like Heroku, it’s ideal for production-grade, large-scale applications. With Google Cloud’s managed services, you get the power of Kubernetes without the overhead of maintaining clusters — making it a smart choice for Flask developers looking to scale.



Learn FullStack Python Training

Read More : Fullstack Flask Deployment: Setting Up Continuous Delivery on AWS with CodePipeline

Read More : Deploying Fullstack Python Apps on AWS Lambda for Serverless Architecture

Read More : Fullstack Python: Using Heroku for Flask App Deployment and Scaling

Visit Our IHUB Talent Training Institute in Hyderabad

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Using Hibernate ORM for Fullstack Java Data Management

Creating a Test Execution Report with Charts in Playwright