跳转至

1 启用新特性

启用新特性

k8s有一种开关机制叫 feature-gates 翻译成中文就是特性开关或"特性门控"。 这种机制用于控制和管理新功能(或不成熟的功能)的启用与禁用,因为k8s是一个在持续不断演进的项目,会不断引入新特性,而某些新特性不够成熟,不适用于在生产环境中默认启用, 但集群管理员可以通过feature-gates来控制这些特性是否启用。

同样,kubevirt 也使用了这种机制来控制是否启用一些新特性。

如何知道有哪些新特性

如何知道kubevirt当前有哪些新特性,可从源码 feature-gates.go 中定义的常量中查看到。

package virtconfig
...
const (
    // k8s支持扩展正在使用的存储,比如在storage Class 中开启 allowVolumeExpansion=true。
    ExpandDisksGate       = "ExpandDisks"
    // 用于KubeVirt识别哪些节点运行了CPU管理器,并自动添加cpumanager=true标签,当KubeVirt识别到CPU管理器不再在该节点上运行时,此标签将被自动删除。
    CPUManager            = "CPUManager"
    // 将主机NUMA拓扑映射到虚拟机拓扑,以提高性能。
    NUMAFeatureGate       = "NUMA"
    // 集成支持 coreOS Ignition 配置工具。
    IgnitionGate          = "ExperimentalIgnitionSupport"
    // 启用严格主机检查,针对Windows虚拟机的优化,确保主机没有已知的Hyper-V支持问题,开启后可能影响兼容性问题。
    HypervStrictCheckGate = "HypervStrictCheck"
    // 支持运行sidecar容器,在CPU专用场景,Kubevirt通常使用sidecar容器来挂载VMI的注册表磁盘。sidecar容器会占用200m的CPU资源,64MB的内存。
    SidecarGate           = "Sidecar"
    // 支持GPU设备。
    GPUGate               = "GPU"
    // 支持主机透传设备。
    HostDevicesGate       = "HostDevices"
    // 支持虚拟机快照。
    SnapshotGate          = "Snapshot"
    // 支持虚拟机导出。
    VMExportGate          = "VMExport"
    // 支持虚拟机热插拔卷(磁盘)。
    HotplugVolumesGate    = "HotplugVolumes"
    // 支持在某个节点上创建或使用磁盘镜像的能力,工作原理类似于k8s的hostpath,使用场景:复用本地高性能磁盘。
    HostDiskGate          = "HostDisk"
    // 支持Virtiofs,即支持多个虚拟机共享目录
    VirtIOFSGate          = "ExperimentalVirtiofsSupport"
    // 向下指标功能允许将虚拟机(VM)及其宿主机上的一组有限度量数据暴露给来宾操作系统,即允许虚拟机访问宿主机和自身的部分性能指标
    DownwardMetricsFeatureGate = "DownwardMetrics"
    // 虚拟机实时迁移或创建主机磁盘镜像文件场景,是否以Root身份运行
    Root                       = "Root"
    // 支持Kubevirt的性能分析功能。
    ClusterProfiler            = "ClusterProfiler"
    // 支持SEV(安全加密虚拟化)。
    WorkloadEncryptionSEV      = "WorkloadEncryptionSEV"
    // DockerSELinuxMCSWorkaround sets the SELinux level of all the non-compute virt-launcher containers to "s0".
    DockerSELinuxMCSWorkaround = "DockerSELinuxMCSWorkaround"
    // virtio-vsock 技术,一种高效的宿主机与虚拟机之间通信机制
    VSOCKGate                  = "VSOCK"
    // DisableCustomSELinuxPolicy disables the installation of the custom SELinux policy for virt-launcher,禁止为virt-launcher容器安装自定义SELinux策略,这样做的目的可能是因为某些环境不需要额外的安全限制,或者存在与特定 SELinux 策略不兼容的情况。
    DisableCustomSELinuxPolicy = "DisableCustomSELinuxPolicy"
    // KubevirtSeccompProfile indicate that Kubevirt will install its custom profile and
    // user can tell Kubevirt to use it,seccomp 是 Linux 内核的一项安全功能,它可以限制容器进程可以使用的系统调用,从而增强容器的安全性。
    KubevirtSeccompProfile = "KubevirtSeccompProfile"
    // DisableMediatedDevicesHandling disables the handling of mediated
    // devices, its creation and deletion,禁用该特性,可能影响vGPU创建。
    DisableMediatedDevicesHandling = "DisableMDEVConfiguration"
    // HotplugNetworkIfacesGate enables the virtio network interface hotplug feature,支持网卡热插拔
    HotplugNetworkIfacesGate = "HotplugNICs"
    // PersistentReservation enables the use of the SCSI persistent reservation with the pr-helper daemon,允许使用SCSI持久保留(persistent reservation)功能,配合pr-helper守护进程来实现持久化的存储锁机制。
    PersistentReservation = "PersistentReservation"
    // VMPersistentState enables persisting backend state files of VMs, such as the contents of the vTPM
    VMPersistentState = "VMPersistentState"
    // 支持多种架构,例如可以在不同的CPU架构上部署和运行虚拟机实例,例如同时支持 x86_64 和 ARM 架构的虚拟机。
    Multiarchitecture = "MultiArchitecture"
    // VMLiveUpdateFeaturesGate allows updating ceratin VM fields, such as CPU sockets to enable hot-plug functionality. 也就是支持CPU热插拔
    VMLiveUpdateFeaturesGate = "VMLiveUpdateFeatures"
    // When BochsDisplayForEFIGuests is enabled, EFI guests will be started with Bochs display instead of VGA
    BochsDisplayForEFIGuests = "BochsDisplayForEFIGuests"
    // NetworkBindingPlugingsGate enables using a plugin to bind the pod and the VM network,支持使用插件来绑定Pod和虚拟机的网络配置
    NetworkBindingPlugingsGate = "NetworkBindingPlugins"
    // AutoResourceLimitsGate enables automatic setting of vmi limits if there is a ResourceQuota with limits associated with the vmi namespace.
    AutoResourceLimitsGate = "AutoResourceLimitsGate"

    // Owner: @lyarwood
    // Alpha: v1.1.0
    //
    // CommonInstancetypesDeploymentGate enables the deployment of common-instancetypes by virt-operator,启用该特性,会自动部署一组预定义的、常用的虚拟机实例类型和偏好设置,可以通过kubectl get vmcf|vmcp查看
    CommonInstancetypesDeploymentGate = "CommonInstancetypesDeploymentGate"
    // AlignCPUsGate allows emulator thread to assign two extra CPUs if needed to complete even parity.某些情况下,虚拟化环境中的CPU对齐可以提高性能和兼容性,
    // 特别是对于需要偶数核心才能正常运行的应用程序或操作系统。当启用AlignCPUs特性时,KubeVirt会根据需要调整分配给虚拟机的CPU资源,确保满足特定的对齐要求。
    AlignCPUsGate = "AlignCPUs"
)
注意 实时迁移LiveMigration特性在kubevirt v0.56之后默认已被启用,即LiveMigration特性已经成熟,不需要再放在featureGates中了;但不是所有从feature-gates中移除的特性都成熟稳定了,有些特性确实是废弃了。

如何启用新特性

通过在kubevirt CR中设置featureGates来启用新特性,例如:

# kubectl edit kubevirt -n kubevirt
apiVersion: kubevirt.io/v1
kind: KubeVirt
metadata:
  name: kubevirt
  namespace: kubevirt
spec:
  configuration:
    developerConfiguration: 
      featureGates:
        - ExpandDisks
        - CPUManager
        - Snapshot
        ...

# kubectl apply -f enable-feature-gate.yaml
注意 featureGates中的特性名称是区分大小写的,例如CPUManager不能写成cpumanager

禁用某个特性

通过在kubevirt CR中的configuration.developerConfiguration.featureGates中移除某个特性来禁用某个特性即可。