Budget-Management-Backend-API

Kubernetes Deployment Overview

Architecture Diagram

flowchart TD
    LB[Load Balancer<br/>Ingress Controller]
    FE[Frontend Service<br/>LoadBalancer]
    BE[Backend Service<br/>ClusterIP]

    LB --> FE
    LB --> BE

    FE --> FBlue[Frontend Blue Pods]
    FE --> FGreen[Frontend Green Pods]
    BE --> BBlue[Backend Blue Pods]
    BE --> BGreen[Backend Green Pods]
    BE --> BCanary[Backend Canary Pods]

    FBlue --> Routing
    FGreen --> Routing
    BBlue --> Routing
    BGreen --> Routing
    BCanary --> Routing

    Routing[Blue-Green / Canary Routing]
    Routing --> CS[ConfigMaps & Secrets]
    CS --> MongoDB[MongoDB Service]
    CS --> PostgreSQL[PostgreSQL Service]
    CS --> Redis[Redis Service]

Component Overview

1. Deployments

Blue-Green Deployments

Canary Deployments

2. Services

Blue-Green Services

backend-service-blue-green.yaml:
  - backend-service: Main service (switches between blue/green)
  - backend-service-blue: Direct access to blue version
  - backend-service-green: Direct access to green version

Canary Services

backend-service-canary.yaml:
  - backend-service-canary: Routes to both stable and canary

3. Horizontal Pod Autoscaler (HPA)

4. Pod Disruption Budget (PDB)

5. Network Policies

6. Ingress

7. ServiceMonitor (Prometheus)

Resource Requirements

Backend Pods

Resource Request Limit
CPU 250m 500m
Memory 256Mi 512Mi

Frontend Pods

Resource Request Limit
CPU 100m 200m
Memory 128Mi 256Mi

Total Cluster Requirements (Blue-Green)

Assuming both blue and green are running during deployment:

Component Pods CPU Request Memory Request
Backend Blue 3 750m 768Mi
Backend Green 3 750m 768Mi
Frontend Blue 3 300m 384Mi
Frontend Green 3 300m 384Mi
Total 12 2100m (2.1 cores) 2304Mi (2.25GB)

Total Cluster Requirements (Canary)

Component Pods CPU Request Memory Request
Backend Stable 9 2250m 2304Mi
Backend Canary 1 250m 256Mi
Frontend Stable 9 900m 1152Mi
Frontend Canary 1 100m 128Mi
Total 20 3500m (3.5 cores) 3840Mi (3.75GB)

Deployment Files

flowchart TD
    K[kubernetes]

    subgraph BlueGreen["Blue-Green Deployments"]
        K --> BDB[backend-deployment-blue.yaml<br/>Backend blue version]
        K --> BDG[backend-deployment-green.yaml<br/>Backend green version]
        K --> FDB[frontend-deployment-blue.yaml<br/>Frontend blue version]
        K --> FDG[frontend-deployment-green.yaml<br/>Frontend green version]
    end

    subgraph Canary["Canary Deployments"]
        K --> BDC[backend-deployment-canary.yaml<br/>Backend canary version]
        K --> BDS[backend-deployment-canary-stable.yaml<br/>Backend stable version]
        K --> FDC[frontend-deployment-canary.yaml<br/>Frontend canary version]
        K --> FDS[frontend-deployment-canary-stable.yaml<br/>Frontend stable version]
    end

    subgraph Services["Services"]
        K --> BSBG[backend-service-blue-green.yaml<br/>Blue-green backend services]
        K --> BSC[backend-service-canary.yaml<br/>Canary backend service]
        K --> BSOrig[backend-service.yaml<br/>Original backend service - deprecated]
        K --> FSBG[frontend-service-blue-green.yaml<br/>Blue-green frontend services]
        K --> FSC[frontend-service-canary.yaml<br/>Canary frontend service]
        K --> FSOrig[frontend-service.yaml<br/>Original frontend service - deprecated]
    end

    subgraph Platform["Shared Platform Resources"]
        K --> CFG[configmap.yaml<br/>Application configuration]
        K --> HPA[hpa.yaml<br/>Horizontal Pod Autoscalers]
        K --> PDB[pdb.yaml<br/>Pod Disruption Budgets]
        K --> NP[network-policy.yaml<br/>Network security policies]
        K --> ING[ingress.yaml<br/>Ingress configuration]
        K --> SM[servicemonitor.yaml<br/>Prometheus monitoring]
    end

