113 lines
3.7 KiB
Markdown
113 lines
3.7 KiB
Markdown
# Zadanie 2 - Kubernetes web application
|
|
|
|
This project deploys a simple web application into Kubernetes. The user enters a name in the browser, the frontend sends it to the backend API, and the backend stores the value in PostgreSQL. The saved names are then displayed back in the browser.
|
|
|
|
## Used containers
|
|
|
|
- `z2-frontend:latest` - custom Nginx image serving the static frontend and proxying API requests to the backend service.
|
|
- `z2-backend:latest` - custom Node.js and Express API image that stores and reads user data from PostgreSQL.
|
|
- `postgres:15-alpine` - database container running inside a StatefulSet with persistent storage.
|
|
|
|
## Kubernetes objects
|
|
|
|
- `Namespace zkt26-z2` - isolates all application resources into one namespace.
|
|
- `Deployment backend-deployment` - runs the backend API container.
|
|
- `Deployment frontend-deployment` - runs the frontend Nginx container.
|
|
- `Service backend-service` - exposes the backend inside the cluster.
|
|
- `Service frontend-service` - exposes the frontend on NodePort `30080`.
|
|
- `Service postgres-service` - stable network endpoint for PostgreSQL.
|
|
- `PersistentVolume postgres-pv` - host storage for PostgreSQL data.
|
|
- `PersistentVolumeClaim postgres-pvc` - binds storage for the database pod.
|
|
- `StatefulSet postgres-statefulset` - runs PostgreSQL with persistent data.
|
|
|
|
## Networks and volumes
|
|
|
|
The application uses the default Kubernetes cluster networking. Pods communicate with each other through Kubernetes services:
|
|
|
|
- `frontend-service` for browser access
|
|
- `backend-service` for frontend to backend communication
|
|
- `postgres-service` for backend to database communication
|
|
|
|
Persistent data is stored using:
|
|
|
|
- `PersistentVolume postgres-pv`
|
|
- `PersistentVolumeClaim postgres-pvc`
|
|
|
|
The volume uses `hostPath` at `/tmp/zkt26-postgres-data`, so the database data remains available even after the pod restarts.
|
|
|
|
## Container configuration
|
|
|
|
The frontend container is based on Nginx and includes a custom `nginx.conf` that proxies `/api/*` requests to the backend service.
|
|
|
|
The backend container uses environment variables for the database connection:
|
|
|
|
- `DB_HOST=postgres-service`
|
|
- `DB_PORT=5432`
|
|
- `DB_USER=user`
|
|
- `DB_PASSWORD=password`
|
|
- `DB_NAME=mydb`
|
|
|
|
The PostgreSQL container is configured with:
|
|
|
|
- `POSTGRES_USER=user`
|
|
- `POSTGRES_PASSWORD=password`
|
|
- `POSTGRES_DB=mydb`
|
|
|
|
## How to prepare, start, stop and delete the application
|
|
|
|
Run the commands from the `z2` directory:
|
|
|
|
```bash
|
|
chmod +x prepare-app.sh start-app.sh stop-app.sh
|
|
./prepare-app.sh
|
|
./start-app.sh
|
|
```
|
|
|
|
To stop and delete the application:
|
|
|
|
```bash
|
|
./stop-app.sh
|
|
```
|
|
|
|
## How to pause the application
|
|
|
|
The application can be paused by scaling deployments and the stateful set to zero:
|
|
|
|
```bash
|
|
kubectl scale deployment frontend-deployment --replicas=0 -n zkt26-z2
|
|
kubectl scale deployment backend-deployment --replicas=0 -n zkt26-z2
|
|
kubectl scale statefulset postgres-statefulset --replicas=0 -n zkt26-z2
|
|
```
|
|
|
|
To start it again:
|
|
|
|
```bash
|
|
kubectl scale deployment frontend-deployment --replicas=1 -n zkt26-z2
|
|
kubectl scale deployment backend-deployment --replicas=1 -n zkt26-z2
|
|
kubectl scale statefulset postgres-statefulset --replicas=1 -n zkt26-z2
|
|
```
|
|
|
|
## How to open the web application
|
|
|
|
After starting the application in a standard Kubernetes environment, open the browser at:
|
|
|
|
- `http://localhost:30080`
|
|
|
|
If you are using Minikube in WSL with the Docker driver, get the real browser URL with:
|
|
|
|
```bash
|
|
minikube service frontend-service -n zkt26-z2 --url
|
|
```
|
|
|
|
Keep that terminal open and open the printed URL in the browser.
|
|
|
|
If your Kubernetes environment does not expose NodePort on localhost directly, or if you want a fixed local port, use:
|
|
|
|
```bash
|
|
kubectl port-forward service/frontend-service 8080:80 -n zkt26-z2
|
|
```
|
|
|
|
Then open:
|
|
|
|
- `http://localhost:8080`
|