Skip to content

Satishchoudhary94/Blue_Green_deployment_Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

Perfect πŸ‘ Below is a FINAL, CLEAN README.md in pure GitHub Markdown format.

πŸ‘‰ You can directly copy–paste this exactly as it is into your GitHub repo πŸ‘‰ No extra explanation, no chat text, no formatting issues πŸ‘‰ This is production-ready + interview-ready


# πŸš€ Blue-Green Deployment Using Nginx (Zero Downtime)

This repository demonstrates a **zero-downtime Blue-Green deployment strategy** using **Nginx**, **PM2**, and **Node.js**, where traffic is seamlessly switched between two application versions without stopping the service.

---

## πŸ“Œ Objective

- Run **two versions of the same API** (Blue & Green) in parallel
- Switch live traffic **without downtime**
- No application restart during deployment
- Traffic switches using **Nginx reload**
- Instant rollback capability

---

## 🧠 Architecture Overview

Client | v Nginx (Reverse Proxy - Port 8080) | |----> Green App (Port 5000) | |----> Blue App (Port 5001)


---

## πŸ› οΈ Tech Stack

| Component | Purpose |
|--------|--------|
| Node.js | Backend API |
| PM2 | Process manager |
| Nginx | Reverse proxy & traffic switch |
| systemd | Reload hook automation |

---

## πŸ“ Project Structure

blue-green-poc/ β”œβ”€β”€ apps/ β”‚ β”œβ”€β”€ app-green.js β”‚ └── app-blue.js β”œβ”€β”€ ecosystem.config.js └── README.md


---

## βš™οΈ Prerequisites

- Ubuntu/Linux VM
- sudo access
- Node.js (via NVM recommended)
- PM2
- Nginx

---

## πŸ”§ Setup Instructions

### 1️⃣ Install Node.js (NVM)

```bash
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts

Verify:

node -v
npm -v

2️⃣ Install PM2

npm install -g pm2
pm2 -v

3️⃣ Install Nginx

sudo apt update
sudo apt install -y nginx

🧩 Application Setup

GREEN App (Port 5000)

const express = require("express");
const app = express();

app.get("/hello", (req, res) => {
  res.json({ message: "Hello from GREEN (5000)" });
});

app.listen(5000);

BLUE App (Port 5001)

const express = require("express");
const app = express();

app.get("/hello", (req, res) => {
  res.json({ message: "Hello from BLUE (5001)" });
});

app.listen(5001);

Install Dependencies

npm init -y
npm install express

βš™οΈ PM2 Configuration

module.exports = {
  apps: [
    { name: "hello-green", script: "./apps/app-green.js" },
    { name: "hello-blue", script: "./apps/app-blue.js" }
  ]
};

Start applications:

pm2 start ecosystem.config.js
pm2 list

Test:

curl http://localhost:5000/hello
curl http://localhost:5001/hello

🌐 Nginx Configuration

Disable Default Site

sudo rm /etc/nginx/sites-enabled/default

Create State Directory

sudo mkdir -p /etc/nginx/state

Active Backend File

sudo nano /etc/nginx/state/backend_upstream.conf
server 127.0.0.1:5000;

Upstream Config

sudo nano /etc/nginx/conf.d/backend.conf
upstream backend_app {
    include /etc/nginx/state/backend_upstream.conf;
}

Server Config

sudo nano /etc/nginx/sites-available/blue-green.conf
server {
    listen 8080;
    server_name _;

    location / {
        proxy_pass http://backend_app;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Enable site:

sudo ln -s /etc/nginx/sites-available/blue-green.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Test:

curl http://localhost:8080/hello

πŸ” Zero-Downtime Deployment Demo

Continuous Traffic Simulation

while true; do curl -s http://localhost:8080/hello; sleep 0.5; done

Manual Traffic Switch

sudo nano /etc/nginx/state/backend_upstream.conf

Change to:

server 127.0.0.1:5001;

Reload Nginx:

sudo nginx -s reload

βœ” Traffic switches with zero downtime


πŸ€– Automatic Switch on Nginx Reload (Production Style)

Switch Script

sudo nano /usr/local/bin/nginx-blue-green-switch.sh
#!/bin/bash

STATE="/etc/nginx/state/active_port"
UPSTREAM="/etc/nginx/state/backend_upstream.conf"

CURRENT=$(cat $STATE)

if [ "$CURRENT" = "5000" ]; then
  echo "5001" > $STATE
  echo "server 127.0.0.1:5001;" > $UPSTREAM
else
  echo "5000" > $STATE
  echo "server 127.0.0.1:5000;" > $UPSTREAM
fi
sudo chmod +x /usr/local/bin/nginx-blue-green-switch.sh

Hook Script into Nginx Reload

sudo systemctl edit nginx
[Service]
ExecReload=
ExecReload=/usr/local/bin/nginx-blue-green-switch.sh
ExecReload=/usr/sbin/nginx -s reload

Apply changes:

sudo systemctl daemon-reexec

πŸ”₯ Final Demo

sudo systemctl reload nginx

βœ” Automatic traffic switch βœ” Zero downtime βœ” No failed requests


πŸ”„ Rollback

Rollback is instant:

sudo systemctl reload nginx

🎯 One-Line Summary

Two application versions run in parallel and Nginx reload switches traffic instantly with zero downtime.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors