> ## Documentation Index
> Fetch the complete documentation index at: https://autopilot.docs.xano.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cluster Rebalancing

> Cost optimization — recompute optimal node configs and migrate workloads, provision-first or rolling drain.

The Rebalancer analyzes pod placement and computes optimal node configurations. It supports
scheduled continuous rebalancing and on-demand runs.

**CRD:** `RebalancePolicy` (cluster-scoped) — includes optional maintenance windows for
deep node consolidation, including fragile pods.

## On-demand rebalance

Via the CLI or API:

```bash theme={null}
# Dry-run: see what would change
opsctl rebalance -p <cluster>

# Execute with rolling drain (no double capacity needed)
opsctl rebalance --rolling-drain -p <cluster>

# Replace all nodes (K8s version upgrades)
opsctl rebalance --rolling-drain --replace-all -p <cluster>
```

## Execution strategies

* **Provision-first** (default): creates replacement VMs, waits for them to be ready, then drains old nodes. Requires temporary double capacity.
* **Rolling drain**: drains nodes in batches of 5–10, deletes them, and lets the scaler provision right-sized replacements. \~0.5 min/node, no double capacity.

## When to use

| Situation                              | Command                                          |
| -------------------------------------- | ------------------------------------------------ |
| After a runaway scale-up               | `opsctl rebalance --rolling-drain`               |
| Kubernetes version upgrades            | `opsctl rebalance --rolling-drain --replace-all` |
| Cost optimization (greenfield re-pack) | `opsctl rebalance` (provision-first)             |

## Key features

* **Cost comparison** — before/after cost analysis per node.
* **Crash recovery** — full state persisted in ConfigMaps; a run resumes on controller restart.
* **Maintenance windows** — scheduled deep consolidation including fragile pods.
* **Fragile pod detection** — single-replica workloads, no-PDB workloads, and OrderedReady StatefulSets are handled carefully.
* **PDB-respecting eviction** — all pod movement goes through the Kubernetes Eviction API.

## Dashboard

The **Rebalancer** page shows run history with cost comparison (before vs after). Operators
can cancel an active drain from the dashboard; the drain uncordons remaining nodes and marks
the run `Cancelled`.
