From 0f00f7ad314f8d533dc93489722de55f7874b731 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 6 May 2025 18:45:17 +0200 Subject: [PATCH] Finalized sk1 project for submission --- z2/Dockerfile | 9 ---- z2/README.md | 32 ------------- z2/app/main.py | 81 --------------------------------- z2/app/requirements.txt | 5 -- z2/k8s/deployment.yaml | 32 ------------- z2/k8s/migrate-job.yaml | 12 ----- z2/k8s/namespace.yaml | 4 -- z2/k8s/postgres-deployment.yaml | 57 ----------------------- z2/k8s/service.yaml | 13 ------ z2/prepare-app.sh | 10 ---- z2/start-app.sh | 18 -------- z2/stop-app.sh | 18 -------- 12 files changed, 291 deletions(-) delete mode 100644 z2/Dockerfile delete mode 100644 z2/README.md delete mode 100644 z2/app/main.py delete mode 100644 z2/app/requirements.txt delete mode 100644 z2/k8s/deployment.yaml delete mode 100644 z2/k8s/migrate-job.yaml delete mode 100644 z2/k8s/namespace.yaml delete mode 100644 z2/k8s/postgres-deployment.yaml delete mode 100644 z2/k8s/service.yaml delete mode 100755 z2/prepare-app.sh delete mode 100755 z2/start-app.sh delete mode 100755 z2/stop-app.sh diff --git a/z2/Dockerfile b/z2/Dockerfile deleted file mode 100644 index a7e0bc1..0000000 --- a/z2/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM python:3.9-slim - -WORKDIR /app - -COPY app/ . - -RUN pip install --no-cache-dir -r requirements.txt - -CMD ["python", "main.py"] diff --git a/z2/README.md b/z2/README.md deleted file mode 100644 index 413be49..0000000 --- a/z2/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Flask + PostgreSQL Kubernetes Deployment - -This project deploys a Flask web application with a PostgreSQL backend using Kubernetes. - -## 🛠 Structure - -- `app/`: Flask source code (`main.py`, `requirements.txt`) -- `k8s/`: Kubernetes manifests for deployments, services, namespace, etc. -- `Dockerfile`: Builds the Flask app image -- `prepare-app.sh`: Builds Docker image and prepares volumes -- `start-app.sh`: Applies all Kubernetes objects -- `stop-app.sh`: Deletes all Kubernetes resources -- `statefulset.yaml`: Defines StatefulSet, PV, and PVC - -## 🚀 Steps to Deploy - -1. **Prepare the application:** - ```bash - ./prepare-app.sh - - -hafzal03@LAPTOP-ELUS3HGM:~/mypro/z2$ kubectl get pods -n webapp-namespace -NAME READY STATUS RESTARTS AGE -flask-app-6b844bf6-cq9t6 1/1 Running 0 8m37s -postgres-644fc4c86d-l9h4f 1/1 Running 0 14m -hafzal03@LAPTOP-ELUS3HGM:~/mypro/z2$ minikube service flask-service -n webapp-namespace - -minikube service flask-service -n webapp-namespace - -kubectl get pods -n webapp-namespace - -kubectl rollout restart deployment flask-app -n webapp-namespace diff --git a/z2/app/main.py b/z2/app/main.py deleted file mode 100644 index 74c109e..0000000 --- a/z2/app/main.py +++ /dev/null @@ -1,81 +0,0 @@ -from flask import Flask, request, jsonify, render_template_string -import psycopg2 -import os - -app = Flask(__name__) - -def get_db_connection(): - return psycopg2.connect( - host=os.environ.get("POSTGRES_HOST"), - database=os.environ.get("POSTGRES_DB"), - user=os.environ.get("POSTGRES_USER"), - password=os.environ.get("POSTGRES_PASSWORD"), - port=os.environ.get("POSTGRES_PORT", 5432), - ) - -def init_db(): - conn = get_db_connection() - cur = conn.cursor() - cur.execute(""" - CREATE TABLE IF NOT EXISTS students ( - id SERIAL PRIMARY KEY, - name TEXT NOT NULL, - room_number TEXT NOT NULL, - faculty TEXT NOT NULL - ); - """) - conn.commit() - cur.close() - conn.close() - -@app.route("/") -def index(): - return "

Welcome!

Go to /add to add a student, /students to view all.

" - -# HTML Form + POST Submission -@app.route("/add", methods=["GET", "POST"]) -def add_student(): - if request.method == "POST": - name = request.form.get("name") - room_number = request.form.get("room_number") - faculty = request.form.get("faculty") - - if not name or not room_number or not faculty: - return "All fields are required!", 400 - - conn = get_db_connection() - cur = conn.cursor() - cur.execute("INSERT INTO students (name, room_number, faculty) VALUES (%s, %s, %s)", (name, room_number, faculty)) - conn.commit() - cur.close() - conn.close() - return "

Student added successfully!

Add another" - - # HTML form - return render_template_string(""" -

Add Student

