zkt25/assignment2/README.md

137 lines
4.2 KiB
Markdown

# 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 |
| `ConfigMap` | Created by script | Stores `init-db.sql` used to create the `weather_log` table |
---
## Networking & Volumes
### Virtual Networks:
- Kubernetes handles inter-service communication via internal DNS and `ClusterIP` services.
- Frontend and backend are externally reachable using `NodePort` services.
### Volumes:
- `PersistentVolume` and `PersistentVolumeClaim` ensure PostgreSQL data is retained.
- `ConfigMap` mounts the SQL init script for PostgreSQL setup.
---
## ⚙️ Container Configurations
### Frontend
- Dockerfile uses `nginx:alpine`
- Serves `weather.html` (renamed as `index.html` inside container)
- Exposed on port `80`
### Node.js API
- Built with Node.js 18
- Listens on port `3000`
- Connects to PostgreSQL using service DNS
- Accepts POST requests at `/log` with weather data
### PostgreSQL
- `postgres:15` with `weatheruser`, `weatherpass`, and `weatherdb`
- Init script creates a `weather_log` table with a sample row
---
## How to Use the Application
### 1. Build Docker Images
```bash
./prepare-app.sh
```
### 2. Start the App (create namespace and apply all configs)
```bash
./start-app.sh
```
### 3. View the App
```bash
minikube service weather-service -n webapp-ns
```
Or get the NodePort manually:
```bash
kubectl get svc -n webapp-ns
minikube ip
```
Then open:
```
http://<minikube-ip>:<frontend-nodeport>
```
### 4. Use the App
- Enter a city
- Weather will be shown
- Data is logged in PostgreSQL automatically
### 5. Stop and Clean Up
```bash
./stop-app.sh
```
---
## API Key Setup
- Register at [OpenWeatherMap](https://openweathermap.org/api)
- Get your free API key
- Replace the placeholder in `weather.html`:
```js
const apiKey = "YOUR_API_KEY";
```
---
## Conclusion
- Frontend calls OpenWeatherMap and logs data to your backend
- PostgreSQL is prepped with a table to store queries
- You can expand the API to serve saved data or stats