diff --git a/z2-failed/README.md b/z2-failed/README.md new file mode 100644 index 0000000..2dade06 --- /dev/null +++ b/z2-failed/README.md @@ -0,0 +1,69 @@ +# Visitor Counter Application + +## Description of the Application +This application is designed to count the number of visits to a webpage. It utilizes Flask for the backend, Apache for serving static web content, and Redis for storing visitor counts. + +## List of Containers Used + +- **Backend Container**: Built using Flask, this container handles API requests and updates visitor counts in Redis. +- **Web Container**: Built using Apache, this container serves static HTML content. +- **Redis Container**: Uses the official Redis image to store visitor counts persistently. + +## List of Kubernetes Objects + +- **Deployments**: + - **Backend Deployment**: Manages the backend Flask application. + - **Web Deployment**: Manages the web Apache server. +- **Services**: + - **Backend Service**: Exposes the backend application on a specific port. + - **Web Service**: Exposes the web server on a specific port. + - **Redis Service**: Provides access to the Redis database. +- **StatefulSet**: + - **Redis StatefulSet**: Ensures persistent storage for Redis data. +- **PersistentVolumeClaim (PVC)** and **PersistentVolume (PV)**: + - Used for providing persistent storage to Redis. + +## Virtual Networks and Named Volumes + +- **Virtual Networks**: Kubernetes automatically manages pod-to-pod communication within a cluster. +- **Named Volumes**: Used for persistent storage in Redis, ensuring data is retained across pod restarts. + +## Container Configuration + +- **Backend Container**: Configured to listen on port 5000 and connect to Redis for storing visitor counts. +- **Web Container**: Configured to serve static HTML content on port 80. +- **Redis Container**: Configured to store data persistently using a PersistentVolumeClaim. + +## Instructions to Prepare, Run, Pause, and Delete the Application + +1. **Prepare the Application**: + Run the `prepare-app.sh` script to build Docker images and load them into Minikube. + +./prepare-app.sh + + +2. **Run the Application**: +Run the `start-app.sh` script to create all necessary Kubernetes objects and start the application. +./start-app.sh + + +3. **Pause the Application**: +You can pause the application by scaling down deployments to zero replicas: + +kubectl scale deployment backend-deployment --replicas=0 +kubectl scale deployment web-deployment --replicas=0 + + +4. **Delete the Application**: +Run the `stop-app.sh` script to delete all Kubernetes objects and stop the application. +./stop-app.sh + + +## Instructions to View the Application on the Web + +1. **Access the Web Service**: +Use Minikube to access the web service: + +minikube service web-service --url + +This will output a URL that you can use to view the application in your web browser by ctrl+clicking on the link. diff --git a/z2/backend/Dockerfile b/z2-failed/backend/Dockerfile similarity index 100% rename from z2/backend/Dockerfile rename to z2-failed/backend/Dockerfile diff --git a/z2-failed/backend/app.py b/z2-failed/backend/app.py new file mode 100644 index 0000000..a7d56ed --- /dev/null +++ b/z2-failed/backend/app.py @@ -0,0 +1,22 @@ +from flask import Flask, jsonify +from flask_cors import CORS +import redis + +app = Flask(__name__) +CORS(app) + +# Connect to Redis (running in a separate container and foldr) +redis_client = redis.StrictRedis(host='redis-service', port=6379, decode_responses=True) + +@app.route('/counter', methods=['GET']) +def counter(): + try: + visits = redis_client.incr('visits') + print(f"[INFO] Visit count: {visits}") + return jsonify({"visits": visits}) + except Exception as e: + print(f"[ERROR] Redis connection failed: {e}") + return jsonify({"error": "Could not connect to Redis"}), 500 + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0', port=80) diff --git a/z2/check.sh b/z2-failed/check.sh similarity index 100% rename from z2/check.sh rename to z2-failed/check.sh diff --git a/z2/compose.yaml b/z2-failed/compose.yaml similarity index 100% rename from z2/compose.yaml rename to z2-failed/compose.yaml diff --git a/z2/deployment.yaml b/z2-failed/deployment.yaml similarity index 94% rename from z2/deployment.yaml rename to z2-failed/deployment.yaml index 53efc0f..d1886b9 100644 --- a/z2/deployment.yaml +++ b/z2-failed/deployment.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: backend-deployment + namespace: visitor-app spec: replicas: 1 selector: diff --git a/z2/minikube-linux-amd64 b/z2-failed/minikube-linux-amd64 similarity index 100% rename from z2/minikube-linux-amd64 rename to z2-failed/minikube-linux-amd64 diff --git a/z2-failed/namespace.yaml b/z2-failed/namespace.yaml new file mode 100644 index 0000000..a122115 --- /dev/null +++ b/z2-failed/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: visitor-app diff --git a/z2-failed/prepare-app.sh b/z2-failed/prepare-app.sh new file mode 100755 index 0000000..71b6da2 --- /dev/null +++ b/z2-failed/prepare-app.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# build Docker images for backend and web +docker build -t backend:latest ./backend +docker build -t web:latest ./web + +# load images into minikube +minikube image load backend:latest +minikube image load web:latest diff --git a/z2/redis-service.yaml b/z2-failed/redis-service.yaml similarity index 91% rename from z2/redis-service.yaml rename to z2-failed/redis-service.yaml index 35eedea..8e0fc9c 100644 --- a/z2/redis-service.yaml +++ b/z2-failed/redis-service.yaml @@ -2,6 +2,7 @@ apiVersion: v1 kind: Service metadata: name: redis-service + namespace: visitor-app spec: selector: app: redis-app # This must match the pod label in StatefulSet. diff --git a/z2/service.yaml b/z2-failed/service.yaml similarity index 87% rename from z2/service.yaml rename to z2-failed/service.yaml index c02b7ed..2688037 100644 --- a/z2/service.yaml +++ b/z2-failed/service.yaml @@ -2,6 +2,7 @@ apiVersion: v1 kind: Service metadata: name: backend-service + namespace: visitor-app spec: selector: app: backend-app diff --git a/z2-failed/start-app.sh b/z2-failed/start-app.sh new file mode 100755 index 0000000..dc4f073 --- /dev/null +++ b/z2-failed/start-app.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# start minikube +minikube start + +#create namespace +kubectl apply -f namespace.yaml + +# apply all Kubernetes YAML files +kubectl apply -f deployment.yaml -n visitor-app +kubectl apply -f web-deployment.yaml -n visitor-app +kubectl apply -f statefulset.yaml -n visitor-app +kubectl apply -f service.yaml -n visitor-app +kubectl apply -f web-service.yaml -n visitor-app +kubectl apply -f redis-service.yaml -n visitor-app + +# Output web service URL +#minikube service web-service --url diff --git a/z2/statefulset.yaml b/z2-failed/statefulset.yaml similarity index 96% rename from z2/statefulset.yaml rename to z2-failed/statefulset.yaml index f2f3587..e47a78c 100644 --- a/z2/statefulset.yaml +++ b/z2-failed/statefulset.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-statefulset + namespace: visitor-app spec: serviceName: "redis-service" replicas: 1 diff --git a/z2-failed/stop-app.sh b/z2-failed/stop-app.sh new file mode 100755 index 0000000..75539ca --- /dev/null +++ b/z2-failed/stop-app.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# delete the whole namespace since it will deleat all resoruces inside it +kubectl delete namespace visitor-app + +# stop minikube +minikube stop diff --git a/z2/web-deployment.yaml b/z2-failed/web-deployment.yaml similarity index 94% rename from z2/web-deployment.yaml rename to z2-failed/web-deployment.yaml index 11bf383..8b150e8 100644 --- a/z2/web-deployment.yaml +++ b/z2-failed/web-deployment.yaml @@ -2,6 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: web-deployment + namespace: visitor-app spec: replicas: 1 selector: diff --git a/z2/web-service.yaml b/z2-failed/web-service.yaml similarity index 86% rename from z2/web-service.yaml rename to z2-failed/web-service.yaml index 39584bf..b32ed83 100644 --- a/z2/web-service.yaml +++ b/z2-failed/web-service.yaml @@ -2,6 +2,7 @@ apiVersion: v1 kind: Service metadata: name: web-service + namespace: visitor-app spec: selector: app: web-app diff --git a/z2-failed/web/Dockerfile b/z2-failed/web/Dockerfile new file mode 100644 index 0000000..3d80ccb --- /dev/null +++ b/z2-failed/web/Dockerfile @@ -0,0 +1,20 @@ +FROM httpd:2.4 + +# Enable mod_proxy and mod_proxy_http +RUN sed -i '/^#LoadModule proxy_module/s/^#//' /usr/local/apache2/conf/httpd.conf && \ + sed -i '/^#LoadModule proxy_http_module/s/^#//' /usr/local/apache2/conf/httpd.conf + +# Copy the HTML file +COPY index.html /usr/local/apache2/htdocs/ + +# Add a proxy configuration for /counter +#RUN echo "ProxyPass /counter http://backend-service:5000/counter" >> /usr/local/apache2/conf/httpd.conf && \ + # echo "ProxyPassReverse /counter http://backend-service:5000/counter" >> /usr/local/apache2/conf/httpd.conf + + +#Add a proxy configuration for /counter +RUN echo "ProxyPass /counter http://backend-service.visitor-app.svc.cluster.local:5000/counter" >> /usr/local/apache2/conf/httpd.conf && \ + echo "ProxyPassReverse /counter http://backend-service.visitor-app.svc.cluster.local:5000/counter" >> /usr/local/apache2/conf/httpd.conf + + +EXPOSE 80 diff --git a/z2/web/index.html b/z2-failed/web/index.html similarity index 92% rename from z2/web/index.html rename to z2-failed/web/index.html index d63b496..85ee849 100644 --- a/z2/web/index.html +++ b/z2-failed/web/index.html @@ -8,7 +8,7 @@

Welcome to the Visitor Counter App!

Made by ALI

-

Also made for assinment 1

+

Also copied from assinemnt 1 to make assinment 2

This page has been visited 0 times.