Kubevirt - Part 2: Cluster initialization and KubeVirt installation

In short
  • Initialization of a vanilla K8S cluster without kube-proxy.
  • Deployment of Cilium followed by Multus.
  • Installation of KubeVirt.
  • Deployment of NetworkAttachmentDefinitions, with homogeneous bridges on all nodes.

Introduction

After reviewing the prerequisites and network configurations in the previous article, it's time to move on to initializing the Kubernetes cluster and installing KubeVirt.

As I always do for my K8S usage, I deploy it using a so-called vanilla installation. This means that I don't use a specific Kubernetes distribution, but I rely on the official installation tool: kubeadm.

This allows me to stay as close as possible to the Kubernetes versions released by the developers and to have as few dependencies as possible on a specific distribution.

The disadvantage is that it's a less integrated solution, which sometimes requires more configuration and attention, although I find that vanilla K8S installations are becoming increasingly simple with each version.

There's still the issue of updates, but even there, it ultimately becomes relatively straightforward as long as you adopt a structured approach, treating each component individually and carefully checking compatibility matrices with each new version.

Cluster initialization

The cluster consists of a single node for the purposes of this tutorial. But whether you use one or multiple servers, the initialization remains the same.

Connect to the server that will act as the first node and the first control plane (here, my physical server previously deployed, prdk8sctp001).

We rely on the configuration file kubeadm.conf which, to reiterate, contains the following:

  
---
# PARTIE 1 : Configuration du noeud initial (Master)
apiVersion: kubeadm.k8s.io/v1beta4
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: "192.168.10.160"
  bindPort: 6443
nodeRegistration:
  criSocket: "unix:///var/run/containerd/containerd.sock"
  kubeletExtraArgs:
  - name: "node-ip"
    value: "192.168.10.160"

---
# PARTIE 2 : Configuration du Cluster
apiVersion: kubeadm.k8s.io/v1beta4
kind: ClusterConfiguration
kubernetesVersion: "1.34.2"
clusterName: "rubikub"
controlPlaneEndpoint: "rubikub.coolcorp.priv:6443"
networking:
  dnsDomain: cluster.local
  podSubnet: "10.11.0.0/16"
  serviceSubnet: "10.12.0.0/16"
apiServer:
  certSANs:
  - "192.168.10.160"
  - "rubikub.coolcorp.priv"
  - "127.0.0.1"
  - "localhost"
scheduler: {}
controllerManager: {}

---
# PARTIE 3 : Configuration du Kubelet
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
failSwapOn: true
authentication:
  anonymous:
    enabled: false
    

To run the command kubeadm (installed via the packages deployed in the previous article), connect to the root profile and execute the command kubeadm init --config kubeadm.conf --skip-phases=addon/kube-proxy

Usage of kubeadm

Click the image to enlarge.

The configuration file is passed as an argument, and we specify that we do not want to deploy the "kube-proxy" component; it will be replaced by the Cilium CNI driver later.

The operation takes several minutes, but should end with instructions for adding control planes or workers.

Output of the kubeadm command

Click the image to enlarge.

Here, I simply retrieve the configuration file generated during cluster initialization. This file contains the authentication details for accessing the K8S API as an administrator.

I copy this file to my user profile so that it can be automatically accessed by the kubectl CLI.

cp -i /etc/kubernetes/admin.conf /home/bvivi57/.kube/config
chown -R bvivi57:bvivi57 /home/bvivi57/.kube/config

Feel free to browse my K8S cookbook for more information on cluster initialization. You will find other examples with the addition of extra nodes.

Once all commands have been executed, you can switch back to a standard session profile and test the status of the node using the command kubectl get node

The API should respond positively, but the node is not yet in the ready state. This is normal at this stage.

Node status at the end of the operation

Click the image to enlarge.

Cilium deployment

To make the node operational, it is necessary to deploy a CNI (Container Network Interface) driver. In this case, I have chosen Cilium.

I will not detail what a CNI is, but you can find related information in my dedicated article in my K8S cookbook.

Cilium is currently the preferred driver for network management under Kubernetes. It ensures all internal connectivity within the cluster.

