Deploy your VM in Kubernetes Cluster using Kubevirt

Solution for integration between Microservices & Virtual Machine | Installation Kubevirt

Btech Engineering
btech-engineering
4 min readApr 7, 2023

--

https://kubevirt.io/

Kubevirt is an open-source virtualization platform that allows users to deploy virtual machines (VMs) in a Kubernetes cluster. It provides a powerful solution for integrating microservices and virtual machines, enabling users to take advantage of the benefits of both technologies. In this article, we will provide an overview of Kubevirt and guide you through the process of deploying a VM in a Kubernetes cluster using Kubevirt.

Overview

Kubevirt extends the Kubernetes API to provide a platform for managing virtual machines alongside containers. It enables users to deploy, run, and manage VMs as Kubernetes objects, leveraging Kubernetes’ advanced scheduling, networking, and storage capabilities. Kubevirt supports a range of hypervisors, including KVM, QEMU, and VirtualBox, and provides features such as live migration, snapshots, and storage management.

Kubevirt Components

Kubevirt consists of several components that work together to provide a platform for managing VMs in a Kubernetes cluster. These components include:

  1. API Server: The Kubevirt API server extends the Kubernetes API to support VMs as a first-class object. It provides a RESTful API for creating, updating, and deleting VMs and their associated resources.
  2. Virtual Machine Controller: The Virtual Machine Controller (VMC) is responsible for creating and managing VMs. It interacts with the Kubernetes API server to create VMs and their associated resources, such as virtual disks, networks, and interfaces.
  3. Virtual Machine Instance Controller: The Virtual Machine Instance Controller (VMIC) is responsible for managing the lifecycle of VMs. It interacts with the VMC to create and start VMs, and monitors their status to ensure they are running correctly.
  4. CDI: The Containerized Data Importer (CDI) is a Kubernetes-native tool for managing the import and export of data between VMs and containers. It enables users to import data into VMs from container images and export data from VMs to container images.
  5. Virt-Handler: Virt-Handler is a lightweight, secure runtime for running VMs. It runs as a privileged container in the Kubernetes cluster and provides a secure execution environment for VMs.

Installation

Lab Environment

  • Kubernetes Cluster 1 Master 2 Node, 1 Nfs Storage
  • Each Node 8 CPU, 16GB Ram
  • Kubernetes Version 1.26.3

Step-by-Step

Deploy Kubevirt

KubeVirt can be installed using the KubeVirt operator, which manages the lifecycle of all the KubeVirt core components.

  • Use kubectlto deploy the KubeVirt operator:
export VERSION=$(curl -s <https://api.github.com/repos/kubevirt/kubevirt/releases> | grep tag_name | grep -v -- '-rc' | sort -r | head -1 | awk -F': ' '{print $2}' | sed 's/,//' | xargs) echo $VERSION kubectl create -f <https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-operator.yaml>
  • Again use kubectl to deploy the KubeVirt custom resource definitions:
kubectl create -f <https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/kubevirt-cr.yaml>

Verify Components

  • Check the deployment:
kubectl get kubevirt.kubevirt.io/kubevirt -n kubevirt -o=jsonpath="{.status.phase}"  

...
Deployed
  • Check the components:
kubectl get all -n kubevirt
...
Warning: kubevirt.io/v1 VirtualMachineInstancePresets is now deprecated and will be removed in v2.
NAME READY STATUS RESTARTS AGE
pod/virt-api-9db4877f9-ts4f7 1/1 Running 0 8m24s
pod/virt-api-9db4877f9-v4krt 1/1 Running 0 8m24s
pod/virt-controller-6b69b5b887-29gqs 1/1 Running 0 7m59s
pod/virt-controller-6b69b5b887-kvjkw 1/1 Running 0 7m59s
pod/virt-handler-28rrj 1/1 Running 0 7m59s
pod/virt-handler-c487h 1/1 Running 0 7m59s
pod/virt-operator-7848c6bbc7-8zwxp 1/1 Running 0 9m15s
pod/virt-operator-7848c6bbc7-dxn5r 1/1 Running 0 9m15s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubevirt-operator-webhook ClusterIP 10.100.18.217 <none> 443/TCP 8m27s
service/kubevirt-prometheus-metrics ClusterIP None <none> 443/TCP 8m27s
service/virt-api ClusterIP 10.105.255.140 <none> 443/TCP 8m27s
service/virt-exportproxy ClusterIP 10.107.196.14 <none> 443/TCP 8m27s

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/virt-handler 2 2 2 2 2 kubernetes.io/os=linux 7m59s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/virt-api 2/2 2 2 8m24s
deployment.apps/virt-controller 2/2 2 2 7m59s
deployment.apps/virt-operator 2/2 2 2 9m15s

NAME DESIRED CURRENT READY AGE
replicaset.apps/virt-api-9db4877f9 2 2 2 8m24s
replicaset.apps/virt-controller-6b69b5b887 2 2 2 7m59s
replicaset.apps/virt-operator-7848c6bbc7 2 2 2 9m15s

NAME AGE PHASE
kubevirt.kubevirt.io/kubevirt 8m53s Deployed

Virtctl

KubeVirt provides an additional binary called virtctl for quick access to the serial and graphical ports of a VM and also handle start/stop operations.

  • virtctlcan be retrieved from the release page of the KubeVirt github page.
VERSION=$(kubectl get kubevirt.kubevirt.io/kubevirt -n kubevirt -o=jsonpath="{.status.observedKubeVirtVersion}")
ARCH=$(uname -s | tr A-Z a-z)-$(uname -m | sed 's/x86_64/amd64/') || windows-amd64.exe
echo ${ARCH}
curl -L -o virtctl https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/virtctl-${VERSION}-${ARCH}
chmod +x virtctl
sudo install virtctl /usr/local/bin

Check Version

~# virtctl version
Client Version: version.Info{GitVersion:"v0.59.0-alpha.2", GitCommit:"6f97adc2fd3144cb9813363e3bb147c39c2260af", GitTreeState:"clean", BuildDate:"2023-01-03T16:07:04Z", GoVersion:"go1.19.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{GitVersion:"v0.59.0-alpha.2", GitCommit:"6f97adc2fd3144cb9813363e3bb147c39c2260af", GitTreeState:"clean", BuildDate:"2023-01-03T16:07:04Z", GoVersion:"go1.19.2", Compiler:"gc", Platform:"linux/amd64"}

Conclusion

Kubevirt is a powerful solution for integrating microservices and virtual machines, enabling users to take advantage of the benefits of both technologies. By deploying VMs in a Kubernetes cluster, users can leverage Kubernetes’ advanced features for managing containers and build hybrid applications that combine microservices and traditional applications. Kubevirt also simplifies the management of VMs, providing a single interface for managing both VMs and containers. With its advanced features and integration capabilities, Kubevirt is an essential tool for users looking to build and manage complex workloads that include both microservices and virtual machines.

By Fauzan Rafi, Research Team Btech

--

--

Btech Engineering
btech-engineering

Our mission is continuous learning and remember together is better.