Quick Start

1. Blue-Green Deployment

# Deploy blue version
kubectl apply -f kubernetes/backend-deployment-blue.yaml
kubectl apply -f kubernetes/frontend-deployment-blue.yaml
kubectl apply -f kubernetes/backend-service-blue-green.yaml
kubectl apply -f kubernetes/frontend-service-blue-green.yaml

# Apply supporting resources
kubectl apply -f kubernetes/configmap.yaml
kubectl apply -f kubernetes/hpa.yaml
kubectl apply -f kubernetes/pdb.yaml
kubectl apply -f kubernetes/network-policy.yaml

# Verify deployment
kubectl get deployments -n production
kubectl get pods -n production
kubectl get services -n production

2. Canary Deployment

# Deploy stable and canary versions
kubectl apply -f kubernetes/backend-deployment-canary-stable.yaml
kubectl apply -f kubernetes/backend-deployment-canary.yaml
kubectl apply -f kubernetes/frontend-deployment-canary-stable.yaml
kubectl apply -f kubernetes/frontend-deployment-canary.yaml
kubectl apply -f kubernetes/backend-service-canary.yaml
kubectl apply -f kubernetes/frontend-service-canary.yaml

# Apply supporting resources
kubectl apply -f kubernetes/configmap.yaml
kubectl apply -f kubernetes/hpa.yaml
kubectl apply -f kubernetes/pdb.yaml
kubectl apply -f kubernetes/network-policy.yaml

# Verify deployment
kubectl get deployments -n production
kubectl get pods -n production
kubectl get services -n production

Health Check Endpoints

Backend

Frontend

Monitoring

Metrics

Applications expose Prometheus metrics at:

Key Metrics to Monitor

  1. Application Metrics
    • Request rate
    • Error rate
    • Response time (p50, p95, p99)
    • Active connections
  2. Pod Metrics
    • CPU usage
    • Memory usage
    • Network I/O
    • Disk I/O
  3. Deployment Metrics
    • Available replicas
    • Ready replicas
    • Deployment status
    • Rollout progress

Security Features

Pod Security

Network Security

Secrets Management

High Availability

Features

  1. Multiple Replicas: Minimum 3 replicas per deployment
  2. Pod Anti-Affinity: Spread pods across nodes
  3. Pod Disruption Budget: Maintain minimum availability
  4. Health Checks: Automatic pod restart and removal
  5. Auto-scaling: HPA based on CPU/Memory
  6. Rolling Updates: Zero-downtime deployments

Node Requirements

For production:

Troubleshooting

View Pod Status

kubectl get pods -n production -l app=backend
kubectl describe pod <pod-name> -n production
kubectl logs <pod-name> -n production

Check Service Endpoints

kubectl get endpoints backend-service -n production
kubectl describe service backend-service -n production

View HPA Status

kubectl get hpa -n production
kubectl describe hpa backend-hpa-blue -n production

Check Network Policies

kubectl get networkpolicies -n production
kubectl describe networkpolicy backend-network-policy -n production

Maintenance

Regular Tasks

  1. Review and update resource limits (monthly)
  2. Check HPA scaling patterns (weekly)
  3. Review pod disruption budgets (monthly)
  4. Update security policies (quarterly)
  5. Rotate secrets (quarterly)
  6. Review and optimize autoscaling (monthly)
  7. Audit network policies (quarterly)

Upgrade Procedures

  1. Test in staging environment first
  2. Use appropriate deployment strategy
  3. Monitor metrics during upgrade
  4. Validate functionality post-upgrade
  5. Keep previous version available for rollback
  6. Document any issues or changes

Best Practices

  1. Always use resource limits and requests
  2. Implement comprehensive health checks
  3. Use meaningful labels and annotations
  4. Enable autoscaling for production workloads
  5. Implement network policies for security
  6. Monitor all deployments and services
  7. Maintain pod disruption budgets
  8. Regular backup and disaster recovery testing
  9. Document all configuration changes
  10. Follow least privilege principle

For detailed deployment procedures, see DEPLOYMENT_GUIDE.md