文章大纲
在使用 VM 时,通常应用的数据不会跟根盘放在一起,为此需要为 VM 添加额外的硬盘。
磁盘热插拔
KubeVirt 支持热插拔,将磁盘挂载到正在运行的 VMI 上,当重启时,磁盘也将附加到重启后的虚拟机上。
启用热插拔功能
在 KubeVirt 项目中,卷的热插拔还是属于不稳定的特性(在 OpenShift CNV 中这是稳定的特性,但不能将热插拔的磁盘设置为 virtio
,只能设置为 scsi
,但是在离线的情况下可以设置为 virtio
)。
为 KubeVirt 启动热插拔特性 HotplugVolumes
:
vagrant@master01:~$ kubectl edit kubevirt kubevirt -n kubevirt
启用特性:
spec:
certificateRotateStrategy: {}
configuration:
developerConfiguration:
featureGates:
- HotplugVolumes
创建用于热插拔的磁盘
创建一个新的 PVC,如果需要导入数据也可以使用 DataVolume
。
以下是通过 DataVolume 创建一个新的空 PVC:
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: example-volume-hotplug
spec:
source:
blank: {}
pvc:
accessModes:
- ReadWriteMany
volumeMode: Block
resources:
requests:
storage: 10Gi
创建并查看状态:
vagrant@master01:~$ kubectl get pvc example-volume-hotplug -n cdi-test
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
example-volume-hotplug Bound pvc-336bc844-b018-4e1c-878e-d73512331d5a 10Gi RWX rook-ceph-block <unset> 39s
添加卷到虚拟机
使用 virtctl
将创建的 PVC 关联到虚拟机中:
vagrant@master01:~$ virtctl -n cdi-test addvolume fedora-vm --volume-name=example-volume-hotplug
Successfully submitted add volume request to VM fedora-vm for volume example-volume-hotplug
登录到虚拟机中进行验证:
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 07:23:16 2024 from 10.244.241.120
[fedora@fedora-vm ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 10G 0 disk
sr0 11:0 1 1M 0 rom
zram0 251:0 0 435M 0 disk [SWAP]
vda 252:0 0 5G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1000M 0 part /boot
├─vda3 252:3 0 100M 0 part /boot/efi
├─vda4 252:4 0 4M 0 part
└─vda5 252:5 0 3.9G 0 part /home
/
可以看到磁盘 /dev/sda
已经添加到虚拟机中了,类型为 scsi
, 如果要设置为 virtio
必须停止虚拟机。
重启虚拟加,可以看到磁盘依旧附加进来了:
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 09:41:07 2024 from 10.244.30.110
[fedora@fedora-vm ~]$ sudo reboot
[fedora@fedora-vm ~]$ wait: remote command exited without exit status or exit signal
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 09:45:13 2024 from 10.244.241.120
[fedora@fedora-vm ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 10G 0 disk
sr0 11:0 1 1M 0 rom
zram0 251:0 0 435M 0 disk [SWAP]
vda 252:0 0 5G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1000M 0 part /boot
├─vda3 252:3 0 100M 0 part /boot/efi
├─vda4 252:4 0 4M 0 part
└─vda5 252:5 0 3.9G 0 part /home
/
但是停止虚拟机,并重新启动(删除 VMI,重新构建 VMI)时,磁盘不会附加到虚拟机中:
vagrant@master01:~$ virtctl stop fedora-vm -n cdi-test
VM fedora-vm was scheduled to stop
vagrant@master01:~$ kubectl get vmi -n cdi-test
No resources found in cdi-test namespace.
vagrant@master01:~$ virtctl start fedora-vm -n cdi-test
VM fedora-vm was scheduled to start
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 09:51:42 2024 from 10.244.30.110
[fedora@fedora-vm ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1M 0 rom
zram0 251:0 0 435M 0 disk [SWAP]
vda 252:0 0 5G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1000M 0 part /boot
├─vda3 252:3 0 100M 0 part /boot/efi
├─vda4 252:4 0 4M 0 part
└─vda5 252:5 0 3.9G 0 part /home
/
如果要持久附加到 VM 中,需要使用 --persist
选项:
vagrant@master01:~$ virtctl -n cdi-test addvolume fedora-vm --volume-name=example-volume-hotplug --persist
Successfully submitted add volume request to VM fedora-vm for volume example-volume-hotplug
vagrant@master01:~$ kubectl describe vm fedora-vm -n cdi-test
Name: fedora-vm
Namespace: cdi-test
Labels: kubevirt.io/os=linux
Annotations: kubevirt.io/latest-observed-api-version: v1
kubevirt.io/storage-observed-api-version: v1
API Version: kubevirt.io/v1
Kind: VirtualMachine
Metadata:
Creation Timestamp: 2024-04-28T06:07:25Z
Finalizers:
kubevirt.io/virtualMachineControllerFinalize
Generation: 6
Resource Version: 311322
UID: 68914411-de36-4170-9ce6-8f5a2aca2389
Spec:
Running: true
Template:
Metadata:
Creation Timestamp: <nil>
Labels:
kubevirt.io/domain: fedora-vm1
Spec:
Architecture: amd64
Domain:
Cpu:
Cores: 1
Devices:
Disks:
Disk:
Bus: virtio
Name: disk0
Cdrom:
Bus: sata
Readonly: true
Name: cloudinitdisk
Disk:
Bus: scsi
Name: example-volume-hotplug
Serial: example-volume-hotplug
就可以看到在 VM 的定义中已经添加了磁盘,如果不添加的话,此处是没有的,意味着构建新的 VMI 时,磁盘不会附加到虚拟机里面。
更改为 virtio 模式
只需要修改 VM 中 disk 的 bus
值即可,修改为 virtio
:
vagrant@master01:~$ virtctl stop fedora-vm -n cdi-test
VM fedora-vm was scheduled to stop
vagrant@master01:~$ kubectl edit vm fedora-vm -n cdi-test
virtualmachine.kubevirt.io/fedora-vm edited
- disk:
bus: virtio
name: example-volume-hotplug
启动虚拟机进行验证:
vagrant@master01:~$ virtctl start fedora-vm -n cdi-test
VM fedora-vm was scheduled to start
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 10:05:27 2024 from 10.244.241.120
[fedora@fedora-vm ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1M 0 rom
zram0 251:0 0 435M 0 disk [SWAP]
vda 252:0 0 10G 0 disk
vdb 252:16 0 5G 0 disk
├─vdb1 252:17 0 1M 0 part
├─vdb2 252:18 0 1000M 0 part /boot
├─vdb3 252:19 0 100M 0 part /boot/efi
├─vdb4 252:20 0 4M 0 part
└─vdb5 252:21 0 3.9G 0 part /home
/
可以看到磁盘 vdb
了。
同样的,可以在线附加磁盘也可以在线移除磁盘,通过 virtctl removevolme
命令:
vagrant@master01:~$ virtctl removevolume fedora-vm --volume-name=example-volume-hotplug -n cdi-test
Successfully submitted remove volume request to VM fedora-vm for volume example-volume-hotplug
vagrant@master01:~$ virtctl ssh -i .ssh/id_rsa fedora@fedora-vm.cdi-test
Last login: Sun Apr 28 10:32:21 2024 from 10.244.30.110
[fedora@fedora-vm ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1M 0 rom
zram0 251:0 0 435M 0 disk [SWAP]
vdb 252:16 0 5G 0 disk
├─vdb1 252:17 0 1M 0 part
├─vdb2 252:18 0 1000M 0 part /boot
├─vdb3 252:19 0 100M 0 part /boot/efi
├─vdb4 252:20 0 4M 0 part
└─vdb5 252:21 0 3.9G 0 part /home
/
这个仅仅是在 VMI 的基础上移除了磁盘,在 VM 中还是有定义的。
如果要持久的移除,也提供了 --persist
选项:
vagrant@master01:~$ virtctl removevolume fedora-vm --volume-name=example-volume-hotplug --persist -n cdi-test
Successfully submitted remove volume request to VM fedora-vm for volume example-volume-hotplug
验证 VM 中已经没有相应的磁盘了:
vagrant@master01:~$ kubectl describe vm fedora-vm -n cdi-test | grep example-volume-hotplug
vagrant@master01:~$