Topics In Demand
Notification
New

No notification found.

KubeVirt to deploy and manage VMs on Kubernetes
KubeVirt to deploy and manage VMs on Kubernetes

April 15, 2025

9

0

Managing infrastructure with both containers and traditional Virtual Machines (VMs) can be complex. While Kubernetes has transformed container management, many organizations still rely on VMs for legacy applications or compliance. This often leads to disorganized management tools and added complexity for DevOps teams, often resulting in silos and difficulties in scaling infrastructure.

KubeVirt addresses these challenges that DevOps teams face. It helps manage VMs alongside containers in Kubernetes. This allows users to use the same tools and processes for both. In this blog, I will show you how to deploy and manage VMs on Kubernetes using KubeVirt. I will cover the installation steps, create VMs, set up persistent storage, and provide an example that connects traditional virtualization with modern container management.

Prerequisites

Before we begin, we must have:

  • A running Kubernetes cluster. We can set up a cluster using tools like minikube or any other Kubernetes provider
  • kubectl configured to manage our Kubernetes cluster.
  • Basic familiarity with Kubernetes concepts like pods, deployments, and services.

Steps to setup KubeVirt

First, we will select the latest version of KubeVirt and set the environment variable KUBEVIRT_LATEST_VERSION for future commands.


 

Install KubeVirt with latest version

This command sets up a "kubevirt" namespace, installs Custom Resource Definitions for KubeVirt, and configures an operator to start the KubeVirt installation when a configuration resource is detected.



Now Here We will be adding the Kubevirt Custom Resource (named kubevirt). This will prompt the KubeVirt operator to install the remaining components of KubeVirt.


 

NOTE: Working with minikube, direct access to local virtualization hardware might not be available. we may need to enable emulation mode.

kubectl -n kubevirt patch kubevirt kubevirt --type=merge --patch '{"spec":{"configuration":{"developerConfiguration":{"useEmulation":true}}}}'

Install the KubeVirt client, virtctl.

Until we complete the setup, we can download the virtctl command line client for KubeVirt.



To ensure the downloaded binary is executable, execute the following command:


Put the virtctl binary in a directory included in your shell's PATH


Wait for KubeVirt to finish deploying. We can check its deployment status by examining the phase of the Kubevirt Custom Resource (CR). Use the following command:



Once KubeVirt fully deploys, it will show:

To get more details and pods deployed we can use


Once the deployment completes, confirm all necessary Pods are running without issues.

Launch an instance

To create a virtual machine, install the VM manifest using the following command:

NAME_OF_VIRTUAL_MACHINE in above file is 'testvm'

Check the status of the virtual machine we just created:

Wait until it gets RUNNING

If the VM is currently in a stopped state. We can use virtctl to change its state to running:

If failed, we can see the detailed status using

We will see a message that the VM was scheduled to start.

When we create a VirtualMachine, it automatically generates a VirtualMachineInstance, similar to how a Deployment manages Pods. This chain of actions eventually creates a Pod. We can view all these entities using a single 'kubectl get' command.

Connect to VM

Once the virtual machine is running, we can connect to its console using virtctl:


 

To Exit press CTRL and "]" keys together

To stop and delete a virtual machine:

Stop the virtual machine using virtctl from outside the VM:



To delete the virtual machine

Persistent Storage for virtual machines:

For persistent storage in virtual machines, the KubeVirt project offers a solution through the Containerized Data Importer (CDI) add-on. CDI facilitates the import of virtual machine disk images into PersistentVolumeClaims (PVCs), providing a declarative approach to managing persistent storage.

Create a DataVolume:

CDI introduces a CustomResourceDefinition (CRD) called DataVolume (DV), which abstracts the creation and population of disk data onto PVCs in a Kubernetes cluster. Once a DV is created, CDI handles the process of copying a disk image from the specified source into a PVC. Various source types are supported, including external targets like images served over HTTP, internal sources like other PVCs, or container disks in a registry. Below is an example DataVolume that imports a CirrOS image into a PVC, enabling the underlying VM to have persistent storage across reboots.

Install CDI

Get the Latest Version



Install cdi




To trigger the deployment of CDI, create a CDI Custom Resource (CR). This will prompt the operator to deploy CDI.


Check the status and wait until it gets deployed

Once Deployed we are Ready to Work Further

Create a Data Volume from the required image:

ubuntu-dv.yaml



Get it Executed using the following command We can change the url and storage according to our requirement


To check the DV status:

Create an instance from the data volume

Relace HASHED_PASSWORD, SSH_PUB_KEY with its values in the file below

ubuntu-vm.yaml



Get it executed



We can get the created resources:



Once the status of VM turns to "RUNNING" and Ready is "True", we can use the command to access the instance.


If needs to connect through VNC

I have just explained this with an example for an Ubuntu VM. Similarly, you can apply the same process for other operating systems.

Conclusion

Using KubeVirt on Kubernetes to deploy different operating system (OS) images shows clear differences in setup times and resource needs. Lightweight options like CirrOS require fewer resources, while larger distributions such as CentOS and Ubuntu need more. Each OS image has its own benefits and considerations for deployment:

  • CirrOSA lightweight server image of 15MB, typically ready in approximately 50 seconds, making it ideal for quick provisioning and testing.
  • CentOS: A robust server image at 1014 MB, requiring approximately 8 minutes for deployment, suited for enterprise applications and services.
  • Ubuntu Server: With an image size of 613 MB, deployment completes in around 7 minutes, balancing performance and resource efficiency.
  • Ubuntu Server (Minimal Configuration)A lighter variant at 265 MB, ready in about 97 seconds, emphasizing rapid deployment for streamlined environments.

KubeVirt has transformed how we manage infrastructure. Integrating VMs into Kubernetes simplifies operations, speeds up deployments, and helps DevOps teams avoid switching tools. However, this requires in and out understanding of how to leverage KubeVirt. This is where Opcito's experts can guide your organization through hybrid infrastructure transitions with zero-downtime. Contact our KubeVirt experts to discuss tailored solutions.


 


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


© Copyright nasscom. All Rights Reserved.