This commit is contained in:
Bohdan Kapliuk 2026-04-22 14:31:48 +03:00
parent 52dac28335
commit 2d0d0f4e6f

View File

@ -1,45 +1,45 @@
# Zadanie 2 - Kubernetes web application # Zadanie 2 - webova aplikacia v Kubernetes
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. Tento projekt nasadzuje jednoduchu webovu aplikaciu do Kubernetes. Pouzivatel zada meno v prehliadaci, frontend ho odosle backend API a backend ulozi hodnotu do PostgreSQL. Ulozene mena sa potom znova zobrazia v prehliadaci.
## Used containers ## Pouzite kontajnery
- `z2-frontend:latest` - custom Nginx image serving the static frontend and proxying API requests to the backend service. - `z2-frontend:latest` - vlastny obraz Nginx, ktory obsluhuje staticky frontend a proxyuje API poziadavky na backendovu sluzbu.
- `z2-backend:latest` - custom Node.js and Express API image that stores and reads user data from PostgreSQL. - `z2-backend:latest` - vlastny obraz Node.js a Express API, ktory uklada a cita pouzivatelske udaje z PostgreSQL.
- `postgres:15-alpine` - database container running inside a StatefulSet with persistent storage. - `postgres:15-alpine` - databazovy kontajner bezici v StatefulSete s trvalym uloziskom.
## Kubernetes objects ## Objekty Kubernetes
- `Namespace zkt26-z2` - isolates all application resources into one namespace. - `Namespace zkt26-z2` - oddeluje vsetky prostriedky aplikacie do jedneho namespace.
- `Deployment backend-deployment` - runs the backend API container. - `Deployment backend-deployment` - spusta backend API kontajner.
- `Deployment frontend-deployment` - runs the frontend Nginx container. - `Deployment frontend-deployment` - spusta frontendovy Nginx kontajner.
- `Service backend-service` - exposes the backend inside the cluster. - `Service backend-service` - spristupnuje backend vo vnutri klastra.
- `Service frontend-service` - exposes the frontend on NodePort `30080`. - `Service frontend-service` - spristupnuje frontend cez NodePort `30080`.
- `Service postgres-service` - stable network endpoint for PostgreSQL. - `Service postgres-service` - stabilny sietovy endpoint pre PostgreSQL.
- `PersistentVolume postgres-pv` - host storage for PostgreSQL data. - `PersistentVolume postgres-pv` - hostitelske ulozisko pre data PostgreSQL.
- `PersistentVolumeClaim postgres-pvc` - binds storage for the database pod. - `PersistentVolumeClaim postgres-pvc` - pripaja ulozisko pre databazovy pod.
- `StatefulSet postgres-statefulset` - runs PostgreSQL with persistent data. - `StatefulSet postgres-statefulset` - spusta PostgreSQL s trvalymi datami.
## Networks and volumes ## Siete a zvazky
The application uses the default Kubernetes cluster networking. Pods communicate with each other through Kubernetes services: Aplikacia pouziva predvolene sietovanie klastra Kubernetes. Pody medzi sebou komunikuju cez Kubernetes sluzby:
- `frontend-service` for browser access - `frontend-service` pre pristup z prehliadaca
- `backend-service` for frontend to backend communication - `backend-service` pre komunikaciu frontendu s backendom
- `postgres-service` for backend to database communication - `postgres-service` pre komunikaciu backendu s databazou
Persistent data is stored using: Trvale data sa ukladaju pomocou:
- `PersistentVolume postgres-pv` - `PersistentVolume postgres-pv`
- `PersistentVolumeClaim postgres-pvc` - `PersistentVolumeClaim postgres-pvc`
The volume uses `hostPath` at `/tmp/zkt26-postgres-data`, so the database data remains available even after the pod restarts. Zvazok pouziva `hostPath` na adrese `/tmp/zkt26-postgres-data`, takze data databazy ostanu dostupne aj po restarte podu.
## Container configuration ## Konfiguracia kontajnerov
The frontend container is based on Nginx and includes a custom `nginx.conf` that proxies `/api/*` requests to the backend service. Frontendovy kontajner je zalozeny na Nginx a obsahuje vlastny `nginx.conf`, ktory proxyuje poziadavky `/api/*` na backendovu sluzbu.
The backend container uses environment variables for the database connection: Backendovy kontajner pouziva pre pripojenie k databaze tieto premenne prostredia:
- `DB_HOST=postgres-service` - `DB_HOST=postgres-service`
- `DB_PORT=5432` - `DB_PORT=5432`
@ -47,15 +47,15 @@ The backend container uses environment variables for the database connection:
- `DB_PASSWORD=password` - `DB_PASSWORD=password`
- `DB_NAME=mydb` - `DB_NAME=mydb`
The PostgreSQL container is configured with: PostgreSQL kontajner je nakonfigurovany takto:
- `POSTGRES_USER=user` - `POSTGRES_USER=user`
- `POSTGRES_PASSWORD=password` - `POSTGRES_PASSWORD=password`
- `POSTGRES_DB=mydb` - `POSTGRES_DB=mydb`
## How to prepare, start, stop and delete the application ## Ako aplikaciu pripravit, spustit, zastavit a odstranit
Run the commands from the `z2` directory: Prikazy spustite z adresara `z2`:
```bash ```bash
chmod +x prepare-app.sh start-app.sh stop-app.sh chmod +x prepare-app.sh start-app.sh stop-app.sh
@ -63,15 +63,15 @@ chmod +x prepare-app.sh start-app.sh stop-app.sh
./start-app.sh ./start-app.sh
``` ```
To stop and delete the application: Na zastavenie a odstranenie aplikacie pouzite:
```bash ```bash
./stop-app.sh ./stop-app.sh
``` ```
## How to pause the application ## Ako aplikaciu pozastavit
The application can be paused by scaling deployments and the stateful set to zero: Aplikaciu je mozne pozastavit zmenou poctu replik deploymentov a statefulsetu na nulu:
```bash ```bash
kubectl scale deployment frontend-deployment --replicas=0 -n zkt26-z2 kubectl scale deployment frontend-deployment --replicas=0 -n zkt26-z2
@ -79,7 +79,7 @@ kubectl scale deployment backend-deployment --replicas=0 -n zkt26-z2
kubectl scale statefulset postgres-statefulset --replicas=0 -n zkt26-z2 kubectl scale statefulset postgres-statefulset --replicas=0 -n zkt26-z2
``` ```
To start it again: Na jej opatovne spustenie:
```bash ```bash
kubectl scale deployment frontend-deployment --replicas=1 -n zkt26-z2 kubectl scale deployment frontend-deployment --replicas=1 -n zkt26-z2
@ -87,26 +87,26 @@ kubectl scale deployment backend-deployment --replicas=1 -n zkt26-z2
kubectl scale statefulset postgres-statefulset --replicas=1 -n zkt26-z2 kubectl scale statefulset postgres-statefulset --replicas=1 -n zkt26-z2
``` ```
## How to open the web application ## Ako otvorit webovu aplikaciu
After starting the application in a standard Kubernetes environment, open the browser at: Po spusteni aplikacie v standardnom Kubernetes prostredi otvorte prehliadac na adrese:
- `http://localhost:30080` - `http://localhost:30080`
If you are using Minikube in WSL with the Docker driver, get the real browser URL with: Ak pouzivate Minikube vo WSL s Docker driverom, skutocnu URL pre prehliadac ziskate pomocou:
```bash ```bash
minikube service frontend-service -n zkt26-z2 --url minikube service frontend-service -n zkt26-z2 --url
``` ```
Keep that terminal open and open the printed URL in the browser. Nechajte tento terminal otvoreny a otvorte vypisanu URL v prehliadaci.
If your Kubernetes environment does not expose NodePort on localhost directly, or if you want a fixed local port, use: Ak vase Kubernetes prostredie nespristupnuje NodePort priamo na localhoste alebo chcete pevny lokalny port, pouzite:
```bash ```bash
kubectl port-forward service/frontend-service 8080:80 -n zkt26-z2 kubectl port-forward service/frontend-service 8080:80 -n zkt26-z2
``` ```
Then open: Potom otvorte:
- `http://localhost:8080` - `http://localhost:8080`