# Autoscale with Edera zone metrics

Edera exposes per-pod zone metrics at `http://<node-ip>:3035/metrics/kubernetes`. Because pods run inside zones, these metrics are not available through cAdvisor or the kubelet. Prometheus must scrape the Edera endpoint directly.

## Prerequisites

- Prometheus installed in your cluster
- Prometheus Adapter installed in your cluster
- Edera running on each node and listening on port 3035

## Available metrics

Edera exposes per-pod zone metrics labeled with `namespace`, `pod`, and `zone_id`. For the full list, see the [Edera metrics reference](https://docs.edera.dev/reference/observability/metrics/).

## 1. Scrape the Edera daemon

Add a scrape job to Prometheus using `additionalScrapeConfigs`. The job uses Kubernetes node service discovery to find nodes automatically, then rewrites the address to target port 3035.

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: additional-scrape-configs
  namespace: monitoring
stringData:
  scrape-configs.yaml: |
    - job_name: edera
      metrics_path: /metrics/kubernetes
      kubernetes_sd_configs:
      - role: node
      relabel_configs:
      # Swap the kubelet port (10250) for the Edera metrics port (3035)
      - source_labels: [__address__]
        regex: '(.+):\d+'
        target_label: __address__
        replacement: '${1}:3035'
      # Use the node name as the instance label instead of the raw IP
      - source_labels: [__meta_kubernetes_node_name]
        target_label: instance
```

Reference the secret from your Prometheus Custom Resource:

```yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  namespace: monitoring
spec:
  additionalScrapeConfigs:
    name: additional-scrape-configs
    key: scrape-configs.yaml
  # ... rest of your Prometheus spec
```

### Verify scraping is working

```bash
# Check the target is up
kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090 &
curl -s 'http://localhost:9090/api/v1/targets' \
  | jq '.data.activeTargets[] | select(.labels.job=="edera") | {health, lastError}'

# Confirm zone metrics are present
curl -s 'http://localhost:9090/api/v1/query?query=zone_cpu_usage_percent' \
  | jq '.data.result | length'
```

## 2. Configure the Prometheus Adapter

The adapter translates Prometheus metrics into the Kubernetes custom metrics API so HPAs can consume them. Because Edera zone metrics already carry `namespace` and `pod` labels, they map directly to Kubernetes pod resources.

```yaml
# prometheus-adapter-values.yaml
prometheus:
  url: http://prometheus.monitoring.svc.cluster.local
  port: 9090

rules:
  default: false

custom:
  - seriesQuery: 'zone_cpu_usage_percent{namespace!="",pod!=""}'
    resources:
      overrides:
        namespace:
          resource: namespace
        pod:
          resource: pod
    name:
      as: zone_cpu_usage_percent
    metricsQuery: 'avg by (namespace, pod) (zone_cpu_usage_percent{<<.LabelMatchers>>})'
```

Install or upgrade the adapter:

```bash
helm upgrade --install prometheus-adapter prometheus-community/prometheus-adapter \
  --namespace monitoring \
  -f prometheus-adapter-values.yaml
```

### Verify the metric is available

```bash
# Should list zone_cpu_usage_percent
kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1 | jq '[.resources[].name]'

# Should return a value per pod
kubectl get --raw \
  "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/zone_cpu_usage_percent" \
  | jq '.items[] | {pod: .describedObject.name, value}'
```

## 3. Create a Horizontal Pod Autoscaler (HPA)

With the metric available in the custom metrics API, create an HPA that scales on average zone CPU usage across pods:

```yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-edera-app
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-edera-app
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Pods
    pods:
      metric:
        name: zone_cpu_usage_percent
      target:
        type: AverageValue
        averageValue: "70"   # scale up when average vCPU usage exceeds 70%
```

### Verify the HPA is working

```bash
# Check HPA status and current metric value
kubectl get hpa my-edera-app

# Watch scaling events
kubectl describe hpa my-edera-app
```

The `TARGETS` column in `kubectl get hpa` shows the current metric value against the target. If it reads `<unknown>`, the adapter is not reaching the metric – recheck the adapter config and confirm the metric is present in the custom metrics API.

## Next steps

- [Edera metrics reference](https://docs.edera.dev/reference/observability/metrics/) for the full list of available zone metrics
- [Zone security model](https://docs.edera.dev/technical-overview/security/security-model/) to understand what the zone boundary means for your workloads.
