Skill: rohitg00/k8-autoscaling (version 1.0.0)

--- name: k8s-autoscaling description: Configure Kubernetes autoscaling with HPA, VPA, and KEDA. Use for horizontal/vertical pod autoscaling, event-driven scaling, and capacity management. --- # Kubernetes Autoscaling Comprehensive autoscaling using HPA, VPA, and KEDA with kubectl-mcp-server tools. ## Quick Reference ### HPA (Horizontal Pod Autoscaler) Basic CPU-based scaling: ```yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 ``` Apply and verify: ``` apply_manifest(hpa_yaml, namespace) get_hpa(namespace) ``` ### VPA (Vertical Pod Autoscaler) Right-size resource requests: ```yaml apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: my-app-vpa spec: targetRef: apiVersion: apps/v1 kind: Deployment name: my-app updatePolicy: updateMode: "Auto" ``` ## KEDA (Event-Driven Autoscaling) ### Detect KEDA Installation ``` keda_detect_tool() ``` ### List ScaledObjects ``` keda_scaledobjects_list_tool(namespace) keda_scaledobject_get_tool(name, namespace) ``` ### List ScaledJobs ``` keda_scaledjobs_list_tool(namespace) ``` ### Trigger Authentication ``` keda_triggerauths_list_tool(namespace) keda_triggerauth_get_tool(name, namespace) ``` ### KEDA-Managed HPAs ``` keda_hpa_list_tool(namespace) ``` See [KEDA-TRIGGERS.md](KEDA-TRIGGERS.md) for trigger configurations. ## Common KEDA Triggers ### Queue-Based Scaling (AWS SQS) ```yaml apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: sqs-scaler spec: scaleTargetRef: name: queue-processor minReplicaCount: 0 # Scale to zero! maxReplicaCount: 100 triggers: - type: aws-sqs-queue metadata: queueURL: https://sqs.region.amazonaws.com/... queueLength: "5" ``` ### Cron-Based Scaling ```yaml triggers: - type: cron metadata: timezone: America/New_York start: 0 8 * * 1-5 # 8 AM weekdays end: 0 18 * * 1-5 # 6 PM weekdays desiredReplicas: "10" ``` ### Prometheus Metrics ```yaml triggers: - type: prometheus metadata: serverAddress: http://prometheus:9090 metricName: http_requests_total query: sum(rate(http_requests_total{app="myapp"}[2m])) threshold: "100" ``` ## Scaling Strategies | Strategy | Tool | Use Case | |----------|------|----------| | CPU/Memory | HPA | Steady traffic patterns | | Custom metrics | HPA v2 | Business metrics | | Event-driven | KEDA | Queue processing, cron | | Vertical | VPA | Right-size requests | | Scale to zero | KEDA | Cost savings, idle workloads | ## Cost-Optimized Autoscaling ### Scale to Zero with KEDA Reduce costs for idle workloads: ``` keda_scaledobjects_list_tool(namespace) # ScaledObjects with minReplicaCount: 0 can scale to zero ``` ### Right-Size with VPA Get recommendations and apply: ``` get_resource_recommendations(namespace) # Apply VPA recommendations ``` ### Predictive Scaling Use cron triggers for known patterns: ```yaml # Scale up before traffic spike triggers: - type: cron metadata: start: 0 7 * * * # 7 AM end: 0 9 * * * # 9 AM desiredReplicas: "20" ``` ## Multi-Cluster Autoscaling Configure KEDA across clusters: ``` keda_scaledobjects_list_tool(namespace, context="production") keda_scaledobjects_list_tool(namespace, context="staging") ``` ## Troubleshooting ### HPA Not Scaling ``` get_hpa(namespace) get_pod_metrics(name, namespace) # Metrics available? describe_pod(name, namespace) # Resource requests set? ``` ### KEDA Not Triggering ``` keda_scaledobject_get_tool(name, namespace) # Check status get_events(namespace) # Check events ``` ### Common Issues | Symptom | Check | Resolution | |---------|-------|------------| | HPA unknown | Metrics server | Install metrics-server | | KEDA no scale | Trigger auth | Check TriggerAuthentication | | VPA not updating | Update mode | Set updateMode: Auto | | Scale down slow | Stabilization | Adjust stabilizationWindowSeconds | ## Best Practices 1. **Always Set Resource Requests** - HPA requires requests to calculate utilization 2. **Use Multiple Metrics** - Combine CPU + custom metrics for accuracy 3. **Stabilization Windows** - Prevent flapping with scaleDown stabilization 4. **Scale to Zero Carefully** - Consider cold start time - Use activation threshold ## Related Skills - [k8s-cost](../k8s-cost/SKILL.md) - Cost optimization - [k8s-troubleshoot](../k8s-troubleshoot/SKILL.md) - Debug scaling issues

path: KEDA-TRIGGERS.md
content: # KEDA Trigger Reference

Common KEDA trigger configurations.

## Queue-Based Triggers

### AWS SQS

```yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: sqs-scaler
spec:
  scaleTargetRef:
    name: queue-processor
  minReplicaCount: 0
  maxReplicaCount: 100
  triggers:
  - type: aws-sqs-queue
    metadata:
      queueURL: https://sqs.region.amazonaws.com/123456789/my-queue
      queueLength: "5"  # Scale when > 5 messages per pod
      awsRegion: us-east-1
    authenticationRef:
      name: aws-credentials
```

### RabbitMQ

```yaml
triggers:
- type: rabbitmq
  metadata:
    protocol: amqp
    queueName: my-queue
    mode: QueueLength
    value: "10"
    host: amqp://user:[REDACTED_SECRET]:5672
```

### Azure Service Bus

```yaml
triggers:
- type: azure-servicebus
  metadata:
    queueName: my-queue
    messageCount: "5"
  authenticationRef:
    name: azure-sb-auth
```

### Kafka

```yaml
triggers:
- type: kafka
  metadata:
    bootstrapServers: kafka:9092
    consumerGroup: my-consumer
    topic: my-topic
    lagThreshold: "100"
```

## Metric-Based Triggers

### Prometheus

```yaml
triggers:
- type: prometheus
  metadata:
    serverAddress: http://prometheus:9090
    metricName: http_requests_total
    query: sum(rate(http_requests_total{app="myapp"}[2m]))
    threshold: "100"
```

### Datadog

```yaml
triggers:
- type: datadog
  metadata:
    query: avg:system.cpu.user{app:myapp}
    queryValue: "80"
    type: global
  authenticationRef:
    name: datadog-auth
```

## Schedule-Based Triggers

### Cron

```yaml
triggers:
- type: cron
  metadata:
    timezone: America/New_York
    start: 0 8 * * 1-5   # 8 AM Mon-Fri
    end: 0 18 * * 1-5    # 6 PM Mon-Fri
    desiredReplicas: "10"
```

### Multiple Schedules

```yaml
triggers:
# Morning peak
- type: cron
  metadata:
    timezone: UTC
    start: 0 8 * * *
    end: 0 10 * * *
    desiredReplicas: "20"
# Afternoon peak
- type: cron
  metadata:
    timezone: UTC
    start: 0 14 * * *
    end: 0 16 * * *
    desiredReplicas: "15"
```

## HTTP-Based Triggers

### HTTP Request Count

```yaml
triggers:
- type: prometheus
  metadata:
    serverAddress: http://prometheus:9090
    query: sum(rate(nginx_http_requests_total[1m]))
    threshold: "1000"
```

## Database Triggers

### PostgreSQL

```yaml
triggers:
- type: postgresql
  metadata:
    connectionFromEnv: PG_CONNECTION
    query: "SELECT COUNT(*) FROM pending_jobs WHERE status = 'pending'"
    targetQueryValue: "10"
```

### MySQL

```yaml
triggers:
- type: mysql
  metadata:
    connectionStringFromEnv: MYSQL_CONNECTION
    query: "SELECT COUNT(*) FROM work_queue"
    queryValue: "5"
```

## Authentication

### TriggerAuthentication

```yaml
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: aws-credentials
spec:
  secretTargetRef:
  - parameter: awsAccessKeyID
    name: aws-secrets
    key: AWS_ACCESS_KEY_ID
  - parameter: awsSecretAccessKey
    name: aws-secrets
    key: AWS_SECRET_ACCESS_KEY
```

### ClusterTriggerAuthentication

For cluster-wide auth:

```yaml
apiVersion: keda.sh/v1alpha1
kind: ClusterTriggerAuthentication
metadata:
  name: aws-credentials-cluster
spec:
  secretTargetRef:
  - parameter: awsAccessKeyID
    name: aws-secrets
    key: AWS_ACCESS_KEY_ID
    namespace: keda
```

## Advanced Configuration

### Activation Threshold

Don't scale from 0 until threshold met:

```yaml
spec:
  minReplicaCount: 0
  advanced:
    horizontalPodAutoscalerConfig:
      behavior:
        scaleDown:
          stabilizationWindowSeconds: 300
  triggers:
  - type: prometheus
    metadata:
      threshold: "100"
    metricType: AverageValue
```

### Cool Down Period

```yaml
spec:
  cooldownPeriod: 300  # Wait 5 min before scaling down
```

### Polling Interval

```yaml
spec:
  pollingInterval: 30  # Check every 30 seconds
```

## MCP Commands

```python
# List ScaledObjects
keda_scaledobjects_list_tool(namespace)

# Get ScaledObject details
keda_scaledobject_get_tool(name, namespace)

# List TriggerAuthentications
keda_triggerauths_list_tool(namespace)

# Get TriggerAuthentication
keda_triggerauth_get_tool(name, namespace)

# List KEDA-managed HPAs
keda_hpa_list_tool(namespace)
```

## Troubleshooting

### ScaledObject Not Scaling

```python
keda_scaledobject_get_tool(name, namespace)
# Check status conditions
get_events(namespace)
```

**Common Issues:**
- Invalid trigger configuration
- Authentication failure
- Metric not found

### Metric Query Issues

```python
# For Prometheus, test query manually:
# curl prometheus:9090/api/v1/query?query=<your-query>
```


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: clean (LOW)