Inspired by @jdownie and @mcrilly I have had a go at terraform. My use case is that of a base in which to start playing with kubernetes. @jdownie instantiates and tears down his vms with bash scripts but that seemed too hard for me to get going.
Ubuntu server 2404 is frequently used as the base distro for many kubernetes projects and so I went with that. Sam Gabrail has done a couple of recent videos on getting things up and running. He has associated blog posts and most importantly a github repository.
If youâre interested you can read through the blog posts. In retrospect I would be inclined to just use Samâs packer install of the ubuntu server template âas isâ. I mucked around and failed with creating my own user rather that the default (ubuntu: pw - ubuntu) and I used the latest version of ubuntu-24.04.3-live-server-amd64.iso not the version 2 that Sam uses. When instantiating a VM from a template an automatic upgrade runs on each boot so a slightly more recent base install makes little difference (and even less after the first boot).
The nice thing about building a template this way is that you can strip out all unwanted apps and get down to the bare essentials. However you can also add in anything else you may want. I have NFS backend NAS repositories so I added nfs-client.
If you look in the repository at âŚ/http/user-data you will see
packages:
- qemu-guest-agent
- sudo
- openssh-server
- net-tools
- perl
- open-iscsi
snip- nfs-client
The grub boot configuration emulates that of a user at the keyboard. These instructions are pretty specific for each distro and possibly release (although ubuntu-24.04 versions 2 and 3 were the same). It seems a bit fiddly to sort this out and if one wanted to create a template for another distro it would be much better to steal somebody elseâs configuration.
boot_command = [
â<esc><wait>â,
âe<wait>â,
â<down><down><down><end>â,
" autoinstall quiet ds=nocloud",
â<f10><wait>â,
â<wait1m>â,
âyes<enter>â
]
This does the following.
- Press ESC to access the GRUB menu
- Press âeâ to edit the boot entry
- Navigate to the end of the kernel command line
- Add âautoinstall quiet ds=nocloudâ to enable autoinstall
- Press F10 to boot with these options
- Wait for the installer to start
- Respond âyesâ to the partitioning prompt
Sam installs docker from the docker repository in his script. I pulled that out for k3s. It can be added later via ansible.
Security seems pretty tight with the script verifying the sha256sum of the iso and also requiring an api key to get into the build process on the proxmox server.
The process as outlined on the blog post is:
- Initialization: Packer checks and installs required plugins
- VM Creation: Packer instructs Proxmox to create a new VM with ID 9001
- ISO Mounting: Proxmox attaches the Ubuntu 24.04 ISO and the cloud-init ISO
- Installation: The Ubuntu installer runs with our autoinstall configuration
- Provisioning: After installation, Packer connects via SSH and runs our provisioners
- Template Conversion: Finally, the VM is converted to a template
So at the end of it you have a custom template for your kubernetes cluster. It seems like a lot of hard work but once itâs done, itâs done. The real magic happens in part two when terraform instantiates the cluster.