https://github.com/sajjadhz/ansiblebluegreendeploymentfastapi
Deploying FastAPI app with Blue/Green deployment strategy using Ansible
https://github.com/sajjadhz/ansiblebluegreendeploymentfastapi
ansible ansible-playbook ansible-role blue-green-deployment fastapi load-balancer zero-downtime zero-downtime-deploy
Last synced: about 2 months ago
JSON representation
Deploying FastAPI app with Blue/Green deployment strategy using Ansible
- Host: GitHub
- URL: https://github.com/sajjadhz/ansiblebluegreendeploymentfastapi
- Owner: Sajjadhz
- License: mit
- Created: 2025-08-18T09:12:33.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-08-18T09:19:08.000Z (10 months ago)
- Last Synced: 2025-08-18T11:24:21.792Z (10 months ago)
- Topics: ansible, ansible-playbook, ansible-role, blue-green-deployment, fastapi, load-balancer, zero-downtime, zero-downtime-deploy
- Language: Jinja
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Blue/Green Deployment for FastAPI with Nginx and Ansible
This project implements zero-downtime deployments for FastAPI applications using Ansible and Nginx with a blue/green deployment strategy.
## Prerequisites
- Vagrant 2.2+
- Ansible 2.9+
- VirtualBox 6.0+
- Python 3.6+
## Infrastructure Overview
- **Load Balancer**: `lb.example.com` (Nginx)
- **Blue Environment**: `blue.example.com`
- **Green Environment**: `green.example.com`
## File Structure
```
.
├── playbooks/
│ ├── deploy.yml # Deploys application
│ ├── setup_loadbalancer.yml # Configures Nginx
│ ├── switch.yml # Switches traffic
│ └── cleanup.yml # Cleans old deployments
├── templates/
│ ├── fastapi.service.j2 # Systemd service template
│ └── loadbalancer.conf.j2 # Nginx config template
├── inventory/
│ └── production # Inventory file
└── app/
├── blue/ # Blue deployment files
└── green/ # Green deployment files
```
## Deployment Workflow
### 1. Initial Setup
```bash
# Create all VMs
vagrant up
# Configure load balancer
ansible-playbook -i inventory/production playbooks/setup_loadbalancer.yml
```
### 2. First Deployment (Blue)
```bash
ansible-playbook -i inventory/production playbooks/deploy.yml \
-e "target_group=blue deployment_color=blue"
```
### 3. Deploy New Version (Green)
```bash
ansible-playbook -i inventory/production playbooks/deploy.yml \
-e "target_group=green deployment_color=green"
```
### 4. Test Green Environment
```bash
# Directly access green environment
curl http://green.example.com:8000
# Or through load balancer with test header
curl -H "X-Deployment: green" http://lb.example.com
```
### 5. Switch Traffic to Green
```bash
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=green"
```
### 6. (Optional) Clean Up Blue
```bash
ansible-playbook -i inventory/production playbooks/cleanup.yml \
-e "old_deployment_group=blue old_deployment_color=blue"
```
## Rollback Procedure
```bash
# Switch back to blue
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=blue"
# Clean up green if needed
ansible-playbook -i inventory/production playbooks/cleanup.yml \
-e "old_deployment_group=green old_deployment_color=green"
```
## Maintenance Commands
**Check current traffic distribution:**
```bash
vagrant ssh lb
sudo tail -f /var/log/nginx/access.log | awk '{print $6}' | sort | uniq -c
```
**Validate Nginx config:**
```bash
vagrant ssh lb
sudo nginx -t
```
**View service status:**
```bash
# On blue/green nodes
systemctl status fastapi-blue
systemctl status fastapi-green
```
## Customization
### Environment Variables
Set in `inventory/production`:
```ini
[all:vars]
app_port=8000
domain=example.com
```
### Weighted Traffic Distribution
For canary releases:
```bash
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=green blue_weight=90 green_weight=10"
```
```bash
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=green blue_weight=50 green_weight=50"
```
```bash
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=green blue_weight=10 green_weight=90"
```
```bash
ansible-playbook -i inventory/production playbooks/switch.yml \
-e "deployment_color=green blue_weight=0 green_weight=100"
```
## Best Practices
1. Always test new deployments before switching traffic
2. Monitor both environments during cutover
3. Keep previous deployment until new one is verified stable
4. Use low DNS TTL if using DNS-based switching
5. Implement health checks in your application
## Troubleshooting
**Nginx fails to reload:**
- Check syntax: `sudo nginx -t`
- Verify ports are open
- Ensure DNS resolution works
**Service fails to start:**
- Check logs: `journalctl -u fastapi-blue`
- Verify Python dependencies
- Check port availability
```
This README provides:
1. Clear step-by-step deployment instructions
2. Rollback procedures
3. Maintenance commands
4. Customization options
5. Troubleshooting tips
6. Best practices
The workflow ensures zero-downtime deployments with proper testing and rollback capabilities.