106 lines
3.4 KiB
Markdown
106 lines
3.4 KiB
Markdown
# Web Form Application with PostgreSQL on Kubernetes
|
|
|
|
## 📌 Application Description
|
|
|
|
This project is a simple web application built with Flask that provides a form for user input. The form collects the following data:
|
|
- Name
|
|
- Middle name
|
|
- Surname
|
|
- Age
|
|
|
|
Upon submission, the data is stored in a PostgreSQL database. A confirmation message is displayed once the data is successfully inserted.
|
|
|
|
---
|
|
|
|
## 🐳 Containers Used
|
|
|
|
1. **web-app**
|
|
- A Python Flask-based container serving the HTML form and handling form submission.
|
|
- Connects to the PostgreSQL container using environment variables.
|
|
|
|
2. **postgres**
|
|
- Official PostgreSQL container used as the backend database.
|
|
- Uses a named volume to persist data.
|
|
- Initializes the database and the `users` table using a custom `init.sql` script.
|
|
|
|
---
|
|
|
|
## 🕸 Kubernetes Objects
|
|
|
|
| Object | Description |
|
|
|--------------------|-----------------------------------------------------------------------------|
|
|
| `Namespace` | Isolates the application under `webform-app`. |
|
|
| `Deployment` | Manages the Flask web app, ensuring high availability. |
|
|
| `StatefulSet` | Manages the PostgreSQL database pod with persistent identity and storage. |
|
|
| `Service` | Exposes both the web app and PostgreSQL internally via DNS. |
|
|
| `PersistentVolume` | Provides physical storage for PostgreSQL data. |
|
|
| `PersistentVolumeClaim` | Requests storage for the StatefulSet. |
|
|
|
|
---
|
|
|
|
## 🌐 Virtual Networks & Volumes
|
|
|
|
- **Virtual Networking**: Kubernetes Services provide internal DNS-based networking. The Flask app communicates with PostgreSQL using the service name `postgres` within the same namespace.
|
|
|
|
- **Named Volumes**:
|
|
- A named volume is mounted at `/var/lib/postgresql/data` in the PostgreSQL container to persist database data across pod restarts.
|
|
|
|
---
|
|
## ⚙️ Container Configuration
|
|
|
|
- Flask container:
|
|
- Dockerized using `python:3.11-slim` as the base image.
|
|
- `flask` and `psycopg2-binary` installed.
|
|
- `app.py` and `index.html` are copied into the container.
|
|
- Uses port `5000`.
|
|
|
|
- PostgreSQL container:
|
|
- Uses `docker-entrypoint-initdb.d/init.sql` to initialize the database (`userdb`) and create the `users` table on first run.
|
|
- Uses environment variables for default credentials and database name.
|
|
|
|
---
|
|
|
|
|
|
## 🛠️ Instructions
|
|
|
|
### ✅ Prepare the Application
|
|
|
|
```bash
|
|
# Create namespace
|
|
kubectl apply -f kubi/namespace.yaml
|
|
|
|
# Create persistent volume and claim
|
|
kubectl apply -f kubi/pv.yaml
|
|
kubectl apply -f kubi/pvc.yaml
|
|
|
|
# Deploy PostgreSQL StatefulSet and Service
|
|
kubectl apply -f kubi/postgres-statefulset.yaml
|
|
kubectl apply -f kubi/postgres-service.yaml
|
|
|
|
# Deploy Flask web app and Service
|
|
kubectl apply -f kubi/webapp-deployment.yaml
|
|
kubectl apply -f kubi/webapp-service.yaml
|
|
```
|
|
|
|
|
|
# Run the Application
|
|
kubectl get pods -n webform-app
|
|
|
|
# You can pause the app using:
|
|
kubectl scale deployment web-app --replicas=0 -n webform-app
|
|
|
|
# Delete the app
|
|
kubectl delete -f kubi/ --recursive
|
|
|
|
# to view the app in browser
|
|
# find the NodePort exposed by the web app
|
|
kubectl get svc -n webform-app
|
|
|
|
# Then open broswerr and go to
|
|
http://<your-node-ip>:<node-port>
|
|
|
|
# Since its minikube use
|
|
minikube service web-app-service -n webform-app
|
|
|
|
|