Before deploying it, it is necessary to remove a default restriction on our node by removing a "taint".

This involves removing the prohibition of running non-specific assets on a control plane. To do this, run the following command:

kubectl taint nodes --all node-role.kubernetes.io/control-plane-
Removing the taint from the control plane

Click the image to enlarge.

This operation is only necessary because we are installing a single node on the cluster that acts as both a control plane and a worker.

You should never remove this restriction on a multi-node production cluster. Control planes are normally not used to execute any assets and should be reserved for managing the cluster.

In my tutorial, this is a prerequisite if I don't want to be blocked from running certain containers that might refuse to start on a control plane.

I can always reinstate this restriction later when I add more nodes, clearly separating the control plane servers and the workers.

As a reminder, the Cilium configuration prepared previously is associated with this file cilium-values.yaml

  
kubeProxyReplacement: true
k8sServiceHost: 192.168.10.160
k8sServicePort: 6443
cni:
  # Empêche Cilium de prendre le contrôle total du dossier /etc/cni/net.d
  # Cela permet à Multus de rester le "maître" et d'appeler Cilium comme plugin délégué
  exclusive: false
ipam:
  mode: "cluster-pool"
  operator:
    clusterPoolIPv4PodCIDRList:
      - "10.13.0.0/16"
# Configuration Hubble (Visualisation)
hubble:
  relay:
    enabled: true
  ui:
    enabled: true
    frontend:
      server:
        ipv6:
          enabled: false
  tls:
    auto:
      enabled: true
      method: helm
      certValidityDuration: 1095

cgroup:
  autoMount:
    enabled: false
   

The important point concerns the option:

exclusive: false

This is necessary to allow the use of another CNI driver, multus, which we will need later.

To deploy Cilium, I will use Helm, assuming that the associated binary has been copied to the server during the previous step.

Similarly, if this doesn't make sense to you, feel free to consult my K8S cookbook.

Currently, Helm is a package manager for Kubernetes that simplifies the deployment of applications made available on specific repositories.

We start by adding the repository specific to the Cilium package, ensuring that we have an up-to-date version on the node:

helm repo add cilium https://helm.cilium.io/
helm repo update
Adding the Cilium Helm repository

Click the image to enlarge.

Then, we launch the deployment of Cilium with Helm and the install attribute, passing our configuration file as an argument:

helm install --version 1.18.4 --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"
Installing Cilium

Click the image to enlarge.

Note that we are adding many additional attributes that are normally unnecessary, but in my case, as with removing the "taint", it is to ensure that all containers related to Cilium can run on my single node.

Furthermore, for the same reason, if you want to prevent a pod from remaining in an error state, you must then execute this command:

kubectl scale deployment cilium-operator -n kube-system --replicas=1
Reducing the number of Cilium controllers

Click the image to enlarge.

This reduces the number of replicas of the cilium-operator component to 1, since by default, the installation attempts to deploy at least 2 on different nodes for resilience reasons.

You will also need to remember to restore the value to 2 once several nodes are in place.

If everything goes well, we should have the Cilium pods operational in the namespace kube-system:

kubectl get pod -n kube-system
Checking the Cilium pods

Click the image to enlarge.

And the node should now be in a ready state:

kubectl get node
Checking the node

Click the image to enlarge.

Multus deployment

By default, it is only possible to assign a single network interface to a K8S object.

To remove this restriction, in addition to Cilium, you need to add the Multus CNI. Multus relies on Cilium to provide connectivity, but allows multiple interfaces to be mounted within a Kubernetes resource.

Multus will therefore allow us to have 2 network cards on the target OPNsense VM.

There are several ways to implement Multus, but we will focus on the version as a daemon, or "thick" mode, because this is the recommended method currently.

To do this, we execute the YAML file available online directly:

kubectl apply -f https://raw.githubusercontent.com/k8snetworkplumbingwg/multus-cni/master/deployments/multus-daemonset-thick.yml
Installing Multus

Click the image to enlarge.

However, by default, the resources allocated to Multus pods are low. There is a risk that once they are used when creating our VMs, they may end up being automatically restarted by K8S due to memory limits being reached.

