Creating Custom Images in Google Cloud using HashiCorp Packer.
Before we continue let’s ask why we need HashiCorp PACKER?
Going back in time about 30 years ago setting up machine network, installing operating systems and software, configuring and deploying a machine took about a day’s time and fast forward 10 years to 2000s we got the VMware virtualboxes of the world and configuration management tools like Chef, Puppet, and Ansible which made setting up servers and deploying even faster within hours. Starting 2010s we got CLOUDS (we don’t need to have our own machine! what a perfect world…) which enabled setting up servers within minutes with few clicks of a button. Back to 2022, in the current times of IT software delivery, speed and reliability are major drivers of faster time to market which form the crux of DevOps culture and HashiCorp Packer is where DevOps Begins.
What is HashiCorp Packer?
Packer is an open source tool that enables you to create identical machine images for multiple platforms from a single source template.A common use case is creating “golden images” that teams across an organisation can use in cloud infrastructure. A ‘Golden image’ in the context of packer is a preconfigured template for users and it is the hallmark of consistency and ease of deployment. Packer templates are written in a domain specific language called HCL2 (professionals using Terraform can relate) which is a simplified version of JSON. Packer works across various OS distributions (Linux, Windows & Mac). Below are the reasons we need to adopt packer in our IT work environments.
● Due to evolving Software Architecture at Enterprises and maintaining consistency across workloads.
● Establish image factory based on new commits for continuous delivery.
● Automating monthly OS/Software patching for new/existing workloads.
● Create immutable infrastructure using packer in CI/CD pipeline.
● An added benefit of Packer is its templates can be version controlled.
Core Components of Packer
● Source : Source defines the initial image from which we build packer configured customised images.
● Builders : Plugins that are developed to work with a specific platform (AWS, Azure, GCP, VMware). It is similar to providers in Terraform.
● Provisioners : Provisioners use built in and third party integrations to install packages and configure the machine image (built in integrations include file and different shell options) and third party integrations include Ansible (run play books), Chef , Puppet.
● Post-Processors : Post processors are executed after the image is built and provisioners are complete. It can be used to upload artifacts ,execute uploaded scripts , validate installs or import an image. Examples include
↪ Validate a package using a checksum.
↪ Import a package to AWS as an ami.
↪ Push a docker image to registry.
↪ Convert the artifact into a vagrant box.
↪ Create a VMware template from resulting build.
● Communicators : Communicators are mechanism that packer will use to communicate new build, and upload files, execute scripts etc (Example: SSH,WinRM).
● Variables : Packer can use variables to define defaults during build
variables can be declared in .pkrvars.hcl file. you can also declare individually using the -var option.
PACKER Command Line
CLI is how users interact with Packer and there is no UI or API for Packer. Packer uses subcommand and additional arguments to execute packer functionality. All commands start with ‘packer’ command & subcommands available in packer (build, console, version). build command provides core functionality of packer. Packer has few environment variables — (ex-PACKER_LOG). Below are some commands of packer
↪ packer inspect — shows all components
↪ packer validate (validates the packer template)
↪ packer hcl2 upgrade (updates old json template to hcl2)
PACKER Workflow
DOCKER V PACKER in the context of Image Creation
Docker can be classified as a tool in the “Virtual Machine Platforms & Containers” category, while Packer is grouped under “Infrastructure Build Tools”.
Some of the features offered by Docker are:
- Integrated developer tools
- open, portable images
- shareable, reusable apps
On the other hand, Packer provides the following key features:
- Super fast infrastructure deployment. Packer images allow you to launch completely provisioned and configured machines in seconds, rather than several minutes or hours.
- Multi-provider portability. Because Packer creates identical images for multiple platforms, you can run production in AWS, staging/QA in a private cloud like OpenStack, and development in desktop virtualization solutions such as VMware or VirtualBox.
- Improved stability. Packer installs and configures all the software for a machine at the time the image is built. If there are bugs in these scripts, they’ll be caught early, rather than several minutes after a machine is launched.
Since we got a theoretical grasp of what Packer does and where does it fit in any organisation’s DevOps transformation, Let’s get rolling with a sample demo.
● Building a Google Compute Image with Packer in Google Cloud
Let’s build an image using Ubuntu 20.04 image in a specific region with also providing a service account for Packer.
Step 1 : Write the Packer Configuration with all the details for image creation.
Step 2 : Validate the Packer template.
Step 3 : Build the Packer template for creating the Image.
Step 4 : Build Finished status of Packer configuration.
Step 5 : Packer Image Visible in the Images section of Google Cloud Console