Vm rebalance

From RHEcosystemAppEng/agentic-plugins · View interactive version

Vm rebalance is an AI agent skill from the GitHub repository RHEcosystemAppEng/agentic-plugins. Orchestrate VM migrations across cluster nodes for load balancing, maintenance, and resource optimization. Use when: - "Move VM database-01 to worker-03" - "Rebalance VMs to optimize CPU load" - "Drain worker-02 for maintenance" - "Automatically rebalance the cluster" Supports Manual (user-driven) a

How to use this skill

Skill file

/vm-rebalance Skill

Orchestrate VM migrations across OpenShift cluster nodes for load balancing, maintenance, and resource optimization. Supports manual and automatic rebalancing with live migration (zero downtime) and cold migration (brief downtime) strategies.

Implementation: Uses KubeVirt's VirtualMachineInstanceMigration API for live migrations and node affinity for cold migrations.

Prerequisites

Required MCP Server: openshift-virtualization (OpenShift MCP Server)

Required MCP Tools:

Required Environment Variables:

Required Cluster Setup:

Prerequisite Verification

Before executing:

  1. Check openshift-virtualization exists in mcps.json → If missing, report setup
  2. Verify KUBECONFIG is set (presence only, never expose value) → If missing, report
  3. For live migration: Check PVC access mode is ReadWriteMany (RWX) via resources_get

Human Notification Protocol: ❌ Cannot execute vm-rebalance: MCP server not available. Setup: Add to mcps.json, set KUBECONFIG, restart Claude Code. Docs: https://github.com/openshift/openshift-mcp-server

⚠️ SECURITY: Never display KUBECONFIG path or credential values.

When to Use This Skill

Trigger when:

User phrases:

Do NOT use when:

Workflow

Step 1: Determine Rebalancing Mode

Manual Mode: User specifies VM name(s) and target node(s). Example: "Move VM database-01 to worker-03"

Automatic Mode: User requests AI-driven rebalancing. Example: "Rebalance VMs based on CPU"

Step 2: Load Strategy File and Execute

For Manual Mode:

Document Consultation (REQUIRED - Execute FIRST):

  1. Action: Read REBALANCE_MANUAL.md using Read tool
  2. Output to user: "I consulted REBALANCE_MANUAL.md to understand the manual migration workflow."
  3. Then execute: Follow workflow in REBALANCE_MANUAL.md

For Automatic Mode:

Document Consultation (REQUIRED - Execute FIRST):

  1. Action: Read REBALANCE_AUTOMATIC.md using Read tool
  2. Output to user: "I consulted REBALANCE_AUTOMATIC.md to understand the automatic rebalancing workflow."
  3. Then execute: Follow workflow in REBALANCE_AUTOMATIC.md

Common Validation Logic

Shared by ALL migration strategies. Execute before any VM migration:

Validation 1: Verify VM Exists

MCP Tool: resources_get (apiVersion="kubevirt.io/v1", kind="VirtualMachine", name=<vm>, namespace=<ns>)

Extract: spec.template.spec.volumes[].persistentVolumeClaim.claimName, status.ready

Errors: VM not found → Use vm-inventory | Namespace not found → Verify name | Permission denied → Check RBAC

Validation 2: Check Current VM Location

MCP Tool (if VM running): resources_get (apiVersion="kubevirt.io/v1", kind="VirtualMachineInstance", name=<vm>, namespace=<ns>)

Extract: status.nodeName, status.phase

Validation: If already on target → "VM already on target node. No migration needed."

Validation 3: Validate Storage Compatibility

MCP Tool: resources_get (apiVersion="v1", kind="PersistentVolumeClaim", name=<pvc>, namespace=<ns>)

Extract: spec.accessModes

Error for live migration: If RWO → "Cannot live migrate. Use cold migration (brief downtime ~30-60s)."

Reference: references/live-migration-best-practices.md

Validation 4: Verify Target Node Exists

MCP Tool: resources_list (apiVersion="v1", kind="Node")

Validation: Verify target exists, status.conditions[] shows Ready=True, not cordoned

Errors: Not found → "Node doesn't exist" | Not Ready → "Choose different target" | Cordoned → "Uncordon or choose different target"

