KubeVirt: Part 2 - Deploying KubeVirt

In short
  • This article is outdated and has been replaced by the KubeVirt V2 series.
  • Installing Cilium.
  • Deploying KubeVirt and its components on a single-node cluster.
  • VM creation is covered in part 3.

Introduction

In my previous article, I provided an introduction to Kubevirt, describing its components and explaining how it works.

Throughout this article, I will provide the prerequisites for setting up a Kubernetes node capable of running VMs using KubeVirt.

Therefore, I won't reiterate the fundamentals here, and I strongly encourage you to read the first article before continuing with this one.

Now is the time to begin implementing the target detailed in this diagram.

Reminder of the target architecture

Click the image to enlarge.

If you have followed the steps described in the previous article, you should know that at this stage, we have a single Kubernetes node, serving as both master and worker, on which a CNI (Container Network Interface) has not yet been deployed.

Once again, it is important that you are familiar with the concept of CNI before proceeding further. You can learn more by visiting this page.

Installing Cilium

The CNI being used is the same as for my main cluster; it is Cilium.

It will be responsible for handling basic connectivity for pods and VMs.

As I did in the article dedicated to using a GPU under Kubernetes, where I installed Cilium on a single-node architecture, I will use Helm with this configuration file (cilium-values.yaml):

  
kubeProxyReplacement: true
k8sServiceHost: 192.168.10.160
k8sServicePort: 6443
ipam:
  mode: "cluster-pool"
  operator:
    clusterPoolIPv4PodCIDRList:
      - "10.13.0.0/16"
hubble:
  relay:
    enabled: true
  ui:
    enabled: true
    frontend:
      server:
        ipv6:
          enabled: false
  tls:
    auto:
      enabled: true
      method: helm
      certValidityDuration: 1095
operator:
 replicas: 1

Therefore, Cilium must be deployed on my node associated with the API server responding to IP address 192.168.10.160 (the IP address of my prdk8sctp001 server) on port 6443.

A subnet dedicated to pods managed by Cilium's network is defined as 10.13.0.0/16. Of course, this range must be reserved for the cluster and not used by another external network.

We will utilize all of Cilium's features because we are also replacing the basic Kubernetes component, kube-proxy.

Kube-proxy usually handles the distribution of traffic between pods and services. You can rely on it or completely replace it with certain Container Network Interfaces (CNIs) like Cilium. This is not mandatory, but it allows for better performance and superior filtering capabilities thanks to Cilium's native eBPF (Extended Berkeley Packet Filter) integration.

eBPF is a technology integrated into the Linux kernel that allows the execution of custom code directly within the kernel. This makes it possible to provide packet filtering, load balancing, and traffic monitoring functions in a much more optimized and efficient way.

To avoid using kube-proxy, you must not have deployed it previously... That's why in my cookbook on K8S, when I use kubeadm to initialize my cluster, I pass the following argument: --skip-phases=addon/kube-proxy.

If it is active on your cluster, you will need to remove it beforehand, usually with the following command:

kubectl delete daemonset kube-proxy -n kube-system

In my case, this is not necessary because my cluster was initialized without kube-proxy. We can proceed directly to the deployment of Cilium.

We will use the package manager Helm. I mention it in part 3 of my cookbook dedicated to Kubernetes. It is a simple binary that uses your access to the cluster to deploy applications through a repository and packaging logic, simplifying their installation and updates.

Here is the command:

helm install --version 1.17.1 --namespace=kube-system cilium cilium/cilium --values=./cilium-values.yaml --set hubble.relay.tolerations[0].key="node-role.kubernetes.io/control-plane" --set hubble.relay.tolerations[0].operator="Exists" --set hubble.relay.tolerations[0].effect="NoSchedule" --set hubble.relay.tolerations[1].key="node-role.kubernetes.io/master" --set hubble.relay.tolerations[1].operator="Exists" --set hubble.relay.tolerations[1].effect="NoSchedule" --set hubble.ui.tolerations[0].key="node-role.kubernetes.io/control-plane" --set hubble.ui.tolerations[0].operator="Exists" --set hubble.ui.tolerations[0].effect="NoSchedule" --set hubble.ui.tolerations[1].key="node-role.kubernetes.io/master" --set hubble.ui.tolerations[1].operator="Exists" --set hubble.ui.tolerations[1].effect="NoSchedule" --set cni.exclusive=false

