|
| 1 | +# Cross-Region AutoScaling |
| 2 | + |
| 3 | +## Overview |
| 4 | +This guide provides detailed instructions for implementing cross-region autoscaling in Kubernetes deployments, ensuring optimal resource utilization and cost efficiency across multiple regions. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | +- Multi-region Kubernetes clusters |
| 8 | +- kubectl configured |
| 9 | +- Helm installed |
| 10 | +- Basic understanding of autoscaling concepts |
| 11 | +- Access to create and modify resources |
| 12 | + |
| 13 | +## Cluster Autoscaler Setup |
| 14 | + |
| 15 | +### 1. Install Cluster Autoscaler |
| 16 | +```bash |
| 17 | +# Add Helm repository |
| 18 | +helm repo add autoscaler https://kubernetes.github.io/autoscaler |
| 19 | +helm repo update |
| 20 | + |
| 21 | +# Install Cluster Autoscaler |
| 22 | +helm install cluster-autoscaler autoscaler/cluster-autoscaler \ |
| 23 | + --namespace kube-system \ |
| 24 | + --set autoDiscovery.clusterName=my-cluster \ |
| 25 | + --set awsRegion=us-east-1 \ |
| 26 | + --set rbac.create=true \ |
| 27 | + --set extraArgs.balance-similar-node-groups=true \ |
| 28 | + --set extraArgs.skip-nodes-with-system-pods=false \ |
| 29 | + --set extraArgs.scale-down-unneeded-time=10m \ |
| 30 | + --set extraArgs.scale-down-delay-after-add=10m |
| 31 | +``` |
| 32 | + |
| 33 | +### 2. Configure Cross-Region Scaling |
| 34 | +```yaml |
| 35 | +# cross-region-autoscaler.yaml |
| 36 | +apiVersion: autoscaling.openshift.io/v1 |
| 37 | +kind: ClusterAutoscaler |
| 38 | +metadata: |
| 39 | + name: cross-region-autoscaler |
| 40 | +spec: |
| 41 | + resourceLimits: |
| 42 | + maxNodesTotal: 100 |
| 43 | + cores: |
| 44 | + min: 8 |
| 45 | + max: 128 |
| 46 | + memory: |
| 47 | + min: 32 |
| 48 | + max: 512 |
| 49 | + scaleDown: |
| 50 | + enabled: true |
| 51 | + delayAfterAdd: 10m |
| 52 | + delayAfterDelete: 10m |
| 53 | + delayAfterFailure: 3m |
| 54 | + unneededTime: 10m |
| 55 | +``` |
| 56 | +
|
| 57 | +## Horizontal Pod Autoscaling |
| 58 | +
|
| 59 | +### 1. Configure HPA |
| 60 | +```yaml |
| 61 | +# cross-region-hpa.yaml |
| 62 | +apiVersion: autoscaling/v2 |
| 63 | +kind: HorizontalPodAutoscaler |
| 64 | +metadata: |
| 65 | + name: cross-region-hpa |
| 66 | + namespace: my-app |
| 67 | +spec: |
| 68 | + scaleTargetRef: |
| 69 | + apiVersion: apps/v1 |
| 70 | + kind: Deployment |
| 71 | + name: my-app |
| 72 | + minReplicas: 2 |
| 73 | + maxReplicas: 10 |
| 74 | + metrics: |
| 75 | + - type: Resource |
| 76 | + resource: |
| 77 | + name: cpu |
| 78 | + target: |
| 79 | + type: Utilization |
| 80 | + averageUtilization: 70 |
| 81 | + - type: Resource |
| 82 | + resource: |
| 83 | + name: memory |
| 84 | + target: |
| 85 | + type: Utilization |
| 86 | + averageUtilization: 80 |
| 87 | + behavior: |
| 88 | + scaleUp: |
| 89 | + stabilizationWindowSeconds: 60 |
| 90 | + policies: |
| 91 | + - type: Pods |
| 92 | + value: 4 |
| 93 | + periodSeconds: 60 |
| 94 | + - type: Percent |
| 95 | + value: 100 |
| 96 | + periodSeconds: 60 |
| 97 | + scaleDown: |
| 98 | + stabilizationWindowSeconds: 300 |
| 99 | + policies: |
| 100 | + - type: Pods |
| 101 | + value: 4 |
| 102 | + periodSeconds: 60 |
| 103 | + - type: Percent |
| 104 | + value: 50 |
| 105 | + periodSeconds: 60 |
| 106 | +``` |
| 107 | +
|
| 108 | +### 2. Configure Custom Metrics |
| 109 | +```yaml |
| 110 | +# custom-metrics.yaml |
| 111 | +apiVersion: v1 |
| 112 | +kind: ConfigMap |
| 113 | +metadata: |
| 114 | + name: custom-metrics-config |
| 115 | + namespace: kube-system |
| 116 | +data: |
| 117 | + config.yaml: | |
| 118 | + rules: |
| 119 | + - seriesQuery: 'http_requests_total{namespace!="",pod!=""}' |
| 120 | + resources: |
| 121 | + overrides: |
| 122 | + namespace: |
| 123 | + resource: "namespace" |
| 124 | + pod: |
| 125 | + resource: "pod" |
| 126 | + name: |
| 127 | + matches: "http_requests_total" |
| 128 | + as: "http_requests_per_second" |
| 129 | + metricsQuery: 'sum(rate(http_requests_total{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)' |
| 130 | +``` |
| 131 | +
|
| 132 | +## Vertical Pod Autoscaling |
| 133 | +
|
| 134 | +### 1. Install VPA |
| 135 | +```bash |
| 136 | +# Add VPA Helm repository |
| 137 | +helm repo add fairwinds-stable https://charts.fairwinds.com/stable |
| 138 | +helm repo update |
| 139 | + |
| 140 | +# Install VPA |
| 141 | +helm install vpa fairwinds-stable/vpa \ |
| 142 | + --namespace kube-system \ |
| 143 | + --set recommender.enabled=true \ |
| 144 | + --set updater.enabled=true \ |
| 145 | + --set admissionController.enabled=true |
| 146 | +``` |
| 147 | + |
| 148 | +### 2. Configure VPA |
| 149 | +```yaml |
| 150 | +# vpa-config.yaml |
| 151 | +apiVersion: autoscaling.k8s.io/v1 |
| 152 | +kind: VerticalPodAutoscaler |
| 153 | +metadata: |
| 154 | + name: my-app-vpa |
| 155 | + namespace: my-app |
| 156 | +spec: |
| 157 | + targetRef: |
| 158 | + apiVersion: "apps/v1" |
| 159 | + kind: Deployment |
| 160 | + name: my-app |
| 161 | + updatePolicy: |
| 162 | + updateMode: "Auto" |
| 163 | + resourcePolicy: |
| 164 | + containerPolicies: |
| 165 | + - containerName: "*" |
| 166 | + minAllowed: |
| 167 | + cpu: 100m |
| 168 | + memory: 50Mi |
| 169 | + maxAllowed: |
| 170 | + cpu: 1 |
| 171 | + memory: 500Mi |
| 172 | + controlledResources: ["cpu", "memory"] |
| 173 | +``` |
| 174 | +
|
| 175 | +## Monitoring and Alerts |
| 176 | +
|
| 177 | +### 1. Configure Prometheus Rules |
| 178 | +```yaml |
| 179 | +# autoscaling-alerts.yaml |
| 180 | +apiVersion: monitoring.coreos.com/v1 |
| 181 | +kind: PrometheusRule |
| 182 | +metadata: |
| 183 | + name: autoscaling-alerts |
| 184 | + namespace: monitoring |
| 185 | +spec: |
| 186 | + groups: |
| 187 | + - name: autoscaling |
| 188 | + rules: |
| 189 | + - alert: HighScalingActivity |
| 190 | + expr: rate(hpa_scaling_events_total[5m]) > 0.1 |
| 191 | + for: 5m |
| 192 | + labels: |
| 193 | + severity: warning |
| 194 | + annotations: |
| 195 | + summary: High scaling activity detected |
| 196 | + description: More than 0.1 scaling events per minute for 5 minutes |
| 197 | + - alert: FailedScaling |
| 198 | + expr: hpa_scaling_errors_total > 0 |
| 199 | + for: 1m |
| 200 | + labels: |
| 201 | + severity: critical |
| 202 | + annotations: |
| 203 | + summary: Scaling failures detected |
| 204 | + description: HPA has failed to scale the deployment |
| 205 | +``` |
| 206 | +
|
| 207 | +### 2. Create Monitoring Dashboard |
| 208 | +```yaml |
| 209 | +# autoscaling-dashboard.yaml |
| 210 | +apiVersion: integreatly.org/v1alpha1 |
| 211 | +kind: GrafanaDashboard |
| 212 | +metadata: |
| 213 | + name: autoscaling-monitoring |
| 214 | + namespace: monitoring |
| 215 | +spec: |
| 216 | + json: | |
| 217 | + { |
| 218 | + "dashboard": { |
| 219 | + "title": "Cross-Region Autoscaling", |
| 220 | + "panels": [ |
| 221 | + { |
| 222 | + "title": "Cluster Node Count", |
| 223 | + "type": "graph", |
| 224 | + "datasource": "Prometheus", |
| 225 | + "targets": [ |
| 226 | + { |
| 227 | + "expr": "kube_node_status_capacity", |
| 228 | + "legendFormat": "{{node}}" |
| 229 | + } |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "title": "Pod Count", |
| 234 | + "type": "graph", |
| 235 | + "datasource": "Prometheus", |
| 236 | + "targets": [ |
| 237 | + { |
| 238 | + "expr": "kube_pod_info", |
| 239 | + "legendFormat": "{{namespace}}" |
| 240 | + } |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "title": "Resource Utilization", |
| 245 | + "type": "graph", |
| 246 | + "datasource": "Prometheus", |
| 247 | + "targets": [ |
| 248 | + { |
| 249 | + "expr": "sum(rate(container_cpu_usage_seconds_total[5m])) by (namespace)", |
| 250 | + "legendFormat": "CPU {{namespace}}" |
| 251 | + }, |
| 252 | + { |
| 253 | + "expr": "sum(container_memory_usage_bytes) by (namespace)", |
| 254 | + "legendFormat": "Memory {{namespace}}" |
| 255 | + } |
| 256 | + ] |
| 257 | + } |
| 258 | + ] |
| 259 | + } |
| 260 | + } |
| 261 | +``` |
| 262 | +
|
| 263 | +## Best Practices |
| 264 | +
|
| 265 | +### 1. Autoscaling Configuration |
| 266 | +- Set appropriate thresholds |
| 267 | +- Configure stabilization windows |
| 268 | +- Monitor scaling events |
| 269 | +- Regular review |
| 270 | +- Document changes |
| 271 | +
|
| 272 | +### 2. Resource Management |
| 273 | +- Right-size resources |
| 274 | +- Monitor utilization |
| 275 | +- Set limits and requests |
| 276 | +- Regular optimization |
| 277 | +- Document policies |
| 278 | +
|
| 279 | +### 3. Cost Optimization |
| 280 | +- Use spot instances |
| 281 | +- Implement scaling policies |
| 282 | +- Monitor costs |
| 283 | +- Regular review |
| 284 | +- Document savings |
| 285 | +
|
| 286 | +## Next Steps |
| 287 | +1. Monitor scaling behavior |
| 288 | +2. Optimize thresholds |
| 289 | +3. Implement cost controls |
| 290 | +4. Regular reviews |
| 291 | +5. Documentation |
0 commit comments