z1 | ||
z2 | ||
deployment.yaml | ||
Dockerfile | ||
namespace.yaml | ||
persistent-storage.yaml | ||
postgres-deployment.yaml | ||
postgres-service.yaml | ||
prepare-app.sh | ||
README.md | ||
requirements.txt | ||
service.yaml | ||
start-app.sh | ||
statefulset.yaml | ||
stop-app.sh |
Shopping List Web Application on Kubernetes
Overview
This project deploys a Python shopping list web application using Flask and PostgreSQL on a Kubernetes cluster. The application allows users to create, track, and manage their shopping items in a persistent way. The deployment includes a Namespace, Deployments for both the web application and PostgreSQL database, a StatefulSet (with PersistentVolume and PersistentVolumeClaim), and Service.
Application Description
- Simple shopping list application that allows users to add, mark as purchased, and delete items
- Each item can have a quantity associated with it
- All data is stored in PostgreSQL for persistence between application restarts
- The application uses a Flask web framework with a minimalist interface
Containers
- simple-web-app: Runs the Python Flask application on port 5000
- postgres: Runs PostgreSQL database to store shopping list items
Kubernetes Objects
- Namespace: Isolates all the resources under
my-app
- Deployment (Web App): Manages the stateless web application pods with 2 replicas for high availability
- Deployment (PostgreSQL): Manages the PostgreSQL database with persistent storage
- StatefulSet: Manages stateful application pods that require persistent storage
- PersistentVolume (PV): Provides persistent storage from the host (1GB)
- PersistentVolumeClaim (PVC): Claims the PV for storage
- Service (Web App): Exposes the web application externally via LoadBalancer
- Service (PostgreSQL): Headless service for internal database access
Networking and Storage
- Internal service discovery allows the web application to connect to PostgreSQL
- PostgreSQL uses persistent storage to maintain shopping list data even if pods are restarted
- The web application is exposed externally using a LoadBalancer service
Container Configuration
- The web app container is based on Python and includes Flask and psycopg2
- PostgreSQL container uses the official PostgreSQL image
- Resource limits and readiness probes are configured for better stability
How to Prepare, Run, Pause, and Delete the Application
-
Prepare the application:
./prepare-app.sh
This script builds the Docker image and creates the directory for persistent volume.
-
Start the application:
./start-app.sh
This script creates all necessary Kubernetes objects in the correct order, including PostgreSQL.
-
Pause or delete the application:
./stop-app.sh
This script removes all Kubernetes objects created by
start-app.sh
.
Accessing the Application
To access the application:
-
Find the IP address of your Kubernetes node:
kubectl get nodes -o wide
-
Access the application in your browser at:
http://<NODE_IP>:80
Where
<NODE_IP>
is the IP address of any of your Kubernetes nodes.
Application Features
- Add items with quantity
- Mark items as purchased/unpurchased
- Delete items
- Items list is separated into "to buy" and "purchased" sections
- Data persists between sessions and application restarts
Database Schema
The application uses a simple PostgreSQL schema:
- Table:
shopping_items
- Fields:
id
: Serial primary keyitem
: Text (item name)quantity
: Integer (defaults to 1)purchased
: Boolean flag (defaults to false)
Troubleshooting
If you encounter issues:
-
Check pod status:
kubectl get pods -n my-app
-
View pod logs:
kubectl logs <pod-name> -n my-app
-
Check service status:
kubectl get svc -n my-app
-
Check database connectivity:
kubectl exec -it <web-app-pod-name> -n my-app -- python -c "import psycopg2; conn = psycopg2.connect(host='postgres', dbname='postgres', user='postgres', password='postgres'); print('Connection successful')"