-
- Name:
- Room Number:
- Faculty:
- -
- """) - -# View all students -@app.route("/students", methods=["GET"]) -def get_students(): - conn = get_db_connection() - cur = conn.cursor() - cur.execute("SELECT id, name, room_number, faculty FROM students") - rows = cur.fetchall() - cur.close() - conn.close() - - return jsonify([ - {"id": row[0], "name": row[1], "room_number": row[2], "faculty": row[3]} for row in rows - ]) - -if __name__ == "__main__": - init_db() - app.run(host="0.0.0.0", port=8000) diff --git a/z2/app/requirements.txt b/z2/app/requirements.txt deleted file mode 100644 index 3a4074c..0000000 --- a/z2/app/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -Flask==2.0.1 -Werkzeug==2.0.3 -psycopg2-binary==2.9.1 -python-dotenv==0.19.0 -Jinja2==3.0.3 diff --git a/z2/k8s/deployment.yaml b/z2/k8s/deployment.yaml deleted file mode 100644 index 0dd90fc..0000000 --- a/z2/k8s/deployment.yaml +++ /dev/null @@ -1,32 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: flask-app - namespace: webapp-namespace -spec: - replicas: 1 - selector: - matchLabels: - app: flask - template: - metadata: - labels: - app: flask - spec: - containers: - - name: flask - image: flask-app:latest - imagePullPolicy: Never - ports: - - containerPort: 8000 - env: - - name: POSTGRES_HOST - value: postgres - - name: POSTGRES_DB - value: mydatabase - - name: POSTGRES_USER - value: myuser - - name: POSTGRES_PASSWORD - value: mypassword - - name: POSTGRES_PORT - value: "5432" diff --git a/z2/k8s/migrate-job.yaml b/z2/k8s/migrate-job.yaml deleted file mode 100644 index b7d81d8..0000000 --- a/z2/k8s/migrate-job.yaml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: batch/v1 -kind: Job -metadata: - name: migrate -spec: - template: - spec: - containers: - - name: migrator - image: myapp:latest - command: ["python", "manage.py", "migrate"] - restartPolicy: Never diff --git a/z2/k8s/namespace.yaml b/z2/k8s/namespace.yaml deleted file mode 100644 index 2ef198a..0000000 --- a/z2/k8s/namespace.yaml +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: v1 -kind: Namespace -metadata: - name: webapp-namespace diff --git a/z2/k8s/postgres-deployment.yaml b/z2/k8s/postgres-deployment.yaml deleted file mode 100644 index aaf3c1d..0000000 --- a/z2/k8s/postgres-deployment.yaml +++ /dev/null @@ -1,57 +0,0 @@ -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: postgres-pvc - namespace: webapp-namespace -spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: postgres - namespace: webapp-namespace -spec: - replicas: 1 - selector: - matchLabels: - app: postgres - template: - metadata: - labels: - app: postgres - spec: - containers: - - name: postgres - image: postgres:13 - env: - - name: POSTGRES_DB - value: mydatabase - - name: POSTGRES_USER - value: myuser - - name: POSTGRES_PASSWORD - value: mypassword - ports: - - containerPort: 5432 - volumeMounts: - - name: postgres-storage - mountPath: /var/lib/postgresql/data - volumes: - - name: postgres-storage - persistentVolumeClaim: - claimName: postgres-pvc ---- -apiVersion: v1 -kind: Service -metadata: - name: postgres - namespace: webapp-namespace -spec: - selector: - app: postgres - ports: - - port: 5432 diff --git a/z2/k8s/service.yaml b/z2/k8s/service.yaml deleted file mode 100644 index 2d83fc9..0000000 --- a/z2/k8s/service.yaml +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: flask-service - namespace: webapp-namespace -spec: - selector: - app: flask - type: NodePort - ports: - - port: 8000 - targetPort: 8000 - nodePort: 30080 # <-- Changed this diff --git a/z2/prepare-app.sh b/z2/prepare-app.sh deleted file mode 100755 index 2090316..0000000 --- a/z2/prepare-app.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -echo "Building Docker image for Flask app..." -docker build -t flask-app:latest . - -echo "Creating Minikube image cache (if using Minikube)..." -eval $(minikube docker-env) -docker build -t flask-app:latest . - -echo "Preparation complete." diff --git a/z2/start-app.sh b/z2/start-app.sh deleted file mode 100755 index a869e7a..0000000 --- a/z2/start-app.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -echo "Creating Namespace..." -kubectl apply -f k8s/namespace.yaml - -echo "Creating Persistent Volumes and StatefulSet..." -kubectl apply -f k8s/statefulset.yaml -n webapp-namespace - -echo "Deploying PostgreSQL..." -kubectl apply -f k8s/postgres-deployment.yaml -n webapp-namespace - -echo "Deploying Flask App..." -kubectl apply -f k8s/deployment.yaml -n webapp-namespace - -echo "Creating Service..." -kubectl apply -f k8s/service.yaml -n webapp-namespace - -echo "All resources have been applied." diff --git a/z2/stop-app.sh b/z2/stop-app.sh deleted file mode 100755 index 22a14d6..0000000 --- a/z2/stop-app.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -echo "Deleting Flask App Deployment..." -kubectl delete -f k8s/deployment.yaml -n webapp-namespace - -echo "Deleting Flask Service..." -kubectl delete -f k8s/service.yaml -n webapp-namespace - -echo "Deleting PostgreSQL Deployment and PVC..." -kubectl delete -f k8s/postgres-deployment.yaml -n webapp-namespace - -echo "Deleting StatefulSet and PVs..." -kubectl delete -f k8s/statefulset.yaml -n webapp-namespace - -echo "Deleting Namespace..." -kubectl delete -f k8s/namespace.yaml - -echo "All resources have been deleted."