Kubevirt 是 Redhat 开源一套以容器方式运行虚拟机的项目,通过 kubernetes 云原生来管理虚拟机生命周期。
KubeVirt是一个kubernetes插件,在调度容器之余也可以调度传统的虚拟机。它通过使用自定义资源(CRD)和其它 Kubernetes 功能来无缝扩展现有的集群,以提供一组可用于管理虚拟机的虚拟化的API。
kubevirt主要实现了下面几种资源,以实现对虚拟机的管理:
- VirtualMachineInstance(VMI):类似于kubernetes Pod,是管理虚拟机的最小资源。一个VirtualMachineInstance对象即表示一台正在运行的虚拟机实例,包含一个虚拟机所需要的各种配置。
- VirtualMachine(VM):为集群内的VirtualMachineInstance提供管理功能,例如开机、关机、重启虚拟机,确保虚拟机实例的启动状态,与虚拟机实例是1:1的关系,类似与spec.replica为1的StatefulSet。
- VirtualMachineInstanceReplicaSet:类似ReplicaSet,可以启动指定数量的VirtualMachineInstance,并且保证指定数量的VirtualMachineInstance运行,可以配置HPA。
Kubevirt的整体架构:
简化版:
虚拟机镜像(磁盘)是启动虚拟机必不可少的部分,KubeVirt 中提供多种方式的虚拟机磁盘,虚拟机镜像(磁盘)使用方式非常灵活。这里列出几种比较常用的:
- PersistentVolumeClaim : 使用 PVC 做为后端存储,适用于数据持久化,即在虚拟机重启或者重建后数据依旧存在。使用的 PV 类型可以是 block 和 filesystem,使用 filesystem 时,会使用 PVC 上的 /disk.img,格式为 RAW 格式的文件作为硬盘。block 模式时,使用 block volume 直接作为原始块设备提供给虚拟机。
- ephemeral : 基于后端存储在本地做一个写时复制(COW)镜像层,所有的写入都在本地存储的镜像中,VM 实例停止时写入层就被删除,后端存储上的镜像不变化。
- containerDisk : 基于 scratch 构建的一个 docker image,镜像中包含虚拟机启动所需要的虚拟机镜像,可以将该 docker image push 到 registry,使用时从 registry 拉取镜像,直接使用 containerDisk 作为 VMI 磁盘,数据是无法持久化的。
- hostDisk : 使用节点上的磁盘镜像,类似于
hostpath
,也可以在初始化时创建空的镜像。- dataVolume : 提供在虚拟机启动流程中自动将虚拟机磁盘导入 pvc 的功能,在不使用 DataVolume 的情况下,用户必须先准备带有磁盘映像的 pvc,然后再将其分配给 VM 或 VMI。dataVolume 拉取镜像的来源可以时 http,对象存储,另一块 PVC 等。
安装前提已经安装好kubernetes集群。如果需要安装kubernetes请参考:kubernetes单节点安装_闫利朋的博客-CSDN博客
# yum install -y qemu-kvm libvirt virt-install bridge-utils
查看节点是否支持kvm硬件辅助虚拟化:
# virt-host-validate qemu
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup 'memory' controller support : PASS
QEMU: Checking for cgroup 'memory' controller mount-point : PASS
QEMU: Checking for cgroup 'cpu' controller support : PASS
QEMU: Checking for cgroup 'cpu' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuacct' controller support : PASS
QEMU: Checking for cgroup 'cpuacct' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuset' controller support : PASS
QEMU: Checking for cgroup 'cpuset' controller mount-point : PASS
QEMU: Checking for cgroup 'devices' controller support : PASS
QEMU: Checking for cgroup 'devices' controller mount-point : PASS
QEMU: Checking for cgroup 'blkio' controller support : PASS
QEMU: Checking for cgroup 'blkio' controller mount-point : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : WARN (IOMMU appears to be disabled in kernel. Add intel_iommu=on to kernel cmdline arguments)修改物理内核参数,解决WARN问题:
# vim /boot/efi/EFI/centos/grub.cfg
#添加 intel_iommu=on
# reboot
方法二:
# vim /etc/default/grub
添加:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet intel_iommu=on"
# grub2-mkconfig -o /boot/grub2/grub.cfg
验证:
# dmesg | grep -e DMAR | grep -i iommu
[ 0.000000] DMAR: IOMMU enabled
# virt-host-validate qemu
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup 'memory' controller support : PASS
QEMU: Checking for cgroup 'memory' controller mount-point : PASS
QEMU: Checking for cgroup 'cpu' controller support : PASS
QEMU: Checking for cgroup 'cpu' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuacct' controller support : PASS
QEMU: Checking for cgroup 'cpuacct' controller mount-point : PASS
QEMU: Checking for cgroup 'cpuset' controller support : PASS
QEMU: Checking for cgroup 'cpuset' controller mount-point : PASS
QEMU: Checking for cgroup 'devices' controller support : PASS
QEMU: Checking for cgroup 'devices' controller mount-point : PASS
QEMU: Checking for cgroup 'blkio' controller support : PASS
QEMU: Checking for cgroup 'blkio' controller mount-point : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : PASS
如果硬件虚拟化不可用或者想用虚拟机来安装kubevirt,则可以通过在kubeVirt CR中设置启动:
spec.configuration.developerConfiguration.useEmulation
true
# kubectl edit -n kubevirt kubevirt kubevirt
添加如下内容:
spec: ...
configuration:
developerConfiguration:
useEmulation: true
yaml下载地址:kubevirt-v0.52.0版本yaml-kubernetes文档类资源-CSDN下载
# kubectl apply -f kubevirt-operator.yaml
# kubectl apply -f kubevirt-cr.yaml
安装最新版本:
# export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)
# kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-operator.yaml
# kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-cr.yaml
查看部署结果:
# kubectl get pod -n kubevirt
NAME READY STATUS RESTARTS AGE
virt-api-6677fb87c6-cqmsh 1/1 Running 0 15m
virt-api-6677fb87c6-hsh9k 1/1 Running 0 15m
virt-controller-5dd87c95c4-5zg76 1/1 Running 0 14m
virt-controller-5dd87c95c4-dwh2z 1/1 Running 0 14m
virt-handler-7x4wc 1/1 Running 0 14m
virt-operator-85b8997768-f74zp 1/1 Running 0 88m
virt-operator-85b8997768-nw7jr 1/1 Running 0 88m
CDI(Containerized Data Importer):用于使用PVC作为kubevirt VM磁盘功能。
yaml文件下载:CDI-v1.48.0的yaml文件-kubernetes文档类资源-CSDN下载
# kubectl apply -f cdi-operator.yaml
# kubectl apply -f cdi-cr.yaml
查看部署结果:
# kubectl get pod -n cdi
NAME READY STATUS RESTARTS AGE
cdi-apiserver-5dc575cc5b-tw5r5 1/1 Running 0 4m35s
cdi-deployment-596b69f4bd-hhdd5 1/1 Running 0 4m28s
cdi-operator-59fd74c5df-nlqpj 1/1 Running 0 6m1s
cdi-uploadproxy-6b46f965f6-5vk9l 1/1 Running 0 4m22s安装最新版本:
# export VERSION=$(curl -s https://github.com/kubevirt/containerized-data-importer/releases | grep -o "v[0-9]\.[0-9]*\.[0-9]*"| head -1)
# kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator.yaml
# kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-cr.yaml
virtctl是kubevirt一个命令行工具
# export VERSION=$(curl -s https://api.github.com/repos/kubevirt/kubevirt/releases | grep tag_name | grep -v -- '-rc' | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs)
# curl -L -o /usr/local/bin/virtctl https://github.com/kubevirt/kubevirt/releases/download/$VERSION/virtctl-$VERSION-linux-amd64
# chmod +x /usr/local/bin/virtctl
安装vnc,为后续安装操作做准备:
# yum install -y tigervnc