Reference: scheduling-errors.md

Node Selection for Automatic Rebalancing

Applies to Automatic Mode only.

Use resources_list (apiVersion="v1", kind="Node")

Filter where ALL true:

  1. metadata.labels["kubevirt.io/schedulable"] == "true"
  2. status.capacity["devices.kubevirt.io/kvm"] > "0"
  3. No node-role.kubernetes.io/control-plane or node-role.kubernetes.io/master label

If no nodes: "No suitable nodes. Check OpenShift Virtualization operator and hardware virtualization support."

Note: Ignore custom taints. Use official KubeVirt labels.

Common Migration Types

Live Migration: Zero downtime, <1s pause during cutover. Requires RWX storage. Memory transferred while VM runs.

Cold Migration: Brief downtime (~30-60s). Works with any storage. Stop VM → Update placement → Start on target.

Reference: references/live-migration-best-practices.md

Common Plan Visualization

ALL strategies MUST use this standardized format for consistency.

Information Relevance Principle

Show only what matters:

Standard Plan Format

Table 1: VM Rebalance Plan

## 📋 VM Rebalance Plan

| VM | Instance Type | Current Node | → | New Node | Type | Downtime | Notes |
|----|---------------|--------------|---|----------|------|----------|-------|
| vm-1 | u1.xlarge | worker-01 | → | worker-03 | Live | <1s pause | ContainerDisk |
| vm-2 | u1.2xmedium | worker-01 | → | worker-02 | Cold | ~40s | RWO storage |
| vm-3 | u1.medium | worker-02 | - | *stays* | - | - | Already balanced |

Column Definitions: VM name | Instance type | Current node | Movement indicator | Target node (or stays) | Migration type (Live/Cold/-) | Downtime (<1s/~30-60s/-) | Brief explanation

Table 2: Node State Before → After

## 📊 Node State: Before → After

| Node | VMs Now | CPU Now | Memory Now | → | VMs After | CPU After | Memory After | Change |
|------|---------|---------|------------|---|-----------|-----------|--------------|--------|
| worker-01 | 5 | 85% | 72% | → | 3 | 68% | 59% | ✓ Reduced load |
| worker-02 | 2 | 42% | 48% | → | 3 | 58% | 61% | ← Receiving VMs |
| worker-03 | 3 | 38% | 51% | → | 4 | 55% | 63% | ← Receiving VMs |

CRITICAL - Capacity Calculation Method:

CPU/Memory percentages MUST be calculated based on allocated capacity, not actual runtime usage:

CPU Percentage Calculation:

  1. Get node total CPU capacity from resources_get Node → status.capacity.cpu (e.g., "32" = 32 vCPUs)
  2. For each VM on node, get allocated vCPUs from VMI → spec.domain.cpu.sockets × spec.domain.cpu.cores × spec.domain.cpu.threads
  3. Sum all VM vCPUs on the node
  4. Calculate: (Sum of VM vCPUs / Node CPU capacity) × 100

Memory Percentage Calculation:

  1. Get node total memory capacity from resources_get Node → status.capacity.memory (e.g., "128Gi")
  2. For each VM on node, get allocated memory from VMI → spec.domain.memory.guest
  3. Sum all VM memory allocations on the node (convert to same units)
  4. Calculate: (Sum of VM memory / Node memory capacity) × 100

Example: Node with 32 vCPUs hosting VMs with 2+4+8+4+2 = 20 vCPUs → CPU = 62.5% (20/32), NOT the actual runtime usage which might be 0% if VMs are idle.

Rationale: Shows capacity planning (how much is reserved) rather than runtime utilization, which is more useful for rebalancing decisions.

Overcommit Detection and Warning:

If any node's CPU or Memory percentage exceeds 100% after rebalancing:

⚠️ **OVERCOMMIT WARNING**

**Node(s) will be overcommitted after this rebalance:**
- **worker-02**: CPU 125% (40 vCPUs allocated / 32 vCPUs capacity) - **25% overcommit**
- **worker-03**: Memory 110% (88Gi allocated / 80Gi capacity) - **10% overcommit**

**Impact:**
- **CPU overcommit**: VMs may experience CPU throttling and reduced performance when all VMs are active simultaneously
- **Memory overcommit**: Risk of VM eviction or OOM (Out of Memory) if total memory demand exceeds node capacity

**Recommendations:**
- Consider distributing VMs across more nodes to avoid overcommit
- Review VM instance types to ensure they match actual workload requirements
- Monitor node resource usage closely after rebalancing

**Proceed with overcommit?** (yes/cancel)

When NOT to warn: If percentages ≤ 100%, overcommit is not present. Omit this warning section.

After tables, include:

Key Improvement: "Distribution from 1 node to 4 nodes hosting VMs" or "CPU variance reduced from 22% to 4% (81% improvement)"

Rebalance Summary (batch operations):

- Total VMs: 5 | Live: 4 | Cold: 1 | Staying: 2
- Total Downtime: ~40s | Duration: 1-2min (parallel)

Execution Mode: **Parallel** (default) - all VMs rebalance simultaneously OR **Sequential** (user requested)

Terminology Standards:

Common Error Handling

Error 1: Live Migration Fails - Storage Not RWX

Symptom: "Cannot live migrate: PVC access mode is ReadWriteOnce"

Solution: Use cold migration OR convert PVC to RWX

Reference: storage-errors.md

Error 2: VM Stuck ErrorUnschedulable After Cold Migration

Symptom: "VM cannot be scheduled: ErrorUnschedulable"

Solution: Check node capacity (nodes_top), verify no blocking taints (resources_get Node), add tolerations, choose different target, remove nodeSelector

Reference: scheduling-errors.md

Error 3: Live Migration Times Out

Symptom: "Migration exceeded timeout: 150s per GiB"

Solution: Retry migration, reduce VM workload, use cold migration, increase timeout in HyperConverged CR

Reference: references/performance-tuning.md

Error 4: Migration Rejected - Cluster Limit Reached

Symptom: "Migration rejected: cluster limit reached (5 concurrent)"

Solution: Wait for migrations to complete (resources_list VirtualMachineInstanceMigration), retry, migrate sequentially, increase limit

Reference: references/performance-tuning.md

Error 5: RBAC Permission Denied

Symptom: "Forbidden: User cannot create VirtualMachineInstanceMigration"

Solution: Verify RBAC permissions (create on VirtualMachineInstanceMigration, update on VirtualMachine), contact admin

Error 6: Network Saturation

Symptom: Multiple migrations slow/fail, high network utilization

Solution: Reduce concurrent migrations, set bandwidth limit, use dedicated migration network

Reference: references/performance-tuning.md

Error 7: Resource Version Conflict During Cold Migration

Symptom: "Apply failed: conflict with 'kubernetes-mcp-server' using .spec.runStrategy"

Solution: After vm_lifecycle stop, re-read VM using resources_get before updating nodeAffinity (gets fresh resourceVersion)

Workflow: Stop → Wait → Re-read → Update nodeAffinity → Start

Reference: REBALANCE_MANUAL.md - Sub-step 4b.2.5

Dependencies

Required MCP Servers

Required MCP Tools

Related Skills

Reference Documentation

Skill Strategy Files:

Performance and Best Practices:

Troubleshooting:

Official Documentation:

Critical: Human-in-the-Loop Requirements

IMPORTANT: This skill performs VM migrations affecting placement and availability. You MUST:

  1. Before Initiating Migration
  1. Never Auto-Execute
  1. For Batch Operations

Why: Live migration (brief pause, bandwidth, performance impact), Cold migration (downtime, dropped connections), Wrong node (performance degradation), Batch (network saturation)

Rationale: Prevents unintended disruption; maintains user control.

Security Considerations


Strategy Implementation: ✅ REBALANCE_MANUAL.md | ✅ REBALANCE_AUTOMATIC.md

Reference Documentation: ✅ live-migration-best-practices.md | ✅ performance-tuning.md | ✅ anti-patterns.md | ✅ production-considerations.md

Last Updated: 2026-02-24 | OpenShift Virtualization: 4.17, 4.18, 4.19, 4.20

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.