zkt25/assignment2
2025-04-18 08:33:32 +02:00
..
.prepare-app.sh.swp reuploading assignments to git 2025-04-15 21:41:17 +02:00
deployment-api.yaml fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
deployment.yaml reuploading assignments to git 2025-04-15 21:41:17 +02:00
Dockerfile reuploading assignments to git 2025-04-15 21:41:17 +02:00
Dockerfile.api reuploading assignments to git 2025-04-15 21:41:17 +02:00
init-db.sql reuploading assignments to git 2025-04-15 21:41:17 +02:00
inti-db.sql fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
minikube-linux-amd64 reuploading assignments to git 2025-04-15 21:41:17 +02:00
postgres-service.yaml fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
prepare-app.sh fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
README.md fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
server.js fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
service.yaml reuploading assignments to git 2025-04-15 21:41:17 +02:00
start-app.sh reuploading assignments to git 2025-04-15 21:41:17 +02:00
statefulset.yaml fixed all the postgreSQL issues 2025-04-18 08:33:32 +02:00
stop-app.sh reuploading assignments to git 2025-04-15 21:41:17 +02:00
weather.html reuploading assignments to git 2025-04-15 21:41:17 +02:00

Weather Web App - Kubernetes Deployment

This project deploys a full-stack weather web application to Kubernetes. It includes:

  • A frontend website to search weather by city
  • A backend PostgreSQL database for logging searches
  • A Node.js API to connect the frontend to the database

Application Description

  • Frontend (weather.html): A responsive, modern web UI that shows current weather data using the OpenWeatherMap API.
  • Backend (PostgreSQL): Stores weather search history via a weather_log table.
  • API Server (Node.js): Receives weather data from the frontend and logs it to the PostgreSQL database.

Containers Used

Container Image Description
weather-frontend Custom Nginx-based Serves the weather.html UI
postgres postgres:15 Provides relational database backend
weather-api Custom Node.js API that logs weather data to PostgreSQL

☸️ Kubernetes Objects

Object Type File Description
Namespace (inside script) Isolates resources under webapp-ns
Deployment deployment.yaml Manages frontend app
Deployment deployment-api.yaml Manages the Node.js API
StatefulSet statefulset.yaml Manages PostgreSQL instance with persistent volume
PersistentVolume statefulset.yaml Host-mounted volume for PostgreSQL data
PersistentVolumeClaim statefulset.yaml Requests storage for StatefulSet
Service service.yaml Exposes frontend via NodePort
Service deployment-api.yaml Exposes API via NodePort
Service postgres-service.yaml Internal ClusterIP service for PostgreSQL DNS resolution
ConfigMap db-init-script Stores the SQL init script for table creation

Networking & Volumes

Virtual Networks:

  • Kubernetes services provide internal DNS for postgres and weather-api-service.
  • Frontend and API exposed externally using NodePort services.

Volumes:

  • PersistentVolume and PersistentVolumeClaim ensure PostgreSQL data is retained.
  • ConfigMap used to apply init-db.sql manually via psql.

⚙️ Container Configurations

Frontend

  • Dockerfile uses nginx:alpine
  • Serves weather.html
  • Exposed on port 80

Node.js API

  • Built with Node.js 18
  • Listens on port 3000
  • Connects to PostgreSQL using service DNS postgres
  • Accepts POST requests at /log

PostgreSQL

  • postgres:15 with weatheruser, weatherpass, and weatherdb
  • Table weather_log is created manually after StatefulSet starts

How to Use the Application

1. Build Docker Images

./prepare-app.sh

2. Start the App (create namespace and apply all configs)

./start-app.sh

3. Manually Initialize the Database

kubectl exec -it -n webapp-ns statefulset/postgres -- psql -U weatheruser -d weatherdb

Paste this inside the prompt:

CREATE TABLE IF NOT EXISTS weather_log (
    id SERIAL PRIMARY KEY,
    city VARCHAR(100),
    temperature DECIMAL(5,2),
    description TEXT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Exit with:

\q

4. View the App

minikube service weather-service -n webapp-ns

Or manually:

minikube ip
kubectl get svc -n webapp-ns

Then open:

http://<minikube-ip>:<frontend-nodeport>

5. Use the App

  • Search for a city
  • Weather appears
  • Weather data is logged via API to PostgreSQL

6. View Logged Data

kubectl exec -it -n webapp-ns statefulset/postgres -- psql -U weatheruser -d weatherdb
SELECT * FROM weather_log ORDER BY timestamp DESC;

7. Stop and Clean Up

./stop-app.sh

API Key Setup

const apiKey = "YOUR_API_KEY";

Conclusion

  • Frontend fetches real-time weather from OpenWeatherMap
  • Logs are POSTed to backend and stored in PostgreSQL