Running a Flask web app inside a Kubernetes cluster is one of the best ways to understand container orchestration locally.
In this guide, youβll learn to create, deploy, and expose a Flask app on Minikube β and test it directly in your browser.
π§© Prerequisites
Before you begin, make sure the following tools are installed on your system:
Check versions:
docker --version
minikube version
kubectl version --client
ποΈ Step 1 β Create the Project Structure
mkdir flask-k8s-demo
cd flask-k8s-demo
Folder structure:
flask-k8s-demo/
βββ app/
β βββ main.py
β βββ requirements.txt
βββ Dockerfile
βββ kubernetes/
β βββ deployment.yaml
π Step 2 β Write the Flask App
File: app/main.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "π Hello from Flask running on Kubernetes via Minikube!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
File: app/requirements.txt
flask==3.0.3
π³ Step 3 β Create Dockerfile
File: Dockerfile
# Use Python base image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirement file and install
COPY app/requirements.txt .
RUN pip install -r requirements.txt
# Copy app code
COPY app/ .
# Expose Flask port
EXPOSE 5000
# Run Flask app
CMD ["python", "main.py"]
βοΈ Step 4 β Start Minikube and Use Its Docker Environment
minikube start βdriver=docker
eval $(minikube docker-env)
This ensures your Docker image is built inside Minikubeβs Docker environment.
ποΈ Step 5 β Build the Docker Image
docker build -t flask-k8s-demo:latest .
Verify:
docker images
You should see:
flask-k8s-demo latest <image_id> ...
βΈοΈ Step 6 β Create Kubernetes Deployment and Service
File: kubernetes/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-k8s-deployment
spec:
replicas: 2
selector:
matchLabels:
app: flask-k8s-demo
template:
metadata:
labels:
app: flask-k8s-demo
spec:
containers:
- name: flask-k8s-demo
image: flask-k8s-demo:latest
imagePullPolicy: Never
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: flask-k8s-service
spec:
type: LoadBalancer
selector:
app: flask-k8s-demo
ports:
- protocol: TCP
port: 6000
targetPort: 5000
π Step 7 β Deploy to Kubernetes
kubectl apply -f kubernetes/deployment.yaml
Check resources:
kubectl get pods
kubectl get svc
Example output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
flask-k8s-service LoadBalancer 10.98.151.42 <pending> 6000:32500/TCP 1m
The EXTERNAL-IP will show
<pending>β weβll fix that next.
π Step 8 β Expose the LoadBalancer in Minikube
Run this in a new terminal:
minikube tunnel
Keep this terminal running β it simulates a real cloud load balancer.
Now, check again:
kubectl get svc
Youβll now see:
flask-k8s-service LoadBalancer 10.98.151.42 127.0.0.1 6000:32500/TCP 2m
π§ Step 9 β Access the Flask App
Open your browser and go to:
http://127.0.0.1:6000
β You should see:
π Hello from Flask running on Kubernetes via Minikube!
You can also use:
minikube service flask-k8s-service
This automatically opens the working URL in your browser.
π§Ή Step 10 β Cleanup (Optional)
To start fresh for another practice session:
π§© Delete Deployment & Service
kubectl delete -f kubernetes/deployment.yaml
π³ Remove Docker Image from Minikube
eval $(minikube docker-env)
docker rmi flask-k8s-demo:latest
π‘ Stop or Delete Minikube
minikube stop # just stop
minikube delete # full cleanup
Verify cleanup:
kubectl get all
minikube status
Kubernetes Monitoring & Log Commands for Flask Deployment
These commands will help you check the health, status, events, resources, and logs of your Flask application running inside Minikube.
π 1. Cluster Overview
Check if Minikube and Kubernetes are running fine:
minikube status
kubectl cluster-info
kubectl get nodes -o wide
See all resources in the current namespace:
kubectl get all
π¦ 2. Deployment Monitoring
Check Deployment Status
kubectl get deployment flask-k8s-demo
Detailed description (includes events and rollout info):
kubectl describe deployment flask-k8s-demo
Check Rollout History
kubectl rollout history deployment flask-k8s-demo
If you update your image or config:
kubectl rollout status deployment flask-k8s-demo
Rollback if needed:
kubectl rollout undo deployment flask-k8s-demo
π§± 3. Pod Monitoring
List All Pods
kubectl get pods -o wide
This shows Pod names, node assignment, and IPs.
Describe a Specific Pod
kubectl describe pod <pod-name>
Example:
kubectl describe pod flask-k8s-deployment-abc123
This shows:
- Container image
- Resource limits
- Events (such as restarts or failures)
- Liveness/readiness probe status (if defined)
Watch Pod Status Live
kubectl get pods -w
π 4. Service Monitoring
Get Service Info
kubectl get svc flask-k8s-service -o wide
Describe Service (and see its Endpoints)
kubectl describe svc flask-k8s-service
Check if Pods are attached as endpoints:
kubectl get endpoints flask-k8s-service
π³ 5. Container & Logs
Get Logs from a Pod
kubectl logs <pod-name>
Example:
kubectl logs flask-k8s-deployment-abc123
If your pod has multiple containers (not here, but for reference):
kubectl logs <pod-name> -c <container-name>
Stream Logs in Real Time
kubectl logs -f <pod-name>
π§ 6. Debugging and Exec Commands
Access a Running Podβs Shell
You can get inside your Flask container directly:
kubectl exec -it <pod-name> -- /bin/bash
Check whatβs running inside:
ps aux
curl http://127.0.0.1:5000
exit
π§© 7. Events and Errors
Check all events in the namespace:
kubectl get events --sort-by=.metadata.creationTimestamp
Filter for warnings or errors:
kubectl get events | grep Warning
π 8. Resource Utilization (CPU, Memory)
If you have metrics-server installed (you can enable it via Minikube):
Enable Metrics Server (first time only)
minikube addons enable metrics-server
Then check usage:
kubectl top nodes
kubectl top pods
π§° 9. Namespace and Context Commands
Show current context:
kubectl config current-context
List namespaces:
kubectl get ns
Switch namespace (if you create multiple apps later):
kubectl config set-context --current --namespace=<namespace>
π§Ό 10. Cleanup Monitoring Resources
Delete all Kubernetes objects for this app:
kubectl delete -f kubernetes/deployment.yaml
Delete individual objects (optional):
kubectl delete pod <pod-name>
kubectl delete svc flask-k8s-service
kubectl delete deployment flask-k8s-deployment
β Common Monitoring Workflow (for your Flask app)
Hereβs a typical monitoring checklist:
| Task | Command |
|---|---|
| Check Pods | kubectl get pods -o wide |
| Watch live changes | kubectl get pods -w |
| Check Deployment | kubectl describe deployment flask-k8s-deployment |
| Check Service | kubectl describe svc flask-k8s-service |
| View Logs | kubectl logs <pod-name> -f |
| Inspect inside pod | kubectl exec -it <pod-name> -- /bin/bash |
| Watch Events | kubectl get events --sort-by=.metadata.creationTimestamp |
| Check Resource Usage | kubectl top pods (after enabling metrics-server) |
Tip β Combine Commands for Quick Debug
kubectl get pods -o wide && kubectl describe svc flask-k8s-service && kubectl get events | tail -10
