# 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 1. **Prepare the application:** ```bash ./prepare-app.sh ``` This script builds the Docker image and creates the directory for persistent volume. 2. **Start the application:** ```bash ./start-app.sh ``` This script creates all necessary Kubernetes objects in the correct order, including PostgreSQL. 3. **Pause or delete the application:** ```bash ./stop-app.sh ``` This script removes all Kubernetes objects created by `start-app.sh`. ## Accessing the Application To access the application: 1. Find the IP address of your Kubernetes node: ```bash kubectl get nodes -o wide ``` 2. Access the application in your browser at: ``` http://:80 ``` Where `` 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 key - `item`: Text (item name) - `quantity`: Integer (defaults to 1) - `purchased`: Boolean flag (defaults to false) ## Troubleshooting If you encounter issues: 1. Check pod status: ```bash kubectl get pods -n my-app ``` 2. View pod logs: ```bash kubectl logs -n my-app ``` 3. Check service status: ```bash kubectl get svc -n my-app ``` 4. Check database connectivity: ```bash kubectl exec -it -n my-app -- python -c "import psycopg2; conn = psycopg2.connect(host='postgres', dbname='postgres', user='postgres', password='postgres'); print('Connection successful')" ```