Create a KVM template to K8s clusters based on Debian GNU/Linux Buster

VM logical volume

Let’s create the volume group to store logical volumes to our templates:

# fdisk -l /dev/sda
/dev/sda4       351645696 468860927 117215232 55.9G 8e Linux LVM

# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.

# vgcreate vg-kvm /dev/sda4
  Volume group "vg-kvm" successfully created

# pvs
  PV             VG      Fmt  Attr PSize   PFree 
  /dev/nvme0n1p5 vg-root lvm2 a--  195.31g     0 
  /dev/sda4      vg-kvm  lvm2 a--   55.89g 55.89g

# pvdisplay /dev/sda4
  --- Physical volume ---
  PV Name               /dev/sda4
  VG Name               vg-kvm
  PV Size               55.89 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              14308
  Free PE               11748
  Allocated PE          2560
  PV UUID               Z8QPo0-fUSc-IzVI-kyOZ-mzZg-MWoJ-1rf7Ny

Creating a template for KVM VMs, please notice that this template is going to be used as start point to a K8s cluster configuration and it has some settings related to this purpose:

# lvcreate -L10G -n debian-buster-10_2_0 vg-kvm
  Logical volume "debian-buster-10_2_0" created.
# lvs
  LV                   VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  debian-buster-10_2_0 vg-kvm  -wi-a-----  10.00g

Creating new VM

Notice that I’m using default Guest Network following the first approach settings from this entry:

# virt-install \
    --name k8s-node \
    --ram 4096 \
    --disk path=/dev/mapper/vg--kvm-debian--buster--10_2_0,device=disk,bus=virtio \
    --vcpus 2 \
    --os-type linux \
    --os-variant debian10.2 \
    --network network=default,model=virtio \
    --virt-type kvm \
    --graphics none \
    --console pty,target_type=serial \
    --location /mnt/libvirt/images/debian-10.2.0-amd64-netinst.iso \
    --extra-args 'console=ttyS0,115200n8 serial'

OS installation

Let’s install the mininum amount of required packages, please remember that this VM is going to be prepared specifically as base to a kubernetes node:

k8s-node:~$ curl -sL | sudo sh
k8s-node:~# shutdown -h now

Post installation steps

At this point we need to prepare the VM to be cloned which means that has to be removed any persistent udev entry for network configurations and so on:

# virt-sysprep --enable=cron-spool,dhcp-client-state,dhcp-server-state,logfiles,mail-spool,net-hwaddr,rhn-systemid,ssh-hostkeys,udev-persistent-net,utmp,yum-uuid -d master
[   0.0] Examining the guest ...
[   7.0] Performing "cron-spool" ...
[   7.0] Performing "dhcp-client-state" ...
[   7.0] Performing "dhcp-server-state" ...
[   7.0] Performing "logfiles" ...
[   7.2] Performing "mail-spool" ...
[   7.2] Performing "net-hwaddr" ...
[   7.2] Performing "rhn-systemid" ...
[   7.3] Performing "ssh-hostkeys" ...
[   7.3] Performing "udev-persistent-net" ...
[   7.3] Performing "utmp" ...
[   7.3] Performing "yum-uuid" ...
$ virsh autostart --disable master
Domain master unmarked as autostarted

Cloning VM: Create storage

# lvcreate -L10G -n master vg-kvm
$ virt-clone --original k8s-node --name master --check all=off --file /dev/mapper/vg--kvm-master
Cloning vg--kvm-debian--buster--10_2_0
Clone 'master' created successfully.
$ virsh start master
$ virsh console master
Loading Linux 4.19.0-6-amd64 ...
Loading initial ramdisk ...

Debian GNU/Linux 10 master ttyS0

master login:  

Post clone actions

These are common steps to setup our new clone VM:

##Update /etc/hosts
# vim /etc/hosts
#K8s cluster     master.acme.local       master     worker1.acme.local      worker1     worker2.acme.local      worker2

##Setup network interfaces
# vim /etc/network/interfaces
auto enp1s0
allow-hotplug enp1s0
iface enp1s0 inet static
# ip a del dev enp1s0
# systemctl restart networking
# ip route get via dev enp1s0 src uid 0 

##Set hostname
# hostnamectl set-hostname master.acme.local

##Create ssh keys for the host
# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key

##Create the atjobs directory needed for running atd
# mkdir /var/spool/cron/atjobs
##Create the /var/log/wtmp file to store login information
# touch /var/log/wtmp

Post-cloning actions K8s specific

To setup master node or a worker node follow steps explained here.

And enjoy your new cluster!

Reference links:

“You miss 100% of the shots you don’t take.”
— Wayne Gretzky

One thought on “Create a KVM template to K8s clusters based on Debian GNU/Linux Buster

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s