A bash script that will tear down and recreate 2 virtual machines from a clone of another VM on a proxmox server.
this script will DESTROY proxmox hosts
be certain to edit the variables master and worker to match the IDs of VMs in proxmox that you wish to replace for example:
master=100
worker=101
A host to clone from - this ideally has cloud-init installed but if using Ubuntu server edition this should already come preinstalled.
Also ensure that on the vm that is being cloned from in the case of Ubuntu that the /etc/machine-id file is emtpy before cloning to stop duplcation of IP addresses.
As this script is simple it does not consider that the target host ids may not already exist. If this is the case simply create them for the first time on a root shell session on your proxmox server with something like :
export clone="your-vm-to-create"
qm clone $clone 100 --description "k8s master" --full --name "k8s-master"
qm clone $clone 101 --description "k8s worker" --full --name "k8s-worker"
This section is if you dont already have a vm to clone from. This is based upon Ubuntu server, here 2204 LTS so mileage may vary with other distros.
download and store a recent and up to date iso image to your proxmox server. I favour ubunutu, an LTS version and server, not desktop.
create a vm from this image and boot it up. When creating the vm ensure to have enough disk, cpu and memory for typical starting workloads for example 4 cpu, 4Gig memory, 30+ Gb disk. When installing initial image make sure to have sshd installed.
get the ip address from the booted VM from its console, typically ip a can do this.
from your trusted workstation, add an ssh pub key to the user you intend to access the new vms with
ssh-copy-id <username@<ip or hostname of your running vm>
use this user and key access to shell in and within the booted VM sudo -i to get a root session and do the following things :
make a new sudo entry in /etc/sudoers.d with something like this :
echo 'bob ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/builduser
where bob is the user that you want to be able to run sudo commands without a password
install some nice to have tools :
sudo apt install -y vim net-tools qemu-guest-agent
qemu-ga -D >> /etc/qemu/qemu-ga.conf
add anything to default profiles for your use case
echo "EDITOR=vi" >> /etc/bash.bashrc
last important step
trunctate the machine-id file in /etc :
> /etc/machine-id
( this last step needs repeated whenever you reboot this vm host for cloning as it will create a new machine id on next boot )
any vm then cloned from a machine with a machine-id already in /etc/machine-id will use that machine id, not one it has created itslef - so you end up with duplicate IP addresses on your LAN. This is not what we want to happen.
shut down the vm
telinit 0
this host will remain shut down for cloning but can be rebooted to do updates / changes but the above /etc/machine-id file will need to be truncated before again shutting it down for future clones
cloning can be done from the web console but for convenience here are the commands to do this from a shell session as the root user of the proxmox server itself where the vm to be clonded has an id of 503 and the target vm id is 201 :
create the cloned vm with
qm clone 503 201 --description "k8s master" --full --name "k8s-master"
enable the qemu guest in proxmox with
qm set 201 --agent enabled=1
start the newly cloned vm
qm start 201
proxmox will have a name in its console that reflects your qm command, here k8s-master but the host itself will still be the same as the vm that it was cloned from
this is not a bug but a feature - so they say but this is down to the many and varied ways in which different Linux distrobutions choose to define their hostname. In the case of Ubuntu ( debian based ) distros and others I could care to mention this is simply a matter of editting the file /etc/hostname which can be done from our trusted workstation with
ssh -t <ip address or hostname here> "echo k8s-master | sudo tee /etc/hostname"
to deploy a test kubernetes to this pair of servers then use something like this repo of ansible to do that or similar
also k3s on github has a simple to use ansible repository at
> https://github.com/k3s-io/k3s-ansible.git
which is easy to use - follow its readme to create a list of your own hosts an inventory file and install a k3s cluster in 1 command
very quick and easy