π 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)
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"]
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
βΈ 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
Apply the deployment:
kubectl apply -f deployment.yaml
Expose it:
kubectl expose deployment my-python-app \ --type=LoadBalancer \ --port 80 \ --target-port 80
π Debugging the CrashLoopBackOff
- CrashLoopBackOff At first, pods kept failing:
kubectl get pods -l app=my-python-app # STATUS: CrashLoopBackOff
Logs showed:
python: can't open file '/app/app.py': [Errno 2] No such file or directory
β 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.
- 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!
Top comments (1)
Lets connect and talk about the challenges you had whilst deploying your apps in Google Kubernetes Engine (GKE)