(The version of Cilium should be adjusted as new releases are released.)

Installation of Cilium

Click the image to enlarge.

It may seem very long, but this is mainly due to the use of a single node on which the components of a control plane are located. By default, some elements of Cilium cannot be deployed on a master node.

In the previous article, I introduced the concept of taint associated with a control plane to allow applications to run on the node. (This should never be done in production!)

Despite this, I include the necessary tolerations in my command so that Cilium can execute all of its components, even if they only have one node.

If you are using a real cluster with multiple nodes, you can stick to the following command:

helm install --version 1.17.1 --namespace=kube-system cilium cilium/cilium --values=./cilium-values.yaml --set cni.exclusive=false

Even in this simplified version, I would like to draw your attention to the parameter --set cni.exclusive=false

It is very important because, within the context of KubeVirt, we will need to deploy other Container Network Interfaces (CNIs). Without this option, Cilium will automatically overwrite any third-party configuration from another CNI, effectively taking exclusive control of network management for the cluster.

At this stage, you can verify the deployment of Cilium:

kubectl get pod -n kube-system
Verification of the Cilium installation

Click the image to enlarge.

The node is now in the "ready" state.

kubectl get node
Node ready

Click the image to enlarge.

Installing KubeVirt

It's time to proceed with the installation of KubeVirt.

To do this, we will follow the official guide, starting by setting the target version to be deployed as a variable.

