|
1 |
| -WebApp using python Flask |
| 1 | +# Complete Guide: Flask WebApp with GitHub Actions, Docker, Azure Deployment & Monitoring |
| 2 | + |
| 3 | +This repository contains a Flask-based web application with CI/CD pipeline configured through GitHub Actions, Docker, and deployment to Azure VM with prometheus, grafana monitoring |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Clone the Repository |
| 8 | +```bash |
| 9 | +git clone https://github.com/gurram2001/webapp.git |
| 10 | +cd webapp |
| 11 | +``` |
| 12 | + |
| 13 | +## CI/CD Configuration |
| 14 | + |
| 15 | +### Secrets to Add in GitHub Repository |
| 16 | +Go to your GitHub repo → Settings → Secrets and Variables → Actions and add: |
| 17 | + |
| 18 | +| Name | Description | |
| 19 | +|------|-------------| |
| 20 | +| DOCKER_USERNAME | Your Docker Hub username | |
| 21 | +| DOCKER_PASSWORD | Your Docker Hub password or token | |
| 22 | +| VM_HOST | Public IP of your Azure VM | |
| 23 | +| VM_USER | SSH username (usually azureuser) | |
| 24 | +| VM_KEY | Your private SSH key | |
| 25 | + |
| 26 | +## Set Up Azure VM |
| 27 | + |
| 28 | +### Create an Azure VM (Ubuntu) |
| 29 | +1. Open port 22 (SSH) and 80 (HTTP) in Network Security Group (NSG) |
| 30 | +2. SSH into VM and install Docker: |
| 31 | +```bash |
| 32 | +sudo apt install docker.io |
| 33 | +sudo usermod -aG docker azureuser |
| 34 | +``` |
| 35 | +3. Logout VM and Login again |
| 36 | +4. CHeck with below command |
| 37 | +```bash |
| 38 | +docker run hello-world |
| 39 | +``` |
| 40 | + |
| 41 | +## Test the Full Flow |
| 42 | +1. Commit and push code → GitHub Action triggers |
| 43 | +2. Image builds → pushed to Docker Hub |
| 44 | +3. Azure VM pulls latest image → runs container |
| 45 | +4. Open browser: http://<Azure-VM-IP> to see your app |
| 46 | + |
| 47 | +## Monitoring Setup |
| 48 | + |
| 49 | +### Step 1: SSH into your Azure VM |
| 50 | +```bash |
| 51 | +ssh azureuser@<your-azure-vm-ip> |
| 52 | +``` |
| 53 | + |
| 54 | +### Step 2: Create a monitoring folder |
| 55 | +```bash |
| 56 | +mkdir ~/monitoring && cd ~/monitoring |
| 57 | +``` |
| 58 | + |
| 59 | +### Step 3: Create Prometheus config file |
| 60 | +Create a file named `prometheus.yml`: |
| 61 | +```yaml |
| 62 | +global: |
| 63 | + scrape_interval: 15s |
| 64 | +scrape_configs: |
| 65 | + - job_name: 'docker' |
| 66 | + static_configs: |
| 67 | + - targets: ['localhost:9323'] |
| 68 | +``` |
| 69 | +
|
| 70 | +### Step 4: Run Prometheus and Grafana in Docker |
| 71 | +```bash |
| 72 | +# Run Prometheus |
| 73 | +docker run -d \ |
| 74 | + -p 9090:9090 \ |
| 75 | + -v ~/monitoring/prometheus.yml:/etc/prometheus/prometheus.yml \ |
| 76 | + --name prometheus \ |
| 77 | + prom/prometheus |
| 78 | + |
| 79 | +# Run Grafana |
| 80 | +docker run -d \ |
| 81 | + -p 3000:3000 \ |
| 82 | + --name=grafana \ |
| 83 | + grafana/grafana |
| 84 | +``` |
| 85 | + |
| 86 | +### Step 5: Install Docker Metrics Exporter |
| 87 | +Prometheus needs metrics from Docker – install this exporter: |
| 88 | +```bash |
| 89 | +docker run -d \ |
| 90 | + -p 9323:9323 \ |
| 91 | + -v /var/run/docker.sock:/var/run/docker.sock \ |
| 92 | + --name cadvisor \ |
| 93 | + google/cadvisor:latest |
| 94 | +``` |
| 95 | + |
| 96 | +### Step 6: Access Grafana on Browser |
| 97 | +Open: http://<your-azure-vm-ip>:3000 |
| 98 | + |
| 99 | +Default login: |
| 100 | +- User: admin |
| 101 | +- Password: admin (you'll be prompted to change) |
| 102 | + |
| 103 | +### Step 7: Connect Prometheus as Grafana Data Source |
| 104 | +In Grafana: |
| 105 | +1. Go to Settings → Data Sources → Add Prometheus |
| 106 | +2. URL: http://localhost:9090 |
| 107 | +3. Click Save & Test |
| 108 | + |
| 109 | +### Step 8: Import a Dashboard |
| 110 | +1. Go to + → Import |
| 111 | +2. Paste Dashboard ID: 193 (Docker metrics) |
| 112 | +3. Click Load → Select Prometheus as data source → Import |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +### Understanding Prometheus Configuration |
| 117 | +- **prometheus.yml**: This configuration file tells Prometheus what to monitor (targets) and how often (scrape interval) |
| 118 | + - The `scrape_interval` defines how frequently Prometheus collects metrics (15s = every 15 seconds) |
| 119 | + - The `scrape_configs` section defines what services to monitor and where to find them |
| 120 | + - The `targets` field specifies the host:port where metrics can be collected |
| 121 | + |
| 122 | +### About Exporters |
| 123 | +- Exporters are components that collect and expose metrics from various services in a format Prometheus can understand |
| 124 | +- **cAdvisor** (Container Advisor) is an exporter that collects container metrics from Docker |
| 125 | +- The exporter exposes Docker metrics on port 9323, which Prometheus then scrapes based on the configuration |
| 126 | + |
0 commit comments