We will now boost the pods by allowing higher CPU and memory consumption limits:

kubectl set resources daemonset kube-multus-ds -n kube-system --containers=kube-multus --limits=memory=512Mi,cpu=500m --requests=memory=128Mi,cpu=100m

At this stage, the Multus resources should be up in the kube-system namespace:

kubectl get pod -n kube-system
Checking Multus

Click the image to enlarge.

KubeVirt deployment

Now that the Kubernetes base is operational, we can deploy KubeVirt.

We start by exporting the version of the latest stable release as a variable:

export RELEASE=$(curl -L https://storage.googleapis.com/kubevirt-prow/release/kubevirt/kubevirt/stable.txt)

Currently, at the time of this article, it is 1.7.0

Then, using this variable, we retrieve all components online via these two commands:

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
Installing KubeVirt

Click the image to enlarge.

You will need to wait several minutes, as there are many things to deploy.

At the end of the process, it is essential to verify that all active pods are located in the kubevirt namespace. It is strongly recommended to avoid using a different namespace, as this one is created automatically during the installation of kubevirt.

Checking the KubeVirt installation

Click the image to enlarge.

All that remains is to retrieve the virtcl binary, which, like kubectl for K8S, allows you to interact with the components specific to kubevirt, starting with the VMs.

To do this, we retrieve the same version as the one we just deployed for kubevirt:

wget https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/virtctl-${RELEASE}-linux-amd64
sudo install virtctl-${RELEASE}-linux-amd64 /usr/local/bin/virtctl
Retrieving the virtctl binary

Click the image to enlarge.

We check the command call:

virtctl version
Calling virtctl

Click the image to enlarge.

Deployment of NetworkAttachmentDefinition

It remains to deploy the NetworkAttachmentDefinitions

These are K8S objects that will allow us to attach the bridge network interfaces created in the first part to a network space that can be used by Multus to attach virtual interfaces mounted by Cilium.

In simple terms, we could say that these are the virtual networks declared within KubeVirt that we will be able to use to map the network to our VMs.

As we saw in the initial architecture diagram, we will use three different networks:

  • The LAN: default network
  • The VLAN_WEB: network for hosting VMs exposed externally
  • The LAN DMZ: network directly connected to my external router.

Therefore, we need to create three NetworkAttachmentDefinitions, one per network zone.

We will find them in this YAML file:

  
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
  name: nad-lan-default
  namespace: inf-kubevirt
spec:
  config: '{
      "cniVersion": "0.3.1",
      "name": "nad-lan-default",
      "type": "bridge",
      "bridge": "br-lan",
      "ipam": {}
    }'
---
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
  name: nad-vlan-web
  namespace: inf-kubevirt
spec:
  config: '{
      "cniVersion": "0.3.1",
      "name": "nad-vlan-web",
      "type": "bridge",
      "bridge": "br-web",
      "ipam": {}
    }'
---
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
  name: nad-dmz-first
  namespace: inf-kubevirt
spec:
  config: '{
      "cniVersion": "0.3.1",
      "name": "nad-dmz-first",
      "type": "bridge",
      "bridge": "br-dmz",
      "ipam": {}
      }'      
  

Each section corresponds to a network. We reference the bridge interface to which it is attached.

It is obvious that, in the case of a multi-node cluster, the names of the bridge interfaces created previously must exist with the same naming convention on all nodes.

Before executing the file, we create a dedicated namespace called 'inf-kubevirt'. This will be used to store all common configurations specific to KubeVirt without affecting the default 'kubevirt' namespace.

kubectl create namespace inf-kubevirt
Creating the 'inf-kubevirt' namespace

Click the image to enlarge.

Then, we apply the NetworkAttachmentDefinition file:

kubectl apply -f 01-nad-all.yml
NAD deployment

Click the image to enlarge.

We ensure that they exist:

kubectl get network-attachment-definitions -n inf-kubevirt
Checking the NADs

Click the image to enlarge.

This is the end of this second part. Before proceeding with VM creation, we will continue in the next section by deploying some optional but very useful third-party components...