3.4 KiB
3.4 KiB
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
-
web-app
- A Python Flask-based container serving the HTML form and handling form submission.
- Connects to the PostgreSQL container using environment variables.
-
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 custominit.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.
- A named volume is mounted at
⚙️ Container Configuration
-
Flask container:
- Dockerized using
python:3.11-slim
as the base image. flask
andpsycopg2-binary
installed.app.py
andindex.html
are copied into the container.- Uses port
5000
.
- Dockerized using
-
PostgreSQL container:
- Uses
docker-entrypoint-initdb.d/init.sql
to initialize the database (userdb
) and create theusers
table on first run. - Uses environment variables for default credentials and database name.
- Uses
🛠️ Instructions
✅ Prepare the Application
# 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://:
Since its minikube use
minikube service web-app-service -n webform-app