export RELEASE=$(curl https://storage.googleapis.com/kubevirt-prow/release/kubevirt/kubevirt/stable.txt)
Definition of the version variable

Click the image to enlarge.

At the time of writing this article, it is version 1.4.

Verification of the variable

Click the image to enlarge.

We then use this variable to retrieve and execute the necessary YAML files directly from the project's repository.

kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-operator.yaml kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-cr.yaml
Deployment of KubeVirt

Click the image to enlarge.

After a few minutes, you can check the status of the KubeVirt components with the following command:

kubectl get all -n kubevirt
Verification of the component installation

Click the image to enlarge.

To summarize what has been deployed, we have the following table:

Component Role
KubeVirt Operator Manages the installation and updates of KubeVirt
Virt-API Provides the REST API for managing VMs
Virt-Controller Manages the creation, scheduling, and migration of VMs
Virt-Handler Runs on each node to interact with QEMU/KVM
Virt-Launcher Container that runs the VM via QEMU/KVM in a pod

Note that we have not attempted to customize the namespace hosting the resources. We are using the default namespace provided with the installation: kubevirt.

Installing the CLI

You may be getting used to this if you have already followed some of my articles, such as the one on Velero, or if you are already familiar with the K8S ecosystem. However, you will need to retrieve an additional binary in order to take advantage of all the features of KubeVirt.

This is called virtctl and can be downloaded directly from its repository.

It is important to use a version that is identical to the deployed KubeVirt version and compatible with the client operating system used to manage your cluster.

Retrieval of virtctl

Click the image to enlarge.

virtctl typically uses your kubectl access configuration file. Feel free to review my article on the principles of k8S administration if you would like more information on this topic.

Retrieve the desired binary and place it in your operating system's execution path so that you can call it from any location.

For example, under Linux:

  
wget https://github.com/kubevirt/kubevirt/releases/download/v1.4.0/virtctl-v1.4.0-linux-amd64
mv virtctl-v1.4.0-linux-amd64 virtctl
chmod +x virtctl
sudo mv virtctl /usr/local/bin/

You can then test everything with the following command:

virtctl version
Verification of the installation with virtcl

Click the image to enlarge.

Deploying network addons

As explained in the previous article, it will be necessary to deploy other CNIs to offer more network services to pods and VMs.

  • Multus: to allow multiple network interfaces.
  • Macvtap: to allow mapping a physical network interface directly within a VM.

Each of these CNI tools can be installed individually and configured separately.

This is not what we will focus on, because the KubeVirt project offers a complementary operator that can be deployed on the cluster, simplifying the installation of these additional CNI tools.

The repository for this project is this one.

Installing the operator involves the following three commands:

 
  kubectl apply -f https://github.com/kubevirt/cluster-network-addons-operator/releases/download/v0.98.1/namespace.yaml
  kubectl apply -f https://github.com/kubevirt/cluster-network-addons-operator/releases/download/v0.98.1/network-addons-config.crd.yaml
  kubectl apply -f https://github.com/kubevirt/cluster-network-addons-operator/releases/download/v0.98.1/operator.yaml
  

After these actions, we have a namespace cluster-network-addons and access to a new K8S object called NetworkAddonsConfig, declared in the branch networkaddonsoperator.network.kubevirt.io/v1.

We will use this new object to create the file 01-networkaddonconfig-macvtap-multus.yml, which contains the following:

 
---
apiVersion: networkaddonsoperator.network.kubevirt.io/v1
kind: NetworkAddonsConfig
metadata:
  name: cluster
spec:
  imagePullPolicy: IfNotPresent
  macvtap: {}
  multus: {}

By applying it with the command kubectl apply -f 01-networkaddonconfig-macvtap-multus.yml, we will automatically deploy macvtap and multus. With this method, you can add as many network plugins as supported by the operator (the list is visible in the GitHub repository).

Application of the add-on deployment

Click the image to enlarge.

We will only use the two that we need. In fact, if we list the pods in the cluster-network-addons namespace, we can see elements specific to our two CNI tools.

kubectl get pod -n cluster-network-addons
Listing of the addons

Click the image to enlarge.

If you want to ensure that you have new CNI tools installed, you can also list the contents of the directory /etc/cni/net.d/.

You should find a structure like this:

Verification of the CNI configuration

Click the image to enlarge.

Note that there is no file associated with macvtap, but this is normal because it does not use any files at this level, and only Cilium and Multus are present.

macvtap configuration

Specifically, macvtap requires a specific configuration that we need to provide in the form of a configmap (cm).

This configmap (macvtap-deviceplugin-config) exists by default in the namespace cluster-network-addons. It is created by the operator when macvtap is deployed.

The problem is that this configmap allows macvtap to use any of the network cards present on the server (you can view its contents with the command kubectl get cm macvtap-deviceplugin-config -n cluster-network-addons -o yaml).

Contents of the default macvtap ConfigMap

Click the image to enlarge.

This is not what we want. If you refer to the target architecture diagram at the beginning of this article, we only want to use:

  • enp8s0: the base interface for LAN access
  • enp8s0.6: an interface tagged with VLAN6 for access to the VLAN_WEB zone
  • enp13s0f3: the base interface for accessing the DMZ

The associated ConfigMap should therefore be as follows (content of the 02-cm-macvtap-default.yml file):

 
kind: ConfigMap
apiVersion: v1
metadata:
  name: macvtap-deviceplugin-config
  namespace: cluster-network-addons
  labels:
    app.kubernetes.io/component: network
    app.kubernetes.io/managed-by: cnao-operator
immutable: true
data:
  DP_MACVTAP_CONF: |
    [
      {
        "name": "enp8s0",
        "lowerDevice": "enp8s0",
        "mode": "bridge",
        "capacity": 30
      },
      {
        "name": "enp8s0.6",
        "lowerDevice": "enp8s0.6",
        "mode": "bridge",
        "capacity": 30
      },
      {
        "name": "enp13s0f3",
        "lowerDevice": "enp13s0f3",
        "mode": "bridge",
        "capacity": 30
      }
    ]
  

Each section corresponds to an interface, with the mode to use, bridge, and the number of virtual sub-interfaces allowed, which is 30 in my example.

The problem is that this ConfigMap is constantly overwritten by the operator's default configuration. If you modify it, it will only be applied for a few seconds before reverting to its original values.

I haven't been able to find a way to change this behavior through the operator, so I allowed myself a small workaround by setting the immutable: true option in my configuration.

With this parameter in the YAML file, I prevent K8S from modifying this ConfigMap. Therefore, all that remains is to delete the default ConfigMap and then recreate it with my configuration.

kubectl delete cm macvtap-deviceplugin-config -n cluster-network-addons kubectl apply -f 02-cm-macvtap-default.yml

Because it is immutable, the operator will no longer be able to modify it itself. This isn't ideal, but it's a trick that works.

Furthermore, if we redeploy the macvtap CNI using the command:

kubectl rollout restart daemonset/macvtap-cni -n cluster-network-addons
Redeployment of the configmap

Click the image to enlarge.

And then query the logs of the associated pod:

kubectl logs macvtap-cni-bj5d6 -n cluster-network-addons

We can see that the configuration is loaded correctly, as we only see our interfaces when the controller attached to the CNI starts.

Verification of the macvtap controller logs

Click the image to enlarge.

Patching KubeVirt

Before proceeding further, there are still a few adjustments to make...

As I explained in the previous article, the KubeVirt ecosystem is not yet completely stable, especially regarding network usage and the operation of certain add-ons.

This is the case with macvtap. If we really want to map physical interfaces within our VMs using this CNI, we will need to lift some restrictions on the deployment of KubeVirt.

We are going to patch the installation of KubeVirt by enabling features that are currently considered alpha.

This involves activating feature gates.

Here are the commands to enter:

kubectl patch kubevirt -n kubevirt kubevirt --type=json -p='[{"op": "add", "path": "/spec/configuration/developerConfiguration/featureGates", "value": ["NetworkBindingPlugins"]}]' kubectl patch kubevirt -n kubevirt kubevirt --type=json -p='[{"op": "add", "path": "/spec/configuration/developerConfiguration/featureGates/-", "value": "NetworkBindingPlugins"}]'
Activation of feature gates

Click the image to enlarge.

 
kubectl patch kubevirts -n kubevirt kubevirt --type=json -p='[{
  "op": "add",
  "path": "/spec/configuration/developerConfiguration/featureGates/-",
  "value": "NetworkBindingPlugins"
}]'
 
