Introduction: What is HashiCorp Packer and Why Use It?

HashiCorp Packer is a tool that allows you to create custom virtual machine images for various platforms such as
KVM, VMWare, Vagrant, Docker, AWS, Azure, Google Cloud
, etc. It’s one of the most important tools in a DevOps engineer’s arsenal.
Packer provides a major benefit for users who need to create images for multiple platforms; it abstracts away platform-specific differences (e.g. KVM vs VMWare vs Hyper-V). It creates identical machine images for multiple platforms from a single source configuration.
Packer is a cross-platform tool that runs on Linux, macOS, Windows, FreeBSD, NetBSD, OpenBSD, and Solaris.

How Does Packer Work?

Packer reads in a configuration or template file to determine how the images should be built. The configuration file can be JSON or HCL2 (HashiCorp’s Configuration file).
Packer’s process for creating a machine image from an existing source image is as follows:
  1. Packer boots up an instance using a base image or ISO either locally, in a remote machine, or in the cloud
  1. It then runs configuration tools like Ansible or scripts to configure the instance into the desired state.
  1. Finally, it creates the image and downloads it from ESXi or the host where the image may be created.
For KVM, Packer uses QEMU to create the image.
For VMWare, the example we will use, it uses a VMWare ESXi server to build the image.
In this article, I’ll show you how to use Packer to create a new Rocky Linux 8.6 operating system that can run on both KVM and VMWare. This could serve as the golden image for different environments and applications. With just a few small changes, you can easily adapt this recipe to support additional platforms.

Install Packer

The installation instructions and the packages are available at this URL:

Sample Packer Template/Configuration File

This sample is a fully working config for building Rocky Linux 8.6 for both KVM/QEMU and VMWare.
In the example above, the ISO is stored locally, but it can also be a remote URL that Packer will download. I chose to download the ISO locally to speed up testing because it usually takes longer to download from a remote server.
For QEMU/KVM builds, if you want to access the console to see the installation as it happens, you can connect to the VM that’s being built via VNC. When Packer sets up the VNC connection, it will show the port where you can connect to:
You can also force the port that it should use in the Packer configuration file.
For the VMWare ESXi connection settings, I store them in a separate file to keep them protected and hidden:
This is the kickstart file that builds the image. It’s templatized
To build the images, run

Conclusion

HashiCorp’s Packer is a great tool for building images for multiple platforms. It is highly desirable to use a tool like Packer to create golden images that document everything that goes into creating the images. The process is easy and repeatable with Packer.
Share this article

Join the newsletter

Join thousands of satisfied readers.