Assignment

This commit is contained in:
Sabareesan Murugesan 2026-04-29 00:05:53 +02:00
parent 9b52815bce
commit cb6d85dd96
10 changed files with 187 additions and 0 deletions

12
Z2/app/Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

9
Z2/app/package.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "k8s-app",
"version": "1.0.0",
"main": "server.js",
"dependencies": {
"express": "^4.18.2",
"pg": "^8.11.0"
}
}

25
Z2/app/server.js Normal file
View File

@ -0,0 +1,25 @@
const express = require("express");
const { Pool } = require("pg");
const app = express();
const pool = new Pool({
host: "postgres-service",
user: "postgres",
password: "postgres",
database: "testdb",
port: 5432,
});
app.get("/", async (req, res) => {
try {
const result = await pool.query("SELECT NOW()");
res.send("DB Time: " + result.rows[0].now);
} catch (err) {
res.send("Database not ready");
}
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});

21
Z2/k8s/deployment.yaml Normal file
View File

@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: my-app-namespace
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: my-web-app:latest
imagePullPolicy: Never
ports:
- containerPort: 3000

4
Z2/k8s/namespace.yaml Normal file
View File

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: my-app-namespace

13
Z2/k8s/service.yaml Normal file
View File

@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: web-service
namespace: my-app-namespace
spec:
type: NodePort
selector:
app: web-app
ports:
- port: 80
targetPort: 3000
nodePort: 30007

73
Z2/k8s/statefulset.yaml Normal file
View File

@ -0,0 +1,73 @@
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/s/docker/Z2/data/postgres"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
namespace: my-app-namespace
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: postgres-service
namespace: my-app-namespace
spec:
ports:
- port: 5432
clusterIP: None
selector:
app: postgres
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: my-app-namespace
spec:
serviceName: "postgres-service"
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:15
env:
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_DB
value: testdb
ports:
- containerPort: 5432
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: postgres-storage
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi

View File

@ -0,0 +1,9 @@
#!/bin/bash
echo "Building Docker image..."
docker build -t my-web-app:latest ./app
echo "Creating volume directory..."
mkdir -p ./data/postgres
echo "Done."

15
Z2/scripts/start-app.sh Normal file
View File

@ -0,0 +1,15 @@
#!/bin/bash
echo "Creating namespace..."
kubectl apply -f k8s/namespace.yaml
echo "Deploying database..."
kubectl apply -f k8s/statefulset.yaml
echo "Deploying web app..."
kubectl apply -f k8s/deployment.yaml
echo "Creating service..."
kubectl apply -f k8s/service.yaml
echo "App started!"

6
Z2/scripts/stop-app.sh Normal file
View File

@ -0,0 +1,6 @@
#!/bin/bash
echo "Deleting all resources..."
kubectl delete namespace my-app-namespace
echo "App stopped!"