kubectl patch kubevirts -n kubevirt kubevirt --type=json -p='[{"op": "add", "path": "/spec/configuration/network",   "value": {
            "binding": {
                "macvtap": {
                    "domainAttachmentType": "tap"
                }
            }
        }}]'
      

That's a lot, but it's what's needed for KubeVirt to restart with support for card binding.

It is very likely that this part of the tutorial will evolve over time. By the time you read these lines, these operations may no longer be necessary.

I invite you to follow this link to stay informed about the evolution of macvtap support and interface binding by KubeVirt.

Containerized-Data-Importer (CDI)

KubeVirt can rely on some existing Kubernetes objects to provide services to VMs. This is the case for storage. KubeVirt uses the concepts of pv (persistent volume) and pvc (persistent volume claim) to provide disks or other data sources to VMs.

To simplify this integration, it is recommended to install the Containerized-Data-Importer (CDI) add-on. This is not mandatory, but CDI allows you to generate PVCs directly from VM images, existing containerized volumes, or ISO files.

We will use these capabilities later to deploy our VMs.

The installation process involves the following commands:

  • We set the version to download using a variable
export TAG=$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest) export VERSION=$(echo ${TAG##*/})
Choice of version

Click the image to enlarge.

  • Then, we install the operator and the new resources specific to CDI:
kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator.yaml kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-cr.yaml
Installation of the operator

Click the image to enlarge.

You can verify that the deployment was successful with the following command:

kubectl get all -n cdi
Verification of the installation

Click the image to enlarge.

We can see that a specific namespace called cdi is being used.

Later, we will use Containerized-Data-Importer to copy an ISO image within a PVC. To do this, CDI exposes an upload proxy service that needs to be made accessible from outside the cluster.

We need to create an ingress. In this case, I will use the Traefik ingress controller that is deployed on the Kubernetes node. I won't go into specific details about this topic; you can find explanations here.

Before creating the ingress, I need to generate a certificate. I won't go into detail here; it simply involves creating a private key and generating a Certificate Signing Request (CSR) to obtain a certificate from a Public Key Infrastructure (PKI).

For the purpose of this tutorial, I am generating a key and a Certificate Signing Request (CSR) for the URL cdi.coolcorp.priv:

openssl req -new -nodes -sha256 -keyout cdi.coolcorp.priv.key -out cdi.coolcorp.priv.csr -newkey rsa:4096 -subj "/C=FR/ST=Ile-de-France/L=Paris/O=COOLCORP/OU=Infrastructure/CN=cdi.coolcorp.priv" -reqexts SAN -config <(printf "[req]\ndistinguished_name = req_distinguished_name\n[req_distinguished_name]\n[SAN]\nsubjectAltName=DNS:cdi.coolcorp.priv")
Generation of the private key and CSR

Click the image to enlarge.

I present the Certificate Signing Request (CSR) to my internal Public Key Infrastructure (PKI). Since I am using a Windows PKI, I use the certreq executable and my custom template specific to the website.

certreq -attrib "CertificateTemplate:TPL-SRV-WEB-DEFAULT" -submit .\cdi.coolcorp.priv.csr
Generation of the certificate

Click the image to enlarge.

Once the certificate and key are retrieved in the same folder, it is sufficient to create the secret that stores the entire set:

kubectl create secret tls sec-cdi-cert --cert=cdi.coolcorp.priv.cer --key=cdi.coolcorp.priv.key -n cdi
Generation of the secret

Click the image to enlarge.

We list the services deployed by the CDI installation to identify the one we need to associate with our ingress.

kubectl get svc -n cdi
Identification of the service

Click the image to enlarge.

This refers to the cdi-uploadproxy service on port 443.

Here is the ingress declared in a file named 01-ing-cdi-proxy.yml:

 
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ing-cdi-proxy
  namespace: cdi
  labels:
    environment: prd
    network: lan
    application: cdi
    tier: proxy
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
    ingressClassName: traefik-lan
    traefik.ingress.kubernetes.io/router.tls: "true"
spec:
  ingressClassName: traefik-lan
  tls:
  - hosts:
    - cdi.coolcorp.priv
    secretName: sec-cdi-cert
  rules:
  - host: cdi.coolcorp.priv
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: cdi-uploadproxy
            port:
              number: 443
  

The secret sec-cdi-cert for the certificate is present, and the URL cdi.coolcorp.priv correctly points to the cdi-uploadproxy service.

Now, we just need to ensure that there is a DNS entry that correctly corresponds to Traefik, and therefore to the Kubernetes node.

Verification of DNS

Click the image to enlarge.

We can apply the kubectl apply -f .\01-ing-cdi-proxy.yml file

Creation of the ingress

Click the image to enlarge.

Finally, we need to create a storage class (sc).

This is because we will subsequently need to handle ISO files, particularly for OS installation. The goal is to use CDI to provision PVCs (and therefore volumes) from a given ISO file as input.

We are going to dedicate an SC for the automatic creation of these PVCs. If you are not familiar with SCs, PVs, and PVCs, I invite you to take a look here and here.

Here is the SC described in a YAML file 04-sc-local-storage-nfs-iso.yml:

 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: sc-local-storage-nfs-iso
provisioner: nfs.csi.k8s.io
parameters:
  # Le serveur NFS
  server: 192.168.10.152
  # L'export contenant la racine des dossiers ISO
  share: /Volume1/nfsshare/tools/pv-iso/
reclaimPolicy: Retain
volumeBindingMode: Immediate

As you can see, we will be using the CSI (Container Storage Interface) NFS and an NFS export on my NAS dedicated to hosting these PVCs containing the ISOs.

Hosting of the ISOs

Click the image to enlarge.

We apply the SC:

kubectl apply -f 04-sc-local-storage-nfs-iso.yml
Application of the SC

Click the image to enlarge.

We can verify that it is correctly declared:

kubectl get sc
Choice of version

Click the image to enlarge.

Conclusion

This concludes this second part dedicated to KubeVirt. After the prerequisites covered in the previous article, we have now deployed KubeVirt and all the necessary add-ons for our initial goal.

Now, it is time to leverage all these components to finally deploy the VMs: the third article is available here!