Skip to content

Enhance Flask-MySQL App with Docker, Kubernetes, Monitoring & Slack Integration #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
114 changes: 84 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -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://<IP>:5000 => Welcome
http://<IP>: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/).

23 changes: 19 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions k8s/application/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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"

18 changes: 18 additions & 0 deletions k8s/application/hpa.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions k8s/application/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app: flask-app
ports:
- protocol: TCP
port: 8000
targetPort: 8000
19 changes: 19 additions & 0 deletions k8s/monitoring/grafana/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions k8s/monitoring/grafana/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: grafana-service
spec:
selector:
app: grafana
ports:
- protocol: TCP
port: 3000
targetPort: 3000
28 changes: 28 additions & 0 deletions k8s/monitoring/prometheus/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions k8s/monitoring/prometheus/prometheus-config.yaml
Original file line number Diff line number Diff line change
@@ -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']
11 changes: 11 additions & 0 deletions k8s/monitoring/prometheus/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
spec:
selector:
app: prometheus
ports:
- port: 9090
targetPort: 9090
type: ClusterIP
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Flask
prometheus-flask-exporter
prometheus_client