使用 virtctl 命令行工具

virtctl 是 KubeVirt 的标准命令行客户端。它通过补充 kubectl 无法直接处理的一些命令来扩展功能,这些命令需要流式连接或子资源调用——例如打开控制台、上传和导出镜像、热插拔磁盘,以及触发迁移。它随 KubeVirt 一起发布;请从 KubeVirt releases 中下载与集群 KubeVirt 版本匹配的二进制文件,并将其放置到你的 PATH 中。

# Example: download virtctl for the cluster's KubeVirt version
VERSION=$(kubectl get kubevirt kubevirt -n kubevirt -o jsonpath='{.status.observedKubeVirtVersion}')
curl -L -o virtctl "https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/virtctl-${VERSION}-linux-amd64"
chmod +x virtctl && sudo mv virtctl /usr/local/bin/

控制台和访问

virtctl console web-01 -n demo        # serial (text) console — see Serial Console
virtctl vnc web-01 -n demo            # graphical VNC console — see VNC
virtctl ssh user@web-01 -n demo       # SSH into the VM (uses the VM's pod network)
virtctl scp ./file user@web-01:/tmp -n demo

参见 Serial Console 和 Logging into the Virtual Machine using VNC。

上传和导出镜像

# Upload a local image into a DataVolume (CDI cdi-uploadproxy must be reachable):
virtctl image-upload dv my-image --size=20Gi --storage-class=rbd-1 \
  --image-path=./my-image.qcow2 -n demo

# Export a VM disk or snapshot for download:
virtctl vmexport download my-export --vm=web-01 --output=disk.img.gz -n demo

有关完整的本地镜像上传和虚拟机创建工作流,请参见 Uploading a Local Image and Creating a Virtual Machine,以及 Exporting Virtual Machine Images。

热插拔磁盘

virtctl addvolume web-01 --volume-name=datadisk-1 --persist -n demo
virtctl removevolume web-01 --volume-name=datadisk-1 -n demo

参见 Managing Virtual Disks。

电源和迁移

virtctl start web-01 -n demo
virtctl stop web-01 -n demo
virtctl restart web-01 -n demo
virtctl pause vm web-01 -n demo        # freeze CPU/memory
virtctl unpause vm web-01 -n demo
virtctl migrate web-01 -n demo         # start a live migration
virtctl migrate-cancel web-01 -n demo  # cancel an in-progress live migration

参见 Virtual Machine Live Migration 和 Pausing and Resuming a Virtual Machine。

使用 libguestfs 编辑和封存镜像

virtctl guestfs 会启动一个交互式 Pod,并将 libguestfs 工具附加到一个 PVC 上,因此你可以离线检查或修改磁盘镜像——例如,在将其作为可启动卷发布之前重置 machine ID 或对模板进行泛化:

virtctl guestfs my-pvc -n demo
# inside the pod:
virt-customize -a /dev/vda --root-password password:'...'
virt-sysprep -a /dev/vda        # generalize (remove host-specific data) before templating