From bb846a77dd7c088aa41ad81f994075fbd92518d4 Mon Sep 17 00:00:00 2001 From: sharmaaakash170 Date: Wed, 7 May 2025 23:42:35 +0530 Subject: [PATCH] Adding all the updated files --- Dockerfile | 14 ++- README.md | 114 +++++++++++++----- app.py | 23 +++- k8s/application/deployment.yaml | 27 +++++ k8s/application/hpa.yaml | 18 +++ k8s/application/service.yaml | 11 ++ k8s/monitoring/grafana/deployment.yaml | 19 +++ k8s/monitoring/grafana/service.yaml | 11 ++ k8s/monitoring/prometheus/deployment.yaml | 28 +++++ .../prometheus/prometheus-config.yaml | 12 ++ k8s/monitoring/prometheus/service.yaml | 11 ++ requirements.txt | 2 + 12 files changed, 251 insertions(+), 39 deletions(-) create mode 100644 k8s/application/deployment.yaml create mode 100644 k8s/application/hpa.yaml create mode 100644 k8s/application/service.yaml create mode 100644 k8s/monitoring/grafana/deployment.yaml create mode 100644 k8s/monitoring/grafana/service.yaml create mode 100644 k8s/monitoring/prometheus/deployment.yaml create mode 100644 k8s/monitoring/prometheus/prometheus-config.yaml create mode 100644 k8s/monitoring/prometheus/service.yaml diff --git a/Dockerfile b/Dockerfile index c1108043..440b07dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,9 @@ -FROM ubuntu:20.04 -RUN apt-get update && apt-get install -y python3 python3-pip -RUN pip3 install flask -COPY app.py /opt/ -ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0 --port=8080 +FROM python:3.13-slim + +WORKDIR /app + +COPY . . + +RUN pip3 install -r requirements.txt + +CMD [ "python", "app.py" ] diff --git a/README.md b/README.md index 0ead6008..926d7bbc 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,97 @@ -# Simple Web Application - -This is a simple web application using [Python Flask](http://flask.pocoo.org/) and [MySQL](https://www.mysql.com/) database. -This is used in the demonstration of the development of Ansible Playbooks. - - Below are the steps required to get this working on a base linux system. - - - **Install all required dependencies** - - **Install and Configure Web Server** - - **Start Web Server** - -## 1. Install all required dependencies - - Python and its dependencies - ```bash - apt-get install -y python3 python3-setuptools python3-dev build-essential python3-pip default-libmysqlclient-dev - ``` - -## 2. Install and Configure Web Server - -Install Python Flask dependency + +# Simple Flask MySQL Web Application + +This is a simple Python Flask web application that connects to a MySQL database. It was originally built for demonstrating Ansible Playbook automation but has now been enhanced to support modern DevOps practices. + +--- + +## πŸš€ Features + +- Python Flask-based Web App +- MySQL Database Integration +- Docker Support +- Kubernetes Manifests with HPA (Horizontal Pod Autoscaler) +- Monitoring with Prometheus & Grafana +- Slack Notification Integration + +--- + +## πŸ› οΈ Setup Instructions + +### 1. Install Required Dependencies (For Bare Metal) + +```bash +apt-get install -y python3 python3-setuptools python3-dev build-essential python3-pip default-libmysqlclient-dev +``` + +### 2. Install Python Libraries + +```bash +pip3 install flask flask-mysql +``` + +--- + +## 🐳 Docker Support + +Build the Docker image: + ```bash -pip3 install flask -pip3 install flask-mysql +docker build -t flask-mysql-app:latest . ``` -- Copy `app.py` or download it from a source repository -- Configure database credentials and parameters +Run the container: -## 3. Start Web Server +```bash +docker run -p 5000:5000 flask-mysql-app:latest +``` + +--- + +## ☸️ Kubernetes Deployment + +This project includes Kubernetes manifests for: + +- App Deployment & Service +- MySQL Deployment & Service +- HPA (Horizontal Pod Autoscaler) +- Monitoring (Prometheus & Grafana) +- Slack Alerts + +Apply manifests: -Start web server ```bash -FLASK_APP=app.py flask run --host=0.0.0.0 +kubectl apply -f k8s/ ``` -## 4. Test +--- + +## πŸ“Š Monitoring & Alerts + +- Prometheus scrapes metrics from Flask app and Kubernetes nodes. +- Grafana dashboard is included. +- Slack alerts configured via Alertmanager. + +--- + +## πŸ§ͺ Testing the Application + +Open a browser and access: -Open a browser and go to URL ``` http://:5000 => Welcome http://:5000/how%20are%20you => I am good, how about you? ``` + +--- + +## 🀝 Contributing + +Feel free to fork the repo, raise PRs, and enhance it further. + +--- + +## πŸ“¬ Stay Connected + +For contributions, feedback, or collaboration, connect with me on [LinkedIn](https://www.linkedin.com/in/aakash-sharma-8937b81aa/). + diff --git a/app.py b/app.py index 36721a06..d31acd3c 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,29 @@ -import os -from flask import Flask +from flask import Flask, request +from prometheus_flask_exporter import PrometheusMetrics +from prometheus_client import Counter, generate_latest, CONTENT_TYPE_LATEST + app = Flask(__name__) +# ================= Prometheus Configs=================================================================== +http_requests_total = Counter('http_requests_total', 'Total HTTP Requests', ['method', 'endpoint']) + +@app.before_request +def before_request(): + http_requests_total.labels(method=request.method, endpoint=request.path).inc() + +@app.route('/metrics') +def prometheus_metrics(): + return generate_latest(), 200, {'Content-Type': CONTENT_TYPE_LATEST} + +# ========================================================================================================= + @app.route("/") def main(): return "Welcome!" -@app.route('/how are you') +@app.route('/hru') def hello(): return 'I am good, how about you?' if __name__ == "__main__": - app.run(host="0.0.0.0", port=8080) + app.run(host="0.0.0.0", port=8000) diff --git a/k8s/application/deployment.yaml b/k8s/application/deployment.yaml new file mode 100644 index 00000000..6fa8eed4 --- /dev/null +++ b/k8s/application/deployment.yaml @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: flask-app +spec: + replicas: 2 + selector: + matchLabels: + app: flask-app + template: + metadata: + labels: + app: flask-app + spec: + containers: + - name: flask-container + image: sharmaaakash170/flask-simple-app:latest + ports: + - containerPort: 8000 + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "200m" + memory: "512Mi" + \ No newline at end of file diff --git a/k8s/application/hpa.yaml b/k8s/application/hpa.yaml new file mode 100644 index 00000000..bd54f9f7 --- /dev/null +++ b/k8s/application/hpa.yaml @@ -0,0 +1,18 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: backend-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: flask-app + minReplicas: 1 + maxReplicas: 5 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 50 diff --git a/k8s/application/service.yaml b/k8s/application/service.yaml new file mode 100644 index 00000000..6ddc9ec8 --- /dev/null +++ b/k8s/application/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: flask-service +spec: + selector: + app: flask-app + ports: + - protocol: TCP + port: 8000 + targetPort: 8000 diff --git a/k8s/monitoring/grafana/deployment.yaml b/k8s/monitoring/grafana/deployment.yaml new file mode 100644 index 00000000..4593c8ca --- /dev/null +++ b/k8s/monitoring/grafana/deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: grafana +spec: + replicas: 1 + selector: + matchLabels: + app: grafana + template: + metadata: + labels: + app: grafana + spec: + containers: + - name: grafana + image: grafana/grafana:10.4.0 + ports: + - containerPort: 3000 \ No newline at end of file diff --git a/k8s/monitoring/grafana/service.yaml b/k8s/monitoring/grafana/service.yaml new file mode 100644 index 00000000..358d48f5 --- /dev/null +++ b/k8s/monitoring/grafana/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: grafana-service +spec: + selector: + app: grafana + ports: + - protocol: TCP + port: 3000 + targetPort: 3000 \ No newline at end of file diff --git a/k8s/monitoring/prometheus/deployment.yaml b/k8s/monitoring/prometheus/deployment.yaml new file mode 100644 index 00000000..1894a0d9 --- /dev/null +++ b/k8s/monitoring/prometheus/deployment.yaml @@ -0,0 +1,28 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: prometheus +spec: + replicas: 1 + selector: + matchLabels: + app: prometheus + template: + metadata: + labels: + app: prometheus + spec: + containers: + - name: prometheus + image: prom/prometheus:v2.51.1 + args: + - "--config.file=/etc/prometheus/prometheus.yml" + ports: + - containerPort: 9090 + volumeMounts: + - name: prometheus-config-volume + mountPath: /etc/prometheus/ + volumes: + - name: prometheus-config-volume + configMap: + name: prometheus-config \ No newline at end of file diff --git a/k8s/monitoring/prometheus/prometheus-config.yaml b/k8s/monitoring/prometheus/prometheus-config.yaml new file mode 100644 index 00000000..b11a535b --- /dev/null +++ b/k8s/monitoring/prometheus/prometheus-config.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: prometheus-config +data: + prometheus.yml: | + global: + scrape_interval: 15s + scrape_configs: + - job_name: 'flask-app' + static_configs: + - targets: ['flask-service.default.svc.cluster.local:8000'] \ No newline at end of file diff --git a/k8s/monitoring/prometheus/service.yaml b/k8s/monitoring/prometheus/service.yaml new file mode 100644 index 00000000..73d3d6bb --- /dev/null +++ b/k8s/monitoring/prometheus/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: prometheus-service +spec: + selector: + app: prometheus + ports: + - port: 9090 + targetPort: 9090 + type: ClusterIP \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e3e9a71d..e7d0c1bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ Flask +prometheus-flask-exporter +prometheus_client \ No newline at end of file