Deploy your VM in Kubernetes Cluster using Kubevirt

Solution for integration between Microservices & Virtual Machine | Deploy VM on top Kubernetes

Btech Engineering
btech-engineering
3 min readApr 14, 2023

--

https://kubevirt.io/

Intro

Kubevirt provides a seamless solution for integrating microservices with virtual machines in a Kubernetes cluster. To get started, install Kubevirt onto your cluster using the Kubevirt Operator, and then use the Kubevirt API to define and create virtual machines. With Kubevirt, you can manage virtual machines just like any other Kubernetes resource, using tools like kubectl to monitor and modify them.

In addition to Kubernetes features such as RBAC and network policies, Kubevirt also provides a user-friendly web-based interface for managing virtual machines. This allows you to easily create and configure VMs, as well as monitor their performance and resource usage. By deploying your VMs in Kubernetes with Kubevirt, you can enjoy the scalability and reliability of containerized microservices, while also leveraging the versatility and power of virtual machines for your application infrastructure.

Purpose

This scenario walks through the creation of a Virtual Machine Instance (VMI) on Kubernetes and then how virtctl is used to interact with its console. This tutorial based on Kubevirt Documentation.

Practical

Pre-requisites

  • Kubernetes Cluster
  • Kubevirt already installed on Kubernetes Cluster

Installation

Create VM

nano vm.yaml
...
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
name: testvm
spec:
running: false
template:
metadata:
labels:
kubevirt.io/size: small
kubevirt.io/domain: ubuntu-bionic
spec:
domain:
cpu:
cores: 1
devices:
disks:
- name: containervolume
disk:
bus: virtio
- name: cloudinitvolume
disk:
bus: virtio
interfaces:
- name: default
masquerade: {}
resources:
requests:
memory: 2048M
networks:
- name: default
pod: {}
volumes:
- name: containervolume
containerDisk:
image: tedezed/ubuntu-container-disk:20.0
- name: cloudinitvolume
cloudInitNoCloud:
userData: |-
#cloud-config
chpasswd:
list: |
ubuntu:ubuntu
root:toor
expire: False

Apply the manifest to Kubernetes

kubectl create ns vm
kubectl apply -f vm.yaml -n vm
...output...
virtualmachine.kubevirt.io "testvm" created
virtualmachineinstancepreset.kubevirt.io "small" created

Manage Virtual Machines

To get a list of existing Virtual Machines

kubectl get vms -n vm
...output...
NAME AGE STATUS READY
testvm 117s Stopped False
............
kubectl get vms -o yaml testvm -n vm

To start a Virtual Machine you can use:

virtctl start testvm -n vm
...output...
VM testvm was scheduled to start
............

kubectl get vms -n vm
...output...
NAME AGE STATUS READY
testvm 4m27s Running True

After start we can check status VM

kubectl get vmis -n vm
NAME AGE PHASE IP NODENAME READY
testvm 2m6s Running 10.10.101.140 kubevirt-worker1 True

Controlling the State of the VM

To shut it down:

virtctl stop testvm -n vm

To delete a Virtual Machine:

kubectl delete vm testvm -n vm

Conclusion

In conclusion, Kubevirt is a powerful tool for deploying virtual machines in a Kubernetes cluster. By integrating microservices and virtual machines in this way, you can take advantage of the scalability and flexibility of containerized applications, while also leveraging the power and versatility of virtualization. With Kubevirt, you can manage virtual machines just like any other Kubernetes resource, using familiar tools and interfaces such as kubectl and web-based dashboards.

In addition, Kubevirt offers a range of features to help you get the most out of your virtual machines, including support for various operating systems, virtualized hardware devices, and high-performance networking. Whether you’re running a complex enterprise application or a simple development environment, Kubevirt can provide a seamless and integrated solution for all your virtualization needs. So, if you’re looking to deploy virtual machines in Kubernetes, be sure to check out Kubevirt and see how it can help you streamline your infrastructure and improve your application performance.

--

--

Btech Engineering
btech-engineering

Our mission is continuous learning and remember together is better.