DEV Community

Cover image for Run VMs alongside Containers in a Kubernetes Custer with KubeVirt
Sayed Naweed Rizvi
Sayed Naweed Rizvi

Posted on

Run VMs alongside Containers in a Kubernetes Custer with KubeVirt

As containers continue to dominate modern application deployment models, many organizations still rely heavily on virtual machines (VMs) for legacy workloads, specialized environments, or complex system-level applications. Instead of choosing between the two, what if we could run both—VMs and containers—side by side within a single Kubernetes cluster?

That's exactly what KubeVirt enables.

What Is KubeVirt?

KubeVirt is an open-source project that extends Kubernetes to manage not only containers but also virtual machines. It treats VMs as first-class citizens in the Kubernetes ecosystem by abstracting VMs into custom resources and scheduling them using Kubernetes-native workflows.

Simply put: KubeVirt lets you run VMs like containers, but without sacrificing the advantages of virtualization.


Why Would You Run VMs in Kubernetes?

You might ask: “Isn’t Kubernetes meant for cloud-native, containerized apps?” Yes. But many enterprises face these challenges:

  • Legacy workloads that can’t be containerized easily.
  • Specialized software requiring full OS-level control.
  • Gradual migration where some components are containerized, and others are not.
  • Hybrid cloud strategies with mixed workloads.
  • Dev/Test environments needing VM support alongside microservices.

How Does KubeVirt Work?

KubeVirt introduces a new CRD (Custom Resource Definition) called VirtualMachineInstance (VMI). When you define a VM using a YAML manifest, KubeVirt:

  1. Uses libvirt and QEMU to create the VM.
  2. Schedules it on a Kubernetes node using the usual scheduler.
  3. Runs the VM inside a container (usually inside a pod with special privileges).
  4. Exposes networking and storage through standard Kubernetes mechanisms (e.g., Services, PVCs).

Example: Spinning Up a VM Using KubeVirt

Here's a simple manifest to launch a VM inside Kubernetes:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: ubuntu-vm
spec:
  running: true
  template:
    metadata:
      labels:
        kubevirt.io/domain: ubuntu-vm
    spec:
      domain:
        devices:
          disks:
          - disk:
              bus: virtio
            name: containerdisk
        resources:
          requests:
            memory: 1Gi
      volumes:
      - name: containerdisk
        containerDisk:
          image: kubevirt/ubuntu-cloud-container-disk-demo
Enter fullscreen mode Exit fullscreen mode

This creates a lightweight Ubuntu VM managed like any Kubernetes workload.


Benefits of Using KubeVirt

  1. Unified Platform: Manage both VMs and containers using the same tooling, policies, and CI/CD pipelines.
  2. Reduced Operational Overhead: No need to maintain separate systems like OpenStack or VMware for VMs.
  3. Kubernetes-native APIs: Developers interact with VMs just like any Kubernetes object (via kubectl).
  4. Elasticity: Leverage Kubernetes autoscaling and scheduling for VMs too.
  5. Enhanced Security and Multi-tenancy: Apply Kubernetes-native RBAC, NetworkPolicies, and isolation models to VMs.
  6. Dev/Test Use Cases: Great for developers needing full VMs for OS-level testing or legacy software.

Real-World Use Cases

Use Case Description
Legacy Modernization Slowly migrate VMs to containers in the same platform.
Secure Sandboxing Run apps in full OS environments for added isolation.
Dev/Test Labs Launch VMs on demand for testing operating systems, agents, etc.
Mixed Workloads Run microservices and monoliths side-by-side.
Edge Computing Use lightweight Kubernetes nodes to host both VMs and containers at the edge.

Challenges & Considerations

  • Performance Overhead: VMs don’t have the same lightweight performance as containers.
  • Security Boundaries: Running VMs inside pods introduces complexity in isolation.
  • Persistent Storage: Proper planning required for VM disks and stateful workloads.
  • Networking: Integrating VMs with container networking (CNI plugins) requires some setup.

Hey, if you have read thus far.
I work in the Cloud & DevOps space, often exploring the intersections of emerging tools and infrastructure patterns. If you're into cloud-native tech, platform engineering, or DevOps transformations—feel free to follow me here for more hands-on insights and real-world discussions.

Top comments (0)