How to Integrate Legacy VMs into Container Pipelines on Kubernetes with KubeVirt
I recently received a request from my team asking if it was possible to integrate an actual Windows virtual machine (VM) into our Kubernetes pipeline, instead of just using a Windows Docker image. One of the possible solution called KubeVirt that allows for seamless integration of legacy VMs into Kubernetes clusters. In this article, I will share my findings and provide examples of how to configure and use KubeVirt to achieve this integration.
Legacy VMs, or traditional virtual machines that are not containerized, can pose challenges when trying to incorporate them into modern container pipelines on Kubernetes. However, with KubeVirt, a Kubernetes extension, it becomes possible to run and manage VMs alongside containers in the same Kubernetes cluster, leveraging the powerful orchestration capabilities of Kubernetes.
To get started, the first step is to install KubeVirt on your Kubernetes cluster. The installation process involves applying a set of manifests that define the necessary resources, such as Custom Resource Definitions (CRDs), Pods, and Services, for KubeVirt to run. Once KubeVirt is installed, you can create VM objects using the kubectl
command-line tool or through Kubernetes manifests.
The VM manifest in KubeVirt is similar to other Kubernetes manifests, but with additional specifications for defining virtual hardware resources, such as CPUs, memory, disks, and network interfaces, for the VM. Here’s an example of a simple VM manifest for a Windows VM:
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
name: car-windows-vm
spec:
running: false
template:
spec:
domain:
devices:
disks:
- name: car-windows-disk
disk:
bus: virtio
interfaces:
- name: car-windows-nic
masquerade: {}
bridge: {}
resources:
requests:
memory: 4Gi
limits:
memory: 4Gi
terminationGracePeriodSeconds: 0
volumes:
- name: car-windows-disk
persistentVolumeClaim:
claimName: car-windows-pvc
In this example, we define a VirtualMachine resource named car-windows-vm
with a desired amount of memory and a single virtual disk attached to a persistent volume claim (PVC) named car-windows-pvc
. The VM is set to not run initially (running: false
), allowing us to configure it before starting it.
Once the VM manifest is defined, we can create the VM object in Kubernetes using kubectl
:
kubectl create -f car-windows-vm.yaml
Next, we can use kubectl
to start the VM:
kubectl virt start car-windows-vm
This will trigger the creation of a Pod in the Kubernetes cluster to run the VM. Once the Pod is running, we can connect to the Windows VM using a remote desktop client or other appropriate tools.
To access the Windows VM using RDP, we need to know the IP address or hostname of the Windows VM. We can retrieve this information using kubectl
:
kubectl virt vmi vnc car-windows-vm
This will display the VNC address and port for the Windows VM, which can be used in an RDP client to establish a remote desktop session.
KubeVirt also provides features live migration, snapshotting, and cloning of VMs, allowing for easy management and operation of legacy VMs within the Kubernetes environment. This makes it possible to incorporate existing Windows VMs into a containerized pipeline on Kubernetes without having to repackage or reconfigure the legacy applications.
Live Migration: One of the powerful features of KubeVirt is the ability to perform live migration of VMs within the Kubernetes environment. Live migration allows for the seamless movement of a running VM from one host to another without disrupting its operation. This is particularly useful for scenarios where you need to perform hardware maintenance on a host or balance the workload across different hosts in a cluster.
KubeVirt leverages the live migration capabilities of QEMU, a widely used open-source emulator, to perform live migration of VMs. This allows for the migration of Windows VMs with minimal downtime, ensuring high availability and uninterrupted operation of legacy applications during host maintenance or other operational activities.
Snapshotting: KubeVirt provides snapshotting functionality, which allows you to create point-in-time copies of VMs for backup, disaster recovery, or testing purposes. Snapshots capture the complete state of a VM, including the memory, CPU, and disk contents, allowing you to restore the VM to that exact state later.
With KubeVirt, you can create and manage snapshots of VMs using standard Kubernetes APIs and commands. This makes it easy to integrate snapshotting into your existing backup and disaster recovery workflows or use it for testing and development purposes.
Cloning: KubeVirt also supports cloning of VMs, which allows you to create multiple copies of a VM with the same configuration and state as the original VM. Cloning is useful for scenarios where you need to quickly provision multiple instances of a VM with identical configurations, such as for scaling up an application or creating multiple testing environments.
KubeVirt allows you to create clones of VMs using Kubernetes APIs and commands, providing a consistent and unified way to manage cloned VMs along with other Kubernetes resources.
These advanced features of KubeVirt — live migration, snapshotting, and cloning — provide powerful management capabilities for legacy VMs within the Kubernetes environment. They enable efficient operations, high availability, and easy backup and recovery of VMs, making it easier to manage legacy applications alongside containerized applications in a Kubernetes pipeline.
Here’s an example of how you can perform live migration of a Windows VM using KubeVirt:
Live Migration
1. Create a VM manifest file, specifying the VM configuration, including the name, CPU, memory, disks, and networks.
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
name: car-windows-vm
spec:
domain:
resources:
requests:
memory: 4G
cpu: 2
volumes:
- name: rootdisk
persistentVolumeClaim:
claimName: car-windows-vm-disk
networks:
- name: default
model: virtio
2. Apply the VM manifest using kubectl
to create the VM on a Kubernetes cluster.
kubectl apply -f car-windows-vm.yaml
3. Start the VM by setting its running
status to true
.
kubectl patch vm car-windows-vm -p '{"spec":{"running":true}}' --type=merge
4. Once the VM is running, you can use the virsh
command-line tool inside the VM container to perform live migration to another host.
kubectl exec -it car-windows-vm -c compute-container -- virsh migrate --live qemu+tcp://new-host/system
This will migrate the running Windows VM to the new host while preserving its state and ensuring minimal downtime.
In addition to live migration, KubeVirt also provides commands and APIs for creating and managing snapshots and clones of VMs. Here’s an example of how you can create a snapshot of a Windows VM using KubeVirt
Snapshots
1. Create a snapshot of the VM using the kubectl
command.
kubectl snapshot create car-windows-vm-snapshot --vm car-windows-vm
2. The snapshot will capture the current state of the VM, including its memory, CPU, and disk contents, and store it as a snapshot resource in Kubernetes.
3. You can then use the snapshot resource to restore the VM to the captured state at any time in the future.
kubectl snapshot restore car-windows-vm-snapshot --vm car-windows-vm
This allows you to easily create and manage snapshots of your VMs for backup, disaster recovery, or testing purposes, using familiar Kubernetes commands and resources.
Similarly, you can also create clones of VMs using KubeVirt. Here’s an example of how you can create a clone of a Windows VM
Cloning
1. Create a VM manifest for the clone, specifying the VM configuration, including the name, CPU, memory, disks, and networks.
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineClone
metadata:
name: car-windows-vm-clone
spec:
source:
name: car-windows-vm
namespace: default
2. Apply the VM clone manifest using kubectl
to create the cloned VM on a Kubernetes cluster.
kubectl apply -f car-windows-vm-clone.yaml
3. The cloned VM will have the same configuration and state as the original VM, allowing you to quickly provision multiple instances of the same VM for testing, development, or scaling purposes.
In addition to the basic VM management features, KubeVirt also supports advanced features such as GPU passthrough, custom resource utilization, and custom virtual hardware configurations, providing flexibility and customization options for running legacy VMs on Kubernetes.
KubeVirt seamlessly integrates with other Kubernetes tools and technologies, allowing for a unified management experience. For example, you can use Kubernetes namespaces to isolate VMs, apply RBAC (Role-Based Access Control) to control access to VMs, and use Kubernetes network plugins for networking configurations. This makes it easy to leverage existing Kubernetes knowledge and expertise for managing legacy VMs alongside containerized applications.
Furthermore, KubeVirt supports common container image formats, such as Docker and OCI (Open Container Initiative) images, allowing you to use existing container images for provisioning VMs. This provides consistency and standardization in the pipeline, making it easier to manage both containerized applications and legacy VMs in a unified manner.
In conclusion, KubeVirt is a powerful solution that enables seamless integration of legacy VMs into container pipelines on Kubernetes. With its features for VM management, customization, and integration with Kubernetes ecosystem, it provides a flexible and efficient way to run and manage legacy VMs alongside containerized applications. By leveraging KubeVirt, organizations can modernize their pipelines, incorporate existing VM-based workloads into their Kubernetes environments, and benefit from the scalability, portability, and automation advantages of containerized deployments.
More Stories you maybe interested?
About the Author
Hi Medium Community, my name is Charles Lo and I’m currently a project manager and data manager at Luxoft. Luxoft is a place where we combine a unique blend of engineering excellence and deep industry expertise to serve clients globally, specializing in cross-industry including but not limited to automotive, financial services, travel and hospitality, healthcare, life sciences, media, and telecommunications. In addition, Luxoft is also a family member of DXC.
I’m passionate about technology and hold several certifications including Offensive Security Certified Professional, AWS Certified Solution Architect, Red Hat Certified Engineer, and PMP Project Management. I have years of experience working in the banking, automotive, and open-source industries and have gained a wealth of knowledge throughout my career.
As I continue on my Medium journey, I hope to share my experiences and help others grow in their respective fields. Whether it’s providing tips for project management, insights into data analytics, or sharing my passion for open-source technology, I look forward to contributing to the Medium community and helping others succeed.
Author Linkedin — https://www.linkedin.com/in/charlesarea/