Creating a VMware vSphere Virtual Machine Template

Creating virtual machines in a repeatable and reliable fashion can often be difficult. VMware vSphere offers the ability to build one VM that can then be converted to a template. The template can then be used to create identically configured VMs. Rancher leverages this capability within node pools to create identical RKE1 and RKE2 nodes.

In order to leverage the template to create new VMs, Rancher has some specific requirements that the VM must have pre-installed. After you configure the VM with these requirements, you will next need to prepare the VM before creating the template. Finally, once preparation is complete, the VM can be converted to a template and moved into a content library, ready for Rancher node pool usage.

Requirements

There is specific tooling required for both Linux and Windows VMs to be usable by the vSphere node driver. The most critical dependency is cloud-init for Linux and cloudbase-init for Windows. Both of these are used for provisioning the VMs by configuring the hostname and by setting up the SSH access and the default Rancher user. Users can add more content to these as desired if other configuration is needed. In addition, other requirements are listed below for reference.

Creating a VMware vSphere Virtual Machine Template - 图1note

If you have any specific firewall rules or configuration, you will need to add this to the VM before creating a template.

Linux Dependencies

The packages that need to be installed on the template are listed below. These will have slightly different names based on distribution; for example, some distributions ship these dependencies by default. The cluster provisioner will automatically install the dependencies required for Kubernetes. The dependencies listed below are required for the functioning of the Rancher cluster provisioner (not for Kubernetes):

  • curl
  • wget
  • git
  • net-tools
  • unzip
  • apparmor-parser
  • ca-certificates
  • cloud-init
  • cloud-guest-utils
  • cloud-image-utils
  • growpart
  • cloud-initramfs-growroot
  • open-iscsi
  • openssh-server
  • open-vm-tools

Windows Dependencies

The list of packages that need to be installed on the template is as follows:

Creating a VMware vSphere Virtual Machine Template - 图2About the configuration for Windows templates varies between RKE1 and RKE2:

  • RKE1 leverages Docker, so any RKE1 templates need to have Docker EE pre-installed as well
  • RKE2 does not require Docker EE, and thus it does not need to be installed

Creating a Template

You may either manually create your VM or you can utilize other alternatives to create your VM.

Manual Creation

  1. Manually create your VM by following these instructions from VMware. Once you have a VM running, you can manually install the dependencies listed above to configure the VM correctly for the vSphere node driver.
  2. Customize as needed based on your specific environment and requirements.
  3. Proceed with the final preparation before creating your template.

Alternatives to Manual Creation

Other alternative options to create VMs are listed below:

Packer is a frequently-used alternative. Refer to this reference for examples of its usage with vSphere.

Preparing Your VM

After creating a VM with all the required dependencies (and any additional required items), you must perform the most critical step next: preparing the VM to be turned into a template. This preparation will reset critical data such as the VM hostname, IPs, etc., to prevent that information from being brought into a new VM. If you fail to perform this step, you could create a VM with the same hostname, IP address, etc.

Note that these preparatory steps differ between Linux and Windows.

Linux Preparation

The commands below will reset your VM in Linux:

  1. # Cleaning logs.
  2. if [ -f /var/log/audit/audit.log ]; then
  3. cat /dev/null > /var/log/audit/audit.log
  4. fi
  5. if [ -f /var/log/wtmp ]; then
  6. cat /dev/null > /var/log/wtmp
  7. fi
  8. if [ -f /var/log/lastlog ]; then
  9. cat /dev/null > /var/log/lastlog
  10. fi
  11. # Cleaning udev rules.
  12. if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
  13. rm /etc/udev/rules.d/70-persistent-net.rules
  14. fi
  15. # Cleaning the /tmp directories
  16. rm -rf /tmp/*
  17. rm -rf /var/tmp/*
  18. # Cleaning the SSH host keys
  19. rm -f /etc/ssh/ssh_host_*
  20. # Cleaning the machine-id
  21. truncate -s 0 /etc/machine-id
  22. rm /var/lib/dbus/machine-id
  23. ln -s /etc/machine-id /var/lib/dbus/machine-id
  24. # Cleaning the shell history
  25. unset HISTFILE
  26. history -cw
  27. echo > ~/.bash_history
  28. rm -fr /root/.bash_history
  29. # Truncating hostname, hosts, resolv.conf and setting hostname to localhost
  30. truncate -s 0 /etc/{hostname,hosts,resolv.conf}
  31. hostnamectl set-hostname localhost
  32. # Clean cloud-init
  33. cloud-init clean -s -l

Windows Preparation

Windows has a utility called sysprep that is used to generalize an image and reset the same items listed above for Linux. The command is as follows:

  1. sysprep.exe /generalize /shutdown /oobe

Converting to a Template

  1. Shut down and stop the VM.
  2. Right-click on the VM in the inventory list and select Template.
  3. Click on Convert to Template.

Result: Once the process has completed, a template will be available for use.

For additional information on converting a VM to a template, see the VMware guide.

Moving to a Content library

Rancher has the ability to use templates provided by a content library. Content libraries store and manage content within vSphere, and they also offer the ability to publish and share that content.

Below are some helpful links on content libraries:

Other Resources

Here is a list of additional resources that may be useful: