Skip to content

Latest commit

Β 

History

History
352 lines (249 loc) Β· 8.37 KB

File metadata and controls

352 lines (249 loc) Β· 8.37 KB

πŸš€ Wanna Fit Deployment Guide

This guide covers deploying the Wanna Fit application to various environments using Docker, Kubernetes, and CI/CD pipelines.

πŸ“‹ Prerequisites

Required Tools

  • Docker (v20.10+)
  • Kubernetes (v1.21+)
  • kubectl (v1.21+)
  • Git (v2.30+)
  • Python (v3.11+)

Cloud Services

  • Container Registry (GitHub Container Registry, Docker Hub, or AWS ECR)
  • Kubernetes Cluster (EKS, GKE, AKS, or local cluster)
  • Database (PostgreSQL 15+)
  • Cache (Redis 7+)
  • Message Queue (Apache Kafka)

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Load Balancer β”‚    β”‚   Ingress       β”‚    β”‚   Django App    β”‚
β”‚   (Nginx)       │───▢│   Controller    │───▢│   (3 replicas)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                        β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
                       β”‚   PostgreSQL    β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚   Database      β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Redis Cache   β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Apache Kafka  β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Go Service    β”‚
                       β”‚   (Data Proc.)  β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ Environment Configuration

1. Development Environment

# Clone repository
git clone <repository-url>
cd wanna-fit

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # Linux/Mac
# or
.venv\Scripts\Activate.ps1  # Windows

# Install dependencies
pip install -r requirements.txt

# Run migrations
python manage.py migrate

# Start development server
python run_dev.py

2. Staging Environment

# Deploy to staging
./scripts/deploy.sh staging

# Or using PowerShell on Windows
.\scripts\deploy.ps1 -Environment staging

3. Production Environment

# Deploy to production
./scripts/deploy.sh production

# Or using PowerShell on Windows
.\scripts\deploy.ps1 -Environment production

🐳 Docker Deployment

Build Images

# Build development image
docker build --target development -t wanna-fit:dev .

# Build staging image
docker build --target staging -t wanna-fit:staging .

# Build production image
docker build --target production -t wanna-fit:prod .

Run with Docker Compose

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

☸️ Kubernetes Deployment

1. Create Namespace

kubectl create namespace wanna-fit-prod

2. Apply Secrets

# Create secrets (replace with actual values)
kubectl create secret generic wanna-fit-prod-secret \
  --from-literal=SECRET_KEY="your-secret-key" \
  --from-literal=DB_PASSWORD="your-db-password" \
  --from-literal=EMAIL_HOST_USER="your-email" \
  --from-literal=EMAIL_HOST_PASSWORD="your-email-password" \
  -n wanna-fit-prod

3. Deploy Infrastructure

# Deploy PostgreSQL
kubectl apply -f k8s/postgres.yaml

# Deploy Redis
kubectl apply -f k8s/redis.yaml

# Deploy Kafka
kubectl apply -f k8s/kafka.yaml

4. Deploy Application

# Deploy Django app
kubectl apply -f k8s/production/django-app.yaml

# Deploy ingress
kubectl apply -f k8s/production/ingress.yaml

5. Deploy Monitoring

# Deploy Prometheus
kubectl apply -f k8s/monitoring/prometheus.yaml

# Deploy Grafana
kubectl apply -f k8s/monitoring/grafana.yaml

πŸ”„ CI/CD Pipeline

GitHub Actions Workflow

The CI/CD pipeline includes:

  1. Testing Phase

    • Unit tests with coverage
    • Security scanning
    • Code quality checks (linting, formatting)
  2. Build Phase

    • Docker image building
    • Multi-architecture support
    • Image scanning for vulnerabilities
  3. Deploy Phase

    • Staging deployment (on develop branch)
    • Production deployment (on main branch)
    • Health checks and smoke tests

Manual Deployment

# Deploy specific version
./scripts/deploy.sh production v1.2.3

# Rollback deployment
kubectl rollout undo deployment/wanna-fit-web-prod -n wanna-fit-prod

πŸ“Š Monitoring and Observability

Health Checks

  • Liveness: GET /health/live/
  • Readiness: GET /health/ready/
  • Health: GET /health/

Metrics

  • Application Metrics: Available at /metrics/
  • Prometheus: Scrapes metrics every 30 seconds
  • Grafana: Dashboard at http://grafana-service:3000

Logging

  • Application Logs: Available in pod logs
  • Centralized Logging: Configure with ELK stack or similar

πŸ” Security Considerations

Production Security

  1. Secrets Management

    • Use Kubernetes secrets
    • Rotate secrets regularly
    • Never commit secrets to repository
  2. Network Security

    • Use TLS/SSL certificates
    • Configure network policies
    • Enable firewall rules
  3. Application Security

    • Enable Django security middleware
    • Use HTTPS in production
    • Configure CORS properly

Environment Variables

# Required environment variables
SECRET_KEY=your-secret-key
DATABASE_URL=postgresql://user:pass@host:port/db
REDIS_URL=redis://host:port/db
ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
DEBUG=False

🚨 Troubleshooting

Common Issues

  1. Pod Not Starting

    kubectl describe pod <pod-name> -n wanna-fit-prod
    kubectl logs <pod-name> -n wanna-fit-prod
  2. Database Connection Issues

    kubectl exec -it <pod-name> -n wanna-fit-prod -- python manage.py dbshell
  3. Cache Issues

    kubectl exec -it redis-pod -n wanna-fit-prod -- redis-cli ping

Performance Optimization

  1. Horizontal Scaling

    kubectl scale deployment wanna-fit-web-prod --replicas=5 -n wanna-fit-prod
  2. Resource Limits

    • Adjust CPU/memory limits in deployment manifests
    • Monitor resource usage with Prometheus
  3. Database Optimization

    • Enable connection pooling
    • Use read replicas for read-heavy workloads

πŸ“ˆ Scaling

Horizontal Pod Autoscaler

The HPA automatically scales pods based on:

  • CPU utilization (target: 70%)
  • Memory utilization (target: 80%)
  • Custom metrics (if configured)

Vertical Scaling

# Update resource limits
kubectl patch deployment wanna-fit-web-prod -n wanna-fit-prod -p '{"spec":{"template":{"spec":{"containers":[{"name":"django","resources":{"limits":{"cpu":"2000m","memory":"2Gi"}}}]}}}}'

πŸ”„ Backup and Recovery

Database Backup

# Create backup
kubectl exec -it postgres-pod -n wanna-fit-prod -- pg_dump -U postgres wanna_fit > backup.sql

# Restore backup
kubectl exec -i postgres-pod -n wanna-fit-prod -- psql -U postgres wanna_fit < backup.sql

Application Backup

# Backup media files
kubectl exec -it <pod-name> -n wanna-fit-prod -- tar -czf /tmp/media-backup.tar.gz /app/media

# Copy backup
kubectl cp <pod-name>:/tmp/media-backup.tar.gz ./media-backup.tar.gz -n wanna-fit-prod

πŸ“ž Support

For deployment issues:

  1. Check the logs: kubectl logs <pod-name> -n wanna-fit-prod
  2. Verify configuration: kubectl describe <resource> -n wanna-fit-prod
  3. Test connectivity: kubectl exec -it <pod-name> -n wanna-fit-prod -- curl localhost:8000/health/

🎯 Next Steps

  1. Set up monitoring alerts
  2. Configure log aggregation
  3. Implement blue-green deployments
  4. Add chaos engineering tests
  5. Set up disaster recovery procedures