DEV Community

Jonathan Chijioke
Jonathan Chijioke

Posted on

Flask Web + DB on Kubernetes

🧰 Project Overview

This project demonstrates how to:
β€’ Containerize a simple Flask web app and a mock database API
β€’ Deploy both services using Kubernetes
β€’ Use inter-service communication inside the cluster
β€’ Access the frontend via browser and retrieve data from the backend

πŸ“¦** Project Structure**

πŸ”§ Prerequisites
β€’ Docker
β€’ Minikube
β€’ kubectl
β€’ A basic understanding of Python and terminal commands

*Step 1: Create the Flask Apps
Web/app.py (file)
*

πŸ“„ db/app.py

🐳 Step 2: Dockerize the Apps

πŸ“„ web/Dockerfile

πŸ“„ db/Dockerfile

πŸ“„ requirements.txt (for both)

πŸ§ͺ Step 3: Build Docker Images in Minikube

RUN:

minikube start
eval $(minikube docker-env)

Then build:
docker build -t flask-web ./web
docker build -t flask-db ./db
**
🧾 Step 4: Create Kubernetes Deployment & Service Files
**
πŸ“„ db-deployment.yaml

πŸ“„ web-deployment.yaml

πŸš€ Step 5: Deploy to Kubernetes

kubectl apply -f db-deployment.yaml
kubectl apply -f web-deployment.yaml

Check the status:

kubectl get pods
kubectl get services

🌐 Step 6: Access the Web App

minikube service web-service --url

*I went on to improve the project by adding monitoring and logging *

βœ… Monitoring using Prometheus + Grafana
βœ… Logging using Loki + Grafana

*Goal *

Prometheus - Collect metrics (CPU, memory, HTTP request)
Grafana - Visualize metrics and logs
Loki - Collect and search logs

🧱 Prerequisites

Make sure you have:
β€’ Minikube
β€’ kubectl
β€’ Your web and db services already deployed
β€’ Optional: Helm (we’ll use it for easier installation)

*Part 1: Monitoring with Prometheus + Grafana *

βœ… Step 1: Enable Helm in Minikube (optional but recommended)
**
minikube addons enable helm-tiller
**
βœ… Step 2: Add Helm Repos

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

βœ… Step 3: Install Prometheus

helm install prometheus prometheus-community/prometheus

This installs:
β€’ Prometheus server
β€’ Node exporter
β€’ Alertmanager

Check it: kubectl get pods
**
βœ… Step 4: Install Grafana**

helm install grafana grafana/grafana \
--set adminPassword='admin' \
--set service.type=NodePort

Find the Grafana service:

minikube service grafana --url

Log in with:
β€’ User: admin
β€’ Password: admin

βœ… Step 5: Add Prometheus as Data Source in Grafana

  1. Open Grafana (from minikube service grafana)
  2. Go to Settings > Data Sources
  3. Click Add Data Source
  4. Choose Prometheus
  5. URL: http://prometheus-server
  6. Click Save & Test Now you’re ready to build dashboards! ** βœ… Step 6: Add Metrics to Your Flask Apps (Optional but recommended) ** Add prometheus_flask_exporter to both web/requirements.txt and db/requirements.txt:

Flask
requests
prometheus_flask_exporter

Update both app.py files:

from prometheus_flask_exporter import PrometheusMetrics

metrics = PrometheusMetrics(app)

Rebuild and redeploy the images:

eval $(minikube docker-env)
docker build -t flask-web ./web
docker build -t flask-db ./db
kubectl rollout restart deployment web
kubectl rollout restart deployment db

Top comments (0)