$npx -y skills add BagelHole/DevOps-Security-Agent-Skills --skill blue-green-deployConfigure zero-downtime deployment strategies including blue-green, canary, and rolling deployments. Implement traffic shifting, health checks, and rollback procedures. Use when implementing production deployment strategies or zero-downtime releases.
| 1 | # Blue-Green & Deployment Strategies |
| 2 | |
| 3 | Implement zero-downtime deployment patterns for production systems. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Implementing zero-downtime deployments |
| 9 | - Reducing deployment risk |
| 10 | - Enabling instant rollbacks |
| 11 | - Running canary releases |
| 12 | - Performing A/B testing in production |
| 13 | |
| 14 | ## Prerequisites |
| 15 | |
| 16 | - Load balancer or ingress controller |
| 17 | - Container orchestration (K8s) or cloud platform |
| 18 | - CI/CD pipeline |
| 19 | - Health check endpoints |
| 20 | |
| 21 | ## Deployment Strategy Overview |
| 22 | |
| 23 | ``` |
| 24 | ┌─────────────────────────────────────────────────────────────┐ |
| 25 | │ DEPLOYMENT STRATEGIES │ |
| 26 | ├─────────────┬─────────────┬─────────────┬──────────────────┤ |
| 27 | │ Blue-Green │ Canary │ Rolling │ Recreate │ |
| 28 | ├─────────────┼─────────────┼─────────────┼──────────────────┤ |
| 29 | │ Full env │ Gradual % │ Pod by pod │ All at once │ |
| 30 | │ swap │ rollout │ replacement │ │ |
| 31 | ├─────────────┼─────────────┼─────────────┼──────────────────┤ |
| 32 | │ Instant │ Slow, safe │ Moderate │ Fast, risky │ |
| 33 | │ rollback │ rollback │ rollback │ │ |
| 34 | ├─────────────┼─────────────┼─────────────┼──────────────────┤ |
| 35 | │ 2x resources│ +10-25% │ Same │ Same │ |
| 36 | │ needed │ resources │ resources │ │ |
| 37 | └─────────────┴─────────────┴─────────────┴──────────────────┘ |
| 38 | ``` |
| 39 | |
| 40 | ## Blue-Green Deployment |
| 41 | |
| 42 | ### Concept |
| 43 | |
| 44 | ``` |
| 45 | Before: |
| 46 | ┌─────────┐ ┌───────────────┐ |
| 47 | │ Users │────▶│ Blue (v1) │ ◀── Active |
| 48 | └─────────┘ └───────────────┘ |
| 49 | ┌───────────────┐ |
| 50 | │ Green (v2) │ ◀── Staging |
| 51 | └───────────────┘ |
| 52 | |
| 53 | After Switch: |
| 54 | ┌─────────┐ ┌───────────────┐ |
| 55 | │ Users │ │ Blue (v1) │ ◀── Standby |
| 56 | └─────────┘ └───────────────┘ |
| 57 | │ ┌───────────────┐ |
| 58 | └────────▶│ Green (v2) │ ◀── Active |
| 59 | └───────────────┘ |
| 60 | ``` |
| 61 | |
| 62 | ### Kubernetes Implementation |
| 63 | |
| 64 | ```yaml |
| 65 | # blue-deployment.yaml |
| 66 | apiVersion: apps/v1 |
| 67 | kind: Deployment |
| 68 | metadata: |
| 69 | name: myapp-blue |
| 70 | labels: |
| 71 | app: myapp |
| 72 | version: blue |
| 73 | spec: |
| 74 | replicas: 3 |
| 75 | selector: |
| 76 | matchLabels: |
| 77 | app: myapp |
| 78 | version: blue |
| 79 | template: |
| 80 | metadata: |
| 81 | labels: |
| 82 | app: myapp |
| 83 | version: blue |
| 84 | spec: |
| 85 | containers: |
| 86 | - name: myapp |
| 87 | image: myapp:v1.0.0 |
| 88 | ports: |
| 89 | - containerPort: 8080 |
| 90 | readinessProbe: |
| 91 | httpGet: |
| 92 | path: /health |
| 93 | port: 8080 |
| 94 | initialDelaySeconds: 5 |
| 95 | periodSeconds: 5 |
| 96 | --- |
| 97 | # green-deployment.yaml |
| 98 | apiVersion: apps/v1 |
| 99 | kind: Deployment |
| 100 | metadata: |
| 101 | name: myapp-green |
| 102 | labels: |
| 103 | app: myapp |
| 104 | version: green |
| 105 | spec: |
| 106 | replicas: 3 |
| 107 | selector: |
| 108 | matchLabels: |
| 109 | app: myapp |
| 110 | version: green |
| 111 | template: |
| 112 | metadata: |
| 113 | labels: |
| 114 | app: myapp |
| 115 | version: green |
| 116 | spec: |
| 117 | containers: |
| 118 | - name: myapp |
| 119 | image: myapp:v2.0.0 |
| 120 | ports: |
| 121 | - containerPort: 8080 |
| 122 | readinessProbe: |
| 123 | httpGet: |
| 124 | path: /health |
| 125 | port: 8080 |
| 126 | initialDelaySeconds: 5 |
| 127 | periodSeconds: 5 |
| 128 | --- |
| 129 | # service.yaml - Switch by changing selector |
| 130 | apiVersion: v1 |
| 131 | kind: Service |
| 132 | metadata: |
| 133 | name: myapp |
| 134 | spec: |
| 135 | selector: |
| 136 | app: myapp |
| 137 | version: blue # Change to 'green' to switch |
| 138 | ports: |
| 139 | - port: 80 |
| 140 | targetPort: 8080 |
| 141 | ``` |
| 142 | |
| 143 | ### Switch Script |
| 144 | |
| 145 | ```bash |
| 146 | #!/bin/bash |
| 147 | # blue-green-switch.sh |
| 148 | |
| 149 | CURRENT=$(kubectl get svc myapp -o jsonpath='{.spec.selector.version}') |
| 150 | NEW_VERSION=$1 |
| 151 | |
| 152 | echo "Current version: $CURRENT" |
| 153 | echo "Switching to: $NEW_VERSION" |
| 154 | |
| 155 | # Verify new deployment is ready |
| 156 | kubectl rollout status deployment/myapp-$NEW_VERSION |
| 157 | |
| 158 | # Check health |
| 159 | HEALTH=$(kubectl exec -it deployment/myapp-$NEW_VERSION -- curl -s localhost:8080/health) |
| 160 | if [ "$HEALTH" != "ok" ]; then |
| 161 | echo "Health check failed" |
| 162 | exit 1 |
| 163 | fi |
| 164 | |
| 165 | # Switch traffic |
| 166 | kubectl patch svc myapp -p "{\"spec\":{\"selector\":{\"version\":\"$NEW_VERSION\"}}}" |
| 167 | |
| 168 | echo "Switched to $NEW_VERSION" |
| 169 | ``` |
| 170 | |
| 171 | ### AWS ECS Blue-Green |
| 172 | |
| 173 | ```yaml |
| 174 | # AWS CodeDeploy appspec.yml |
| 175 | version: 0.0 |
| 176 | Resources: |
| 177 | - TargetService: |
| 178 | Type: AWS::ECS::Service |
| 179 | Properties: |
| 180 | TaskDefinition: "arn:aws:ecs:region:account:task-definition/myapp:2" |
| 181 | LoadBalancerInfo: |
| 182 | ContainerName: "myapp" |
| 183 | ContainerPort: 8080 |
| 184 | Hooks: |
| 185 | - BeforeInstall: "LambdaFunctionToValidateBeforeTrafficShift" |
| 186 | - AfterInstall: "LambdaFunctionToValidateAfterTrafficShift" |
| 187 | - AfterAllowTestTraffic: "LambdaFunctionToValidateTestTraffic" |
| 188 | - BeforeAllowTraffic: "LambdaFunctionToValidateBeforeAllowTraffic" |
| 189 | - AfterAllowTraffic: "LambdaFunctionToValidateAfterAllowTraffic" |
| 190 | ``` |
| 191 | |
| 192 | ## Canary Depl |