Proxmox and Cloud-Init virtual machine templates

In the world of virtualization, efficiency and automation are key. This is where Proxmox Virtual Environment (VE) comes into play, offering a robust solution for easily managing virtual machines (VMs). One of the most interesting features of Proxmox is the ability to use virtual machine templates in combination with Cloud-Init, which simplifies the deployment process and makes it as smooth as in the cloud. Let's figure it out.

Proxmox VM Templates and Cloud-Init
Proxmox VM Templates and Cloud-Init

What is Proxmox?

Proxmox is a free and open source virtualization platform, which means anyone can use or modify it at no cost. It's like a set of tools that allows you to create multiple isolated computers (virtual machines) on a single physical machine. These virtual machines can simultaneously run multiple operating systems such as Windows or Linux, making them versatile for testing, development, or even production environments.

Proxmox is the manager of these virtual environments. It uses a technology called KVM (kernel-based virtual machine), which ensures that each virtual machine runs smoothly without interfering with others. In addition, Proxmox can handle so-called containers via LXC (Linux Containers), which are even lighter than VMs and great for running individual applications with minimal overhead.

You also can manage your VMs and containers from a browser. Web-based interface designed to be user-friendly, so even those new to virtualization can get started without too much trouble.

What are Proxmox VM Templates?

Proxmox VM templates are essentially pre-configured VMs that serve as blueprints for creating new instances. They include the operating system, installed software, and system configurations. The beauty of templates is that they save time and ensure consistency across deployments. You can quickly spin up new VMs without going through the entire installation and configuration process each time.

The Role of Cloud-Init

Cloud-Init is a versatile package that supports various distributions and handles the initial setup of a VM instance, such as network configuration and SSH key distribution. When a VM boots for the first time, Cloud-Init applies the predefined settings, allowing for a hands-off approach to VM provisioning https://pve.proxmox.com/wiki/Cloud-Init_Support.

Combining Proxmox Templates with Cloud-Init

Integrating Cloud-Init with Proxmox VM templates brings the best of both worlds. Here's how you can leverage this combination to your advantage:

Step 1: Preparing Your Template

Start by preparing your VM with the desired configuration. Install the operating system and all necessary packages, including Cloud-Init. Once your VM is ready, convert it into a template to serve as the foundation for future VMs.

Below is the script we use to create the template:

#! /bin/bash

# Install Cloud-Init enabled Ubuntu VM in Proxmox

# Download Ubuntu Cloud Image (Ubuntu 22.04 cloudimg)
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img

# Install qemu-guest-agent on Ubuntu Cloud Image
# The libguestfs-tools package must be installed on the system where the ubuntu cloudimg will be modified.
apt-get update
apt-get install libguestfs-tools

# This step will install qemu-guest-agent on the Ubuntu cloud image via virt-customize.
virt-customize -a jammy-server-cloudimg-amd64.img --install qemu-guest-agent

# Install git
virt-customize -a jammy-server-cloudimg-amd64.img --install git

# Set environment variables. Change these as necessary.
export STORAGE_POOL="DATA"
export VM_ID="1000"
export VM_NAME="jammy-template"

# Create Proxmox VM image from Ubuntu Cloud Image.
qm create $VM_ID --name $VM_NAME --memory 2048 --net0 virtio,bridge=vmbr2 --scsihw virtio-scsi-pci
qm set $VM_ID --scsi0 $STORAGE_POOL:0,import-from=/root/jammy-server-cloudimg-amd64.img

# Create Cloud-Init Disk and configure boot.
qm set $VM_ID --ide2 $STORAGE_POOL:cloudinit
qm set $VM_ID --boot order=scsi0
qm set $VM_ID --serial0 socket --vga serial0
# qm resize $VM_ID scsi0 +20G

# Convert VM to Template
qm template $VM_ID

# Clean Up
rm jammy-server-cloudimg-amd64.img

Step 2: Deploying VMs from the Template

With your template in place, you can create linked clones quickly. These clones are lightweight and share the base image with the template, which means rapid deployment. Before starting the new VM, configure the network and SSH keys through the Proxmox interface.

Step 3: Customizing with Cloud-Init

When you start a VM from the template, Cloud-Init kicks in. It reads the configuration data from an ISO image attached to the VM as a CD-ROM. This data includes network settings, user accounts, and SSH keys. Proxmox automatically generates this ISO image, and Cloud-Init applies the settings on the first boot.

We go through steps 2 and 3 via Terraform. This significantly speeds up the process when you need to change some template parameters and create several identical machines at the same time.

Best Practices

  • SSH Key Authentication: It's recommended to use SSH keys for authentication rather than passwords for security reasons. Proxmox can store encrypted passwords, but keys are a safer alternative.
  • Serial Console: Many Cloud-Init images rely on a serial console, which is a requirement for OpenStack. Ensure that your template is configured to use a serial console as the display.
  • Custom Configuration: While many distributions offer ready-to-use Cloud-Init images, creating your own ensures that you know exactly what's installed, making it easier to customize later.

Conclusion

Proxmox VM templates combined with Cloud-Init provide a powerful, efficient, and automated way to manage VM deployments. This synergy not only saves time but also ensures that each VM is configured consistently according to your standards.

If you want to learn more, I suggest you start by exploring the official Proxmox documentation and community forums for detailed guides and support

Happy virtualizing!