From CrashLoopBackOff to Hello World: Deploying a Flask App on GKE
Gerald Mtetwa

Gerald Mtetwa @gerald475

Joined:
Aug 12, 2025

From CrashLoopBackOff to Hello World: Deploying a Flask App on GKE

Publish Date: Aug 12
1 1

🚀 Project Overview
Tech stack:

Flask – Python web framework

Docker – Containerizing the app

Kubernetes – Orchestrating the containers

Google Kubernetes Engine (GKE) – Running everything in the cloud

Goal: Deploy a Python Flask app that greets users, using Kubernetes on GCP.

📦 Step 1 – The Flask App
Here’s the simple app (app.py):

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, my name is Gerald. What is your name?"

@app.route('/greet', methods=['POST'])
def greet():
    user_name = request.form.get('name', 'Guest')
    return f"Hello {user_name}!"

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

Enter fullscreen mode Exit fullscreen mode

Why port 80?
Because Kubernetes services often route HTTP traffic to port 80 by default — using another port caused issues later.

🐳 Step 2 – Dockerizing the App
Dockerfile:

# Use the official Python image
FROM python:3.12-slim

# Set working directory
WORKDIR /app

# Copy files
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Expose port 80
EXPOSE 80

# Start the app
CMD ["python", "app.py"]

Enter fullscreen mode Exit fullscreen mode

Build & Push to Artifact Registry:

docker build -t africa-south1-docker.pkg.dev/k8s-project-468723/my-docker-repo/my-python-app:v1 .
docker push africa-south1-docker.pkg.dev/k8s-project-468723/my-docker-repo/my-python-app:v1

Enter fullscreen mode Exit fullscreen mode

☸ Step 3 – Kubernetes Deployment
deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-python-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-python-app
  template:
    metadata:
      labels:
        app: my-python-app
    spec:
      containers:
      - name: my-python-app
        image: africa-south1-docker.pkg.dev/k8s-project-468723/my-docker-repo/my-python-app:v1
        ports:
        - containerPort: 80

Enter fullscreen mode Exit fullscreen mode

Apply the deployment:

kubectl apply -f deployment.yaml

Enter fullscreen mode Exit fullscreen mode

Expose it:

kubectl expose deployment my-python-app \
  --type=LoadBalancer \
  --port 80 \
  --target-port 80

Enter fullscreen mode Exit fullscreen mode

🛠 Debugging the CrashLoopBackOff

  1. CrashLoopBackOff At first, pods kept failing:
kubectl get pods -l app=my-python-app
# STATUS: CrashLoopBackOff


Enter fullscreen mode Exit fullscreen mode

Logs showed:

python: can't open file '/app/app.py': [Errno 2] No such file or directory

Enter fullscreen mode Exit fullscreen mode

✅ Fix: My main file was actually named gerald.py. Either rename it to app.py or update the CMD in the Dockerfile to run the correct file.

  1. Wrong Port Originally, Flask ran on port 5000. Since the Kubernetes Service was exposing port 80, the app never responded.

✅ Fix: Changed Flask to run on port 80 and updated the Dockerfile to expose port 80.

🎉 Final Result
After fixing the filename and port

Opening the external IP in the browser showed:

Hello, my name is Gerald. What is your name?

💡 Lessons Learned
Match your container port and service target port — they must align.

Always check pod logs (kubectl logs) for clues.

Keep filenames consistent between your code and Docker configuration.

Start small — test locally before pushing to Kubernetes.

📂 See the Code
Full project available on GitHub: https://github.com/gerald475/simple-python-app

💬 Have you ever battled CrashLoopBackOff?
Share your experience in the comments — let's help each other debug faster!

Comments 1 total

  • Gerald Mtetwa
    Gerald MtetwaAug 12, 2025

    Lets connect and talk about the challenges you had whilst deploying your apps in Google Kubernetes Engine (GKE)

Add comment