# Zero-Downtime Deployment Pipeline
# Platform: Kubernetes with Istio (for traffic shifting) + Prometheus/Grafana (monitoring)
# Tools: GitHub Actions (CI/CD), Helm (for deployments), Argo Rollouts (optional for advanced canary)
# Assumptions:
# - App has /healthz and /readiness endpoints.
# - Services labeled: blue (v1 live), green (v2 staging).
# - Istio VirtualService for traffic weights.
# - Rollback trigger: >5% error rate, >2s latency, or <99% uptime over 5min window.
name: zero-downtime-deploy
on:
push:
branches: [main]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker Image
run: |
docker build -t ${{ secrets.REGISTRY }}/myapp:${{ github.sha }} .
docker push ${{ secrets.REGISTRY }}/myapp:${{ github.sha }}
- name: Unit & Integration Tests
run: |
npm test # or equivalent
# Run integration tests against staging DB
deploy-green:
needs: build-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy New Version to Green Environment
uses: helm/kubectl-apply@v1
with:
kubectl-args: apply -f k8s/green-deployment.yaml # Deploys new image to green namespace/pods
- name: Initial Health Checks on Green
run: |
kubectl rollout status deployment/myapp-green -n green --timeout=300s
# Probe /healthz via kubectl port-forward or curl
for i in {1..10}; do
curl -f http://green-svc.myapp/healthz || exit 1
sleep 10
done
echo "Green healthy ✅"
canary-rollout:
needs: deploy-green
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Start Canary (10% traffic to Green)
run: |
kubectl apply -f istio/canary-10.yaml # VirtualService: blue=90%, green=10%
- name: Monitor Canary Metrics (Prometheus Queries)
run: |
# Wait 5min, query Prometheus via kubectl port-forward or API
sleep 300
ERROR_RATE=$(promql "sum(rate(http_server_requests{status=~"5.."}[5m])) / sum(rate(http_server_requests[5m])) * 100 > 5")
LATENCY_P95=$(promql "histogram_quantile(0.95, rate(http_server_requests_duration_seconds_bucket[5m])) > 2")
UPTIME=$(promql "up{job='myapp'} == 0")
if [[ "$ERROR_RATE" == "true" || "$LATENCY_P95" == "true" || "$UPTIME" == "true" ]]; then
echo "Canary failed! Triggering rollback..."
kubectl apply -f istio/rollback-blue-100.yaml # Revert to blue=100%
exit 1
fi
echo "Canary stable ✅"
blue-green-switch:
needs: canary-rollout
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Ramp Up to Full Green (50% -> 100%)
run: |
kubectl apply -f istio/ramp-50.yaml # blue=50%, green=50%
sleep 60 # Monitor 1min
# Re-check metrics (similar to canary)
kubectl apply -f istio/green-100.yaml # green=100%, blue=0%
- name: Promote Green to Blue (Cleanup)
run: |
kubectl patch deployment/myapp-blue -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"version\":\"${{ github.sha }}\"}}}}"
kubectl delete deployment/myapp-green -n green # Scale down old blue
kubectl apply -f istio/blue-100.yaml # Now blue is live v2
- name: Final Smoke Tests & Alert
run: |
curl -f http://myapp-svc.default/healthz # Production endpoint
# Slack/Teams notification: "Deploy ${{ github.sha }} successful ✅"
# Auto-Rollback Job (triggered by failures or manual)
rollback:
if: failure() # Or webhook/manual dispatch
runs-on: ubuntu-latest
needs: [deploy-green, canary-rollout, blue-green-switch]
steps:
- name: Emergency Rollback to Blue
run: |
kubectl apply -f istio/blue-100.yaml # Instant traffic revert
kubectl rollout undo deployment/myapp-green # Or delete green
# Alert team: "Rollback complete due to failure 🚨"
Key Features Explained
1. Blue/Green
- Blue: Always live production (initially v1).
- Green: Isolated staging for v2. Deploy here first, test in isolation.
- Switch via Istio VirtualService traffic weights (no DNS/proxier TTL issues).
2. Canary Integration
- Post-green deploy: Shift 10% traffic gradually.
- Monitor with Prometheus queries (error rate, P95 latency, uptime).
- Ramp-up: 10% → 50% → 100% with pauses/checks.
3. Health Checks
- Readiness/Liveness Probes: Kubernetes built-in (
/healthz).
- Smoke Tests: Curl loops post-deploy.
- Metrics Monitoring: Prometheus for business KPIs (5min window).
4. Rollback
- Automatic: Metric thresholds fail job → apply blue-100.yaml (seconds).
- Manual: GitHub Actions dispatch or webhook.
- Immutable: Use image tags (
${{ github.sha }}), no mutable deploys.
Benefits & Zero-Downtime Guarantees
- No Downtime: Traffic shifts atomically; unhealthy pods auto-excluded.
- Observability: Grafana dashboards for real-time views.
- Scalability: Works for 100s of services; Argo Rollouts for advanced analysis.
- Cost: Green scales to zero post-switch.
Customization Tips
- No Istio? Use Kubernetes Ingress/Nginx weights or AWS ALB.
- Helm/ArgoCD: Wrap in GitOps for prod.
- Testing: Run in minikube first.
- Secrets: Use external DB/cache (no state in pods).
This pipeline deploys safely at scale. Ping for tweaks! 🕵️♂️