Cloud Native 23 min read

KubeVirt Overview, Architecture, and Practical Usage with Code Examples

This article introduces KubeVirt as a Kubernetes plugin for managing virtual machines, compares it with OpenStack and other solutions, explains its CRDs, components, storage and networking options, and provides detailed code examples and SDK usage for deploying and operating VMs in a cloud‑native environment.

360 Smart Cloud
360 Smart Cloud
360 Smart Cloud
KubeVirt Overview, Architecture, and Practical Usage with Code Examples

KubeVirt is an open‑source Red Hat project that enables Kubernetes to schedule and manage traditional virtual machines (VMs) alongside containers by extending the cluster with custom resources (CRDs) and leveraging Kubernetes networking and storage.

Background : The author’s company originally used two separate scheduling platforms—OpenStack for bare‑metal and VMs, and Kubernetes for containers—leading to duplicated effort and resource waste. As workloads move to containers, a unified Kubernetes‑based solution for VMs, bare‑metal, and containers is desired.

Technical Selection : After evaluating projects such as KubeVirt, Virtlet, and Rancher/Harvester, KubeVirt was chosen for its active community and design. The article then outlines key comparison points:

KubeVirt vs. OpenStack/OVirt: KubeVirt operates as an operator within Kubernetes, reusing CNI/Csi, and does not provide full network/storage services like OpenStack.

KubeVirt vs. Kata: Kata runs VMs with container‑like speed but is not a full VM; KubeVirt manages real VMs using Kubernetes extensibility.

KubeVirt vs. Virtlet: Virtlet implements a VM as a CRI pod, offering limited functionality (~70%).

Why use Kubernetes for VMs instead of OpenStack for containers: Kubernetes is the future mainstream for containers, and using it to also manage VMs simplifies operations.

KubeVirt CRDs : The article lists essential CRDs such as VirtualMachineInstance , DataVolume , and PersistentVolumeClaim , explaining their roles in VM lifecycle management.

KubeVirt Components :

virt‑api

virt‑controller

virt‑handler

virt‑launcher

These components replace OpenStack services (nova, neutron, cinder) with Kubernetes‑native equivalents.

Common Operations (code snippets):

type DomainManager interface {
    //SyncVMI creates a VM
    SyncVMI(*v1.VirtualMachineInstance, bool, *cmdv1.VirtualMachineOptions) (*api.DomainSpec, error)
    //PauseVMI pauses a VM
    PauseVMI(*v1.VirtualMachineInstance) error
    //UnpauseVMI resumes a paused VM
    UnpauseVMI(*v1.VirtualMachineInstance) error
    //KillVMI terminates a VM
    KillVMI(*v1.VirtualMachineInstance) error
    //DeleteVMI deletes a VM
    DeleteVMI(*v1.VirtualMachineInstance) error
    //... other management methods ...
}

Examples of querying VMI instances:

# kubectl get vmi -o wide
NAME.test.foo.demo.com   8d   Running   192.168.10.30   10.10.67.244   True

Listing KubeVirt pods:

# kubectl -n kubevirt get pod
virt-api-68c958dd-6sx4n   1/1   Running   0   14d
virt-controller-647d666bd5-gsnzf   1/1   Running   1   14d
... (other components) ...

Storage Options :

cloudInitNoCloud/cloudInitConfigDrive – provides cloud‑init data via a ConfigMap.

DataVolume – automatically imports VM images from HTTP or PVC sources.

PersistentVolumeClaim – block or filesystem storage for VM disks, with notes on RAW format limitations.

Example DataVolume definition:

spec:
  pvc:
    accessModes:
    - ReadWriteMany
    volumeMode: Block
    resources:
      requests:
        storage: 55G
    storageClassName: csi-rbd-sc
  source:
    http:
      url: http://127.0.0.1:8081/CentOS7.4_AMD64_2.1

Ceph RBD is used as the backend storage, with block RWX volumes required for live migration.

Network : KubeVirt leverages Kube‑OVN (OVS/OVN) to provide VLAN‑based underlay networking, assigning fixed IPs to VMs via custom network CRDs.

Sample network CRD snippet:

spec:
  cidrBlock: 192.168.10.0/23
  gateway: 192.168.10.1
  provider: ovn
  underlayGateway: true
  vlan: ovn-vlan

KubeVirt SDK : Both Python and Go SDKs are available. The article shows how to create an API client in Python, call create_namespaced_virtual_machine , and handle optimistic concurrency by including metadata.resourceVersion when updating resources.

import kubevirt

def get_api_client(host):
    api_client = kubevirt.ApiClient(host=host, header_name="Content-Type", header_value="application/json")
    return api_client

api_client = get_api_client(host="http://127.0.0.1:8001")
api_instance = kubevirt.DefaultApi(api_client)

Additional SDK functions demonstrated include listing VMs/VMI, starting, stopping, restarting, renaming (with a custom newName parameter), and replacing VM specifications.

Ultron Platform Integration : The internal private‑cloud portal "Ultron" now supports creating KubeVirt VMs using the same workflow as OpenStack, providing a unified user experience.

Conclusion : KubeVirt offers a viable cloud‑native alternative to traditional IaaS solutions for private‑cloud environments, enabling unified scheduling of containers and VMs, though it may not yet replace OpenStack in public‑cloud scenarios.

pythonCloudNativeKubernetesGoVirtualizationCephOpenStackKubeVirt
360 Smart Cloud
Written by

360 Smart Cloud

Official service account of 360 Smart Cloud, dedicated to building a high-quality, secure, highly available, convenient, and stable one‑stop cloud service platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.