Upload files to "z2"
uploading necessaery files
This commit is contained in:
parent
da9ad67534
commit
39530b2a80
7
z2/Dockerfile
Normal file
7
z2/Dockerfile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
FROM node:18
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package.json .
|
||||||
|
RUN npm install
|
||||||
|
COPY . .
|
||||||
|
EXPOSE 3000
|
||||||
|
CMD ["node", "server.js"]
|
||||||
236
z2/README.md
Normal file
236
z2/README.md
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
1. Application Description
|
||||||
|
This project is a simple full-stack web application deployed on Kubernetes.
|
||||||
|
It allows users to submit names through a web interface and store them in a PostgreSQL database.
|
||||||
|
The system consists of:
|
||||||
|
|
||||||
|
|
||||||
|
A frontend (static HTML interface)
|
||||||
|
|
||||||
|
|
||||||
|
A backend API built with Node.js (Express)
|
||||||
|
|
||||||
|
|
||||||
|
A PostgreSQL database running as a StatefulSet with persistent storage
|
||||||
|
|
||||||
|
|
||||||
|
The backend provides REST endpoints:
|
||||||
|
|
||||||
|
|
||||||
|
POST /add → adds a name to the database
|
||||||
|
|
||||||
|
|
||||||
|
GET /names → retrieves all stored names
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
2. Containers Used
|
||||||
|
Backend (Node.js)
|
||||||
|
|
||||||
|
|
||||||
|
Image: docker-app-backend:latest
|
||||||
|
|
||||||
|
|
||||||
|
Purpose: Handles API requests and communicates with the database
|
||||||
|
|
||||||
|
|
||||||
|
Database (PostgreSQL)
|
||||||
|
|
||||||
|
|
||||||
|
Image: postgres:15
|
||||||
|
|
||||||
|
|
||||||
|
Purpose: Stores application data persistently
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
3. Kubernetes Objects
|
||||||
|
Namespace
|
||||||
|
|
||||||
|
|
||||||
|
Name: myapp
|
||||||
|
|
||||||
|
|
||||||
|
Used to isolate all application resources
|
||||||
|
|
||||||
|
|
||||||
|
Deployment
|
||||||
|
|
||||||
|
|
||||||
|
Name: backend
|
||||||
|
|
||||||
|
|
||||||
|
Runs the Node.js backend
|
||||||
|
|
||||||
|
|
||||||
|
Ensures availability and automatic restart of the backend container
|
||||||
|
|
||||||
|
|
||||||
|
StatefulSet
|
||||||
|
|
||||||
|
|
||||||
|
Name: db
|
||||||
|
|
||||||
|
|
||||||
|
Runs PostgreSQL
|
||||||
|
|
||||||
|
|
||||||
|
Provides stable identity and persistent storage
|
||||||
|
|
||||||
|
|
||||||
|
PersistentVolume (PV)
|
||||||
|
|
||||||
|
|
||||||
|
Stores PostgreSQL data on node storage
|
||||||
|
|
||||||
|
|
||||||
|
PersistentVolumeClaim (PVC)
|
||||||
|
|
||||||
|
|
||||||
|
Automatically created via StatefulSet
|
||||||
|
|
||||||
|
|
||||||
|
Requests storage for database persistence
|
||||||
|
|
||||||
|
|
||||||
|
Services
|
||||||
|
backend-service (NodePort)
|
||||||
|
|
||||||
|
|
||||||
|
Exposes backend externally
|
||||||
|
|
||||||
|
|
||||||
|
Accessible at:
|
||||||
|
http://localhost:30007
|
||||||
|
|
||||||
|
|
||||||
|
db service (ClusterIP)
|
||||||
|
|
||||||
|
|
||||||
|
Enables internal communication between backend and database
|
||||||
|
|
||||||
|
|
||||||
|
Backend connects using hostname: db
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
4. Networking
|
||||||
|
|
||||||
|
|
||||||
|
Kubernetes DNS allows communication between differents components of the application using service names. Backend communicates with database using db and external users access backend via NodePort service
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
5. Persistent Storage
|
||||||
|
PostgreSQL uses:
|
||||||
|
PersistentVolume (PV) and PersistentVolumeClaim (PVC)
|
||||||
|
|
||||||
|
Using these ensures that data is not lost after pod restart and application state is preserved
|
||||||
|
|
||||||
|
|
||||||
|
6. Container Configuration
|
||||||
|
Backend
|
||||||
|
|
||||||
|
|
||||||
|
Port: 3000
|
||||||
|
|
||||||
|
|
||||||
|
Database connection:
|
||||||
|
|
||||||
|
|
||||||
|
Host: db
|
||||||
|
|
||||||
|
|
||||||
|
User: user
|
||||||
|
|
||||||
|
|
||||||
|
Password: password
|
||||||
|
|
||||||
|
|
||||||
|
Database: mydb
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Database
|
||||||
|
|
||||||
|
|
||||||
|
Port: 5432
|
||||||
|
|
||||||
|
|
||||||
|
This uses persistent volume for data storage
|
||||||
|
|
||||||
|
|
||||||
|
7. Preparation
|
||||||
|
Build backend image:
|
||||||
|
docker build -t docker-app-backend ./backend
|
||||||
|
|
||||||
|
8. Deployment
|
||||||
|
Apply all Kubernetes resources:
|
||||||
|
kubectl apply -f namespace.yamlkubectl apply -f deployment.yamlkubectl apply -f service.yamlkubectl apply -f statefulset.yamlkubectl apply -f db-service.yaml
|
||||||
|
|
||||||
|
9. Checking Application Status
|
||||||
|
kubectl get all -n myappkubectl get pods -n myappkubectl get svc -n myapp
|
||||||
|
|
||||||
|
10. Accessing the Application
|
||||||
|
Backend API:
|
||||||
|
http://localhost:30007
|
||||||
|
Frontend:
|
||||||
|
Open frontend/index.html in a browser
|
||||||
|
|
||||||
|
11. Stopping the Application
|
||||||
|
kubectl delete -f deployment.yamlkubectl delete -f service.yamlkubectl delete -f statefulset.yamlkubectl delete -f db-service.yamlkubectl delete namespace myapp
|
||||||
|
|
||||||
|
12. Removing All Resources
|
||||||
|
kubectl delete namespace myapp
|
||||||
|
|
||||||
|
13. Used Resources
|
||||||
|
|
||||||
|
|
||||||
|
Kubernetes Documentation: https://kubernetes.io/docs/
|
||||||
|
|
||||||
|
|
||||||
|
Docker Documentation: https://docs.docker.com/
|
||||||
|
|
||||||
|
|
||||||
|
PostgreSQL Docker Image
|
||||||
|
|
||||||
|
|
||||||
|
Node.js Express Framework
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
14. Use of Artificial Intelligence
|
||||||
|
Artificial intelligence tools were used to:
|
||||||
|
|
||||||
|
|
||||||
|
Assist with debugging deployment issues
|
||||||
|
|
||||||
|
|
||||||
|
Improve configuration of Kubernetes objects
|
||||||
|
|
||||||
|
|
||||||
|
Help structure the project and documentation
|
||||||
|
|
||||||
|
|
||||||
|
All implementation, testing, and understanding were performed independently.
|
||||||
|
|
||||||
|
15. Summary
|
||||||
|
This project demonstrates:
|
||||||
|
|
||||||
|
|
||||||
|
Containerized web application architecture
|
||||||
|
|
||||||
|
|
||||||
|
Kubernetes Deployment and StatefulSet usage
|
||||||
|
|
||||||
|
|
||||||
|
Persistent storage with PVC
|
||||||
|
|
||||||
|
|
||||||
|
Internal service communication using DNS
|
||||||
|
|
||||||
|
|
||||||
|
External access using NodePort
|
||||||
|
|
||||||
|
|
||||||
|
The application is fully functional and can store and retrieve data using a Kubernetes-based backend and database. AI was use strictly for learninf concepts and debugging, all coding and implementation was done independently.
|
||||||
12
z2/db-service.yaml
Normal file
12
z2/db-service.yaml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: db
|
||||||
|
namespace: myapp
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: db
|
||||||
|
ports:
|
||||||
|
- port: 5432
|
||||||
|
targetPort: 5432
|
||||||
|
clusterIP: None
|
||||||
23
z2/deployment.yaml
Normal file
23
z2/deployment.yaml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: backend
|
||||||
|
namespace: myapp
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: backend
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: backend
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: backend
|
||||||
|
image: docker-app-backend:latest
|
||||||
|
imagePullPolicy: Never
|
||||||
|
ports:
|
||||||
|
- containerPort: 3000
|
||||||
|
|
||||||
|
|
||||||
4
z2/init.sql
Normal file
4
z2/init.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS names (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name TEXT
|
||||||
|
);
|
||||||
4
z2/namespace.yaml
Normal file
4
z2/namespace.yaml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Namespace
|
||||||
|
metadata:
|
||||||
|
name: myapp
|
||||||
10
z2/package.json
Normal file
10
z2/package.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "server.js",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"pg": "^8.11.3",
|
||||||
|
"cors": "^2.8.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
z2/prepare-app.sh
Normal file
3
z2/prepare-app.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
echo "Preparing..."
|
||||||
|
docker build -t docker-app-backend ./backend
|
||||||
33
z2/server.js
Normal file
33
z2/server.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const cors = require("cors");
|
||||||
|
const { Pool } = require("pg");
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
const pool = new Pool({
|
||||||
|
host: "db",
|
||||||
|
user: "user",
|
||||||
|
password: "password",
|
||||||
|
database: "mydb",
|
||||||
|
port: 5432,
|
||||||
|
});
|
||||||
|
|
||||||
|
pool.query("CREATE TABLE IF NOT EXISTS names (id SERIAL PRIMARY KEY, name TEXT);");
|
||||||
|
|
||||||
|
app.post("/add", async (req, res) => {
|
||||||
|
const { name } = req.body;
|
||||||
|
await pool.query("INSERT INTO names(name) VALUES($1)", [name]);
|
||||||
|
res.send("Added");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/names", async (req, res) => {
|
||||||
|
const result = await pool.query("SELECT * FROM names");
|
||||||
|
res.json(result.rows);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(3000, () => console.log("Server running"));
|
||||||
|
app.get("/", (req, res) => {
|
||||||
|
res.send("Backend is running 🚀");
|
||||||
|
});
|
||||||
13
z2/service.yaml
Normal file
13
z2/service.yaml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: backend-service
|
||||||
|
namespace: myapp
|
||||||
|
spec:
|
||||||
|
type: NodePort
|
||||||
|
selector:
|
||||||
|
app: backend
|
||||||
|
ports:
|
||||||
|
- port: 3000
|
||||||
|
targetPort: 3000
|
||||||
|
nodePort: 30007
|
||||||
7
z2/start-app.sh
Normal file
7
z2/start-app.sh
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
echo "Starting..."
|
||||||
|
|
||||||
|
kubectl apply -f namespace.yaml
|
||||||
|
kubectl apply -f deployment.yaml
|
||||||
|
kubectl apply -f service.yaml
|
||||||
|
kubectl apply -f statefulset.yaml
|
||||||
52
z2/statefulset.yaml
Normal file
52
z2/statefulset.yaml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolume
|
||||||
|
metadata:
|
||||||
|
name: db-pv
|
||||||
|
namespace: myapp
|
||||||
|
spec:
|
||||||
|
capacity:
|
||||||
|
storage: 1Gi
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
hostPath:
|
||||||
|
path: "/mnt/data"
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: db
|
||||||
|
namespace: myapp
|
||||||
|
spec:
|
||||||
|
serviceName: "db"
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: db
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: db
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: postgres
|
||||||
|
image: postgres:15
|
||||||
|
ports:
|
||||||
|
- containerPort: 5432
|
||||||
|
env:
|
||||||
|
- name: POSTGRES_USER
|
||||||
|
value: "user"
|
||||||
|
- name: POSTGRES_PASSWORD
|
||||||
|
value: "password"
|
||||||
|
- name: POSTGRES_DB
|
||||||
|
value: "mydb"
|
||||||
|
volumeMounts:
|
||||||
|
- name: db-storage
|
||||||
|
mountPath: /var/lib/postgresql/data
|
||||||
|
volumeClaimTemplates:
|
||||||
|
- metadata:
|
||||||
|
name: db-storage
|
||||||
|
spec:
|
||||||
|
accessModes: ["ReadWriteOnce"]
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 1Gi
|
||||||
7
z2/stop-app.sh
Normal file
7
z2/stop-app.sh
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
echo "Stopping..."
|
||||||
|
|
||||||
|
kubectl delete -f deployment.yaml
|
||||||
|
kubectl delete -f service.yaml
|
||||||
|
kubectl delete -f statefulset.yaml
|
||||||
|
kubectl delete namespace myapp
|
||||||
Loading…
Reference in New Issue
Block a user