|
| 1 | +--- |
| 2 | +name: kubevirt |
| 3 | +description: KubeVirt VM operations via kubectl virt or oc virt. Use when the user wants to create, start, stop, console/VNC/SSH into, or inspect VMs on OpenShift/Kubernetes. Output CLI for the user to run — do not execute kubectl/oc yourself unless they ask. |
| 4 | +--- |
| 5 | + |
| 6 | +## For AI assistants |
| 7 | + |
| 8 | +Do **not** run `kubectl`, `oc`, or `virtctl` in the user’s environment unless they explicitly ask you to execute commands. Give **copy-paste** `bash` blocks, explain placeholders (for example `<vm-name>`, `<namespace>`), and note prerequisites (logged-in cluster context, namespace, installed plugin). |
| 9 | + |
| 10 | +# KubeVirt CLI (`kubectl virt` / `oc virt`) |
| 11 | + |
| 12 | +The `kubectl virt` plugin (OpenShift: `oc virt`) generates and manages KubeVirt `VirtualMachine` resources. |
| 13 | + |
| 14 | +## Install the plugin |
| 15 | + |
| 16 | +Tell the user to install the virt plugin (pick one approach): |
| 17 | + |
| 18 | +```bash |
| 19 | +# Krew (recommended) |
| 20 | +kubectl krew install virt |
| 21 | + |
| 22 | +# Verify |
| 23 | +kubectl virt version |
| 24 | +``` |
| 25 | + |
| 26 | +On OpenShift, the same plugin is typically available as `oc virt` after installing OpenShift Virtualization. |
| 27 | + |
| 28 | +## Discover flags (user runs locally) |
| 29 | + |
| 30 | +```bash |
| 31 | +kubectl virt --help |
| 32 | +kubectl virt create vm --help |
| 33 | +kubectl virt ssh --help |
| 34 | +``` |
| 35 | + |
| 36 | +## Instance types and preferences (OpenShift) |
| 37 | + |
| 38 | +Prefer **instance types** and **preferences** over raw `--memory` when the cluster exposes them: |
| 39 | + |
| 40 | +```bash |
| 41 | +kubectl get virtualmachineclusterinstancetype |
| 42 | +kubectl get virtualmachineclusterpreference |
| 43 | +``` |
| 44 | + |
| 45 | +Common patterns: `u1.medium` (universal), `fedora` / `rhel.9` / `windows.11.virtio` preferences. With a DataSource that has annotations, `--infer-instancetype` and `--infer-preference` can select sizing automatically. |
| 46 | + |
| 47 | +## List and inspect VMs |
| 48 | + |
| 49 | +```bash |
| 50 | +kubectl get vm -A |
| 51 | +kubectl get vm -n <namespace> |
| 52 | +kubectl describe vm <vm-name> -n <namespace> |
| 53 | +kubectl get vmi -n <namespace> |
| 54 | +``` |
| 55 | + |
| 56 | +## Start, stop, restart |
| 57 | + |
| 58 | +```bash |
| 59 | +kubectl virt start <vm-name> -n <namespace> |
| 60 | +kubectl virt stop <vm-name> -n <namespace> |
| 61 | +kubectl virt restart <vm-name> -n <namespace> |
| 62 | +``` |
| 63 | + |
| 64 | +## Create a VM (examples) |
| 65 | + |
| 66 | +`kubectl virt create vm` prints YAML to stdout — the user pipes it to `kubectl apply`: |
| 67 | + |
| 68 | +**Registry containerdisk + instance type** |
| 69 | + |
| 70 | +```bash |
| 71 | +kubectl virt create vm \ |
| 72 | + --name=my-fedora \ |
| 73 | + --instancetype=u1.medium \ |
| 74 | + --preference=fedora \ |
| 75 | + --volume-import=type:registry,url:docker://quay.io/containerdisks/fedora:latest,size:30Gi \ |
| 76 | + --user=fedora \ |
| 77 | + --ssh-key="$(cat ~/.ssh/id_rsa.pub)" \ |
| 78 | + | kubectl apply -f - |
| 79 | +``` |
| 80 | + |
| 81 | +**Cluster DataSource with inferred sizing** |
| 82 | + |
| 83 | +```bash |
| 84 | +kubectl virt create vm \ |
| 85 | + --name=my-vm \ |
| 86 | + --volume-import=type:ds,src:openshift-virtualization-os-images/fedora,size:30Gi \ |
| 87 | + --infer-instancetype --infer-preference \ |
| 88 | + --user=fedora \ |
| 89 | + --ssh-key="$(cat ~/.ssh/id_rsa.pub)" \ |
| 90 | + | kubectl apply -f - |
| 91 | +``` |
| 92 | + |
| 93 | +**Fallback when no instance types (memory only)** |
| 94 | + |
| 95 | +```bash |
| 96 | +kubectl virt create vm \ |
| 97 | + --name=my-vm \ |
| 98 | + --memory=2Gi \ |
| 99 | + --run-strategy=Always \ |
| 100 | + --volume-import=type:registry,url:docker://quay.io/containerdisks/fedora:latest,size:30Gi \ |
| 101 | + --user=fedora \ |
| 102 | + --ssh-key="$(cat ~/.ssh/id_rsa.pub)" \ |
| 103 | + | kubectl apply -f - |
| 104 | +``` |
| 105 | + |
| 106 | +List DataSources (OpenShift golden images): |
| 107 | + |
| 108 | +```bash |
| 109 | +kubectl get datasource -n openshift-virtualization-os-images |
| 110 | +``` |
| 111 | + |
| 112 | +### Storage gotcha |
| 113 | + |
| 114 | +If DataVolumes stay pending with missing access mode / storage class, the cluster may lack a default StorageClass. The user can mark one: |
| 115 | + |
| 116 | +```bash |
| 117 | +kubectl get storageclass |
| 118 | +kubectl annotate storageclass <name> storageclass.kubernetes.io/is-default-class=true |
| 119 | +``` |
| 120 | + |
| 121 | +## Console and VNC |
| 122 | + |
| 123 | +```bash |
| 124 | +kubectl virt console <vm-name> -n <namespace> |
| 125 | +kubectl virt vnc <vm-name> -n <namespace> |
| 126 | +``` |
| 127 | + |
| 128 | +## SSH (virtctl-style via plugin) |
| 129 | + |
| 130 | +```bash |
| 131 | +kubectl virt ssh fedora@<vm-name> -n <namespace> |
| 132 | +kubectl virt ssh fedora@<vm-name> -n <namespace> --identity-file=~/.ssh/id_rsa |
| 133 | +kubectl virt scp ./local.txt fedora@vmi/<vm-name>:./remote.txt -n <namespace> |
| 134 | +``` |
| 135 | + |
| 136 | +## Pause, migrate |
| 137 | + |
| 138 | +```bash |
| 139 | +kubectl virt pause vm <vm-name> -n <namespace> |
| 140 | +kubectl virt unpause vm <vm-name> -n <namespace> |
| 141 | +kubectl virt migrate <vm-name> -n <namespace> |
| 142 | +``` |
| 143 | + |
| 144 | +When unsure of a subcommand, tell the user to run `kubectl virt <command> --help` locally. |
0 commit comments