KubeVirt - Part 6: Creating the VM and its uses
- The VM is just another Kubernetes object, with a virtio bus.
- Deploying the first VM, with fixed MAC addresses.
- Accessing the VNC console and performing the first reboot.
- Additional GUI with KubeVirt Manager.
Introduction
Now it's time to conclude this series of articles by running our OPNsense VM under Kubevirt.
For those who arrive here without having read the beginning of the tutorial, I invite you to start from the beginning via this link.
Recap of components in place
Element for storage
Previously, we created the necessary DataVolumes (DV) for the VM in the namespace dev-opnsenselantoweb-inf, specifically implemented for our needs.
Two data disks (DataVolume) have been mounted: dv-opnsenselantoweb-iso, which hosts the OPNsense installation ISO image, and dv-opnsenselantoweb-root, which will serve as the disk for the virtual machine and the operating system installation.
These disks were imported via Containerized Data Importer (CDI). This space is managed via a StorageClass sc-longhorn-nvme, which itself is managed by the distributed storage solution Longhorn.
Element for networking
In a dedicated article, we addressed network requirements by creating two NetworkAttachmentDefinition:
- nad-lan-default: Connecting the VM to the default LAN.
- nad-vlan-web: Connecting the VM to the WEB VLAN.
As a reminder, the goal is to be able to use OPNsense as a firewall between two network zones, so the VM needs two interfaces.
Final schema
We can summarize our VM's requirements as follows:
Click the image to enlarge.
Now all that remains is to create the Kubernetes object that will group everything together, namely a VirtualMachine object from the API branch kubevirt.io/v1.
VM deployment
YAML file
Here is the content of the YAML file 03-vm-opnsenselantoweb-inf.yml:
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: vm-opnsenselantoweb-inf
namespace: dev-opnsenselantoweb-inf
labels:
app: opnsense
spec:
running: false
template:
metadata:
labels:
kubevirt.io/domain: vm-opnsenselantoweb-inf
spec:
domain:
cpu:
cores: 2
resources:
requests:
memory: 2Gi
devices:
disks:
- name: cdrom-iso
bootOrder: 1
cdrom:
bus: sata
- name: harddrive
bootOrder: 2
disk:
bus: virtio
interfaces:
- name: lan
bridge: {}
- name: wan
bridge: {}
networks:
- name: lan
multus:
networkName: dev-opnsenselantoweb-inf/nad-lan-default
- name: wan
multus:
networkName: dev-opnsenselantoweb-inf/nad-vlan-web
volumes:
- name: cdrom-iso
dataVolume:
name: dv-opnsenselantoweb-iso
- name: harddrive
dataVolume:
name: dv-opnsenselantoweb-root
The VirtualMachine (VM) inherits all the classic logic of Kubernetes objects. This is one of the advantages of KubeVirt, since a VM becomes nothing more than a simple additional object in the cluster, which we can manipulate as we are used to doing with our containerized applications.
We can see the concept of a namespace, here dev-opnsenselantoweb-inf, and also the ability to integrate labels to help identify the VM.
In the spec section, we will find specific points related to the VM object, such as the number of CPUs and the amount of memory.
Then, we will define the disks, our two data volumes (DVs), which can then be referenced in the Volume section, so that they can be referred to in the section dedicated to the VM's disk:
- The datavolume dv-opnsenselantoweb-iso, referenced as cdrom-iso, which we will attach to the emulation of a SATA bus in read-only mode.
- The datavolume dv-opnsenselantoweb-root, referenced as harddrive, which we will attach to the emulation of a virtio bus. (Virtio is the industry standard for "paravirtualized" I/O (input/output). In the KVM and KubeVirt ecosystem, it allows a virtual machine (VM) to communicate with the host in an ultra-fast way, without the slowdown associated with traditional hardware emulation).
Note that it is possible to set the boot order.
Similarly, we will apply the network configuration by associating our NetworkAttachmentDefinition nad-lan-default with the virtual "lan" interface and our NetworkAttachmentDefinition nad-vlan-web with the virtual "wan" interface.
Since the NetworkAttachmentDefinitions are not in the same namespace as the VM, you must specify their full path in the network section.
Those of you who are paying close attention will have noticed that I have fixed the MAC addresses of the virtual interfaces.
Indeed, this is my way of ensuring that I keep identical MAC addresses if, at a later date, I move my VM to another node. However, this can quickly lead to problems when I start creating multiple VMs; therefore, it is important to choose a strict and logical naming convention to avoid the risk of duplicates.
Note that the MAC prefix 02:00:00 does not correspond to a manufacturer, but is specific to the virtual network and is used by other solutions, such as Proxmox or OpenStack.
All that remains is to run the VM using the standard command kubectl apply:
kubectl apply -f 03-vm-opnsenselantoweb-inf.yml
Click the image to enlarge.
You can check its status via:
kubectl get vm -n dev-opnsenselantoweb-inf
Click the image to enlarge.
Accessing the console
The problem now will be accessing the installation interface. Apparently, the VM has started. If the configuration is correct, it should have booted from the OPNsense ISO.
In a typical scenario, you should see the OPNsense startup or installation screen. However, in our case, there is no console, so we cannot see what is happening.
Fortunately, it is possible to access it via the good old VNC protocol.
First, we need to open a port on our worker node so that it can make the VNC stream accessible. You can choose any available port from the list; in this example, we will use port 5900.
Since we are using Rocky Linux 10, we will use the following commands:
sudo firewall-cmd --add-port=5900/tcp --permanent
sudo firewall-cmd --reload
Click the image to enlarge.
Be careful: this port needs to be opened on all worker nodes that may run the VM. In my lab, I currently only have one server acting as both a control plane and a worker node. However, in real-world scenarios, you must remember to handle all execution nodes.
Next, we will use our virtctl CLI, which was deployed in a previous article, to indicate that we want to redirect our VNC connection to the VM
virtctl vnc vm-opnsenselantoweb-inf \
-n dev-opnsenselantoweb-inf \
--proxy-only=true \
--address=0.0.0.0 \
--port=5900
Click the image to enlarge.
From there, you just need a VNC client to make a connection.
Personally, I use TigerVNC on Windows, but everyone is free to choose the client they prefer.
From my workstation, by configuring it to connect to the IP address of our server on port 5900, I can access the VM's console.
Click the image to enlarge.
Click the image to enlarge.
From there, I can control the installation and configure OPNsense.
Click the image to enlarge.
I will not detail this point specifically, but I will ask that the binaries be written to the root disk of the VM and that the correct virtual interfaces are associated with the OPNsense interfaces, while also specifying their IP addresses.
Click the image to enlarge.
Click the image to enlarge.
Click the image to enlarge.
Click the image to enlarge.
First reboot
Once the installation is complete, I can restart the VM.
But first, I will shut it down to modify its configuration and remove support for the emulated CD-ROM drive in order to force it to boot from the disk associated with the dv-opnsenselantoweb-root DV.
To do this, we also use the virtctl command line:
virtctl stop vm-opnsenselantoweb-inf -n dev-opnsenselantoweb-inf
I edit the YAML file, comment out what is no longer needed, and then reapply it
Click the image to enlarge.
Click the image to enlarge.
kubectl apply -f 03-vm-opnsenselantoweb-inf.yml
I start the VM:
virtctl start vm-opnsenselantoweb-inf -n dev-opnsenselantoweb-inf
Click the image to enlarge.
I could run the virtctl vnc vm-opnsenselantoweb-inf -n dev-opnsenselantoweb-inf --proxy-only=true --address=0.0.0.0 --port=5900 command again to regain access to the console.
But I will wait a few minutes instead. If everything works correctly, I should be able to connect to the OPNsense interface directly through my browser, specifying not the IP address of my node, but the IP address assigned to the LAN interface of my VM, which is connected to my NetworkAttachmentDefinition nad-lan-default.
Click the image to enlarge.
Click the image to enlarge.
That's done, my VM is operational. We can test different commands within OPENsense or via other machines on my various networks to ensure that both interfaces are up and reachable.
Additional GUI
Ideally, it would be interesting to have a GUI for KubeVirt to view the VMs directly from a web interface. To do this, you can use KubeVirt Manager.
There are other options available, but Kubevirt-Manager stands out as the simplest and most complete solution for this area.
Its deployment involves several K8S objects that I will adapt somewhat to my needs.
First, we will assign a dedicated namespace, inf-kubevirtmanager-lan.
Kubectl create ns inf-kubevirtmanager-lan
Click the image to enlarge.
Next, I would like to make the interface accessible through a login/password.
To do this, I define a file 01-middleware-kubevirtmanager-auth.yml that creates a Traefik middleware:
apiVersion: v1
kind: Secret
metadata:
name: sec-kubevirtmanager-auth
namespace: inf-kubevirtmanager-lan
type: Opaque
stringData:
users: bvivi57:xxxxxxxxxxxxxxxxxxxxxxxxx
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middelware-kubevirtmanager-auth
namespace: inf-kubevirtmanager-lan
spec:
basicAuth:
secret: sec-kubevirtmanager-auth
I will not go into detail on this point; I will repeat what I did to secure the GUI of Longhorn, which is to define a secret responsible for storing my login and password, and then pass this secret to the Traefik middleware.
Next, we will use a file 02-right-kubevirtmanager.yml that will cover the access rights of kubevirt-manager to KubeVirt:
apiVersion: v1
kind: Namespace
metadata:
name: inf-kubevirtmanager-lan
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-kubevirt-manager
namespace: inf-kubevirtmanager-lan
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: crb-kubevirt-manager
subjects:
- kind: ServiceAccount
name: sa-kubevirt-manager
namespace: inf-kubevirtmanager-lan
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
Indeed, the GUI must be able to access the Kubernetes API to read and manipulate Kubernetes objects.
Therefore, we use a service account sa-kubevirt-manager that has the necessary rights on the cluster via the cluster role cluster-admin.
For those who are not comfortable with the concept of permissions, especially Kubernetes RBAC, I invite you to read my article on the subject.
I could be criticized (justifiably) that the cluster-admin role is too important for the needs of kubevirt-manager. However, I took this shortcut by using this default role, which gives access to all namespaces and K8S objects.
Next, we proceed with the file 03-deployment-kubevirtmanager.yml, which is the standard Deployment:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dpl-kubevirt-manager
namespace: inf-kubevirtmanager-lan
spec:
replicas: 1
selector:
matchLabels:
app: kubevirt-manager
template:
metadata:
labels:
app: kubevirt-manager
spec:
serviceAccountName: sa-kubevirt-manager
containers:
- name: kubevirt-manager
image: kubevirtmanager/kubevirt-manager:latest
ports:
- containerPort: 8080
env:
- name: KUBERNETES_Auth_Type
value: "ServiceAccount"
Which goes hand in hand with the file 04-svc-kubevirtmanager.yml for creating the Service:
---
apiVersion: v1
kind: Service
metadata:
name: svc-kubevirt-manager
namespace: inf-kubevirtmanager-lan
spec:
selector:
app: kubevirt-manager
ports:
- port: 80
targetPort: 8080[
Finally, building on everything covered in the third part of this cookbook dedicated to Kubevirt, we conclude with the file 05-http-kubevirtmanager.yml for creating the HTTPRoute to expose the URL "kubevirt-manager.coolcorp.priv" externally, which I chose for accessing kubevirt-manager:
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: allow-traefik-to-manager
namespace: inf-kubevirtmanager-lan
spec:
from:
- group: gateway.networking.k8s.io
kind: Gateway
namespace: inf-traefik-lan
to:
- group: ""
kind: Service
name: svc-kubevirt-manager
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httproute-kubevirt-manager
namespace: inf-kubevirtmanager-lan
spec:
parentRefs:
- name: traefik-gateway-lan
namespace: inf-traefik-lan
sectionName: websecure
hostnames:
- kubevirt-manager.coolcorp.priv
rules:
- matches:
- path:
type: PathPrefix
value: /
filters:
# On active l'authentification située dans le namespace Traefik
- type: ExtensionRef
extensionRef:
group: traefik.io
kind: Middleware
name: middelware-kubevirtmanager-auth
backendRefs:
- name: svc-kubevirt-manager
port: 80
I won't go into detail about this file. All the necessary information is already covered in the dedicated article, as well as in the article on exposing the Longhorn GUI with the API Gateway and Traefik.
Simply note that I associate my Middleware, created just before, to protect my URL by requiring a login and password through Traefik.
Then, we apply everything using the following command (kubectl apply -f folder_name_containing_the_yaml_files) and then access the URL https://kubevirt-manager.coolcorp.priv.
Click the image to enlarge.
(Don't forget to label the namespace so that it is taken into account by our Gateway (see dedicated article)).
Click the image to enlarge.
We are presented with a login/password prompt, which we complete with the contents of the secret we saved, and then we can access the GUI to manage our VMs.
Click the image to enlarge.
Kubevirt-manager has all the features mentioned previously, such as starting and stopping the VM, as well as accessing the VM console, without relying on a VNC client. This access is done directly from a browser.
Click the image to enlarge.
Click the image to enlarge.
Conclusion
We have now reached the end of this series of tutorials designed to help you understand KubeVirt.
The associated ecosystem is constantly evolving and continues to grow.
Although today, KubeVirt is mainly used through packaged solutions like OpenShift, it is clear that with a little effort, we can build our own KubeVirt cluster from the basic components.
Whether you need multiple network interfaces, specific storage, or console access, everything can be addressed using the basic packages, and in several ways.
Thanks to its KVM base and Kubernetes integration, KubeVirt is certainly a solution for the future and a more than credible alternative to other virtualization solutions.
Now it's time to continue the adventure, with a real multi-node cluster and the evaluation of additional skills, such as moving VMs from one node to another, or automatic recovery in case of an incident.
This, I hope, will happen later this year, thanks to the setup of a new home lab, which will allow me to offer a more comprehensive cookbook.
In the meantime, feel free to experiment on your own and, if necessary, draw inspiration from what has been presented here so far.