Deploying a Basic Flask App on Kubernetes Using Minikube (Step-by-Step Guide)

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:

TaskCommand
Check Podskubectl get pods -o wide
Watch live changeskubectl get pods -w
Check Deploymentkubectl describe deployment flask-k8s-deployment
Check Servicekubectl describe svc flask-k8s-service
View Logskubectl logs <pod-name> -f
Inspect inside podkubectl exec -it <pod-name> -- /bin/bash
Watch Eventskubectl get events --sort-by=.metadata.creationTimestamp
Check Resource Usagekubectl 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
Scroll to Top