Accelerating Deployment: Creating HashiCorp Nomad Machine Images on Vultr Cloud via Packer

justinepdevasia

justinepdevasia

Posted on March 3, 2024

Accelerating Deployment: Creating HashiCorp Nomad Machine Images on Vultr Cloud via Packer

packer vultr logo

Immutable infrastructure creation in the cloud requires on-demand machine images. Whenever we need to make changes to the system, the old image is torn down and a new one is deployed. Packer is a tool provided by HashiCorp to build machine images easily, which can then be used to deploy cloud VMs. In this post, we will see how we can create a machine image in Vultr Cloud using Packer.

Vultr is a cloud computing platform which enables us to deploy virtual machines and various cloud services like loadbalancers, CDNs etc. . In Vultr, the machine image is called snapshots. We will install Nomad and Docker within the snapshot. Later, when a cluster is created, this snapshot ID can be utilized to swiftly spawn the cluster with all the packages pre-installed.

Firstly, let us install packer using hashicorp documentation. Once packer is setup, we need to create a vultr account. Vultr cloud is providing free $250 credits for first time signup. Generate api key from dashboard and keep it safe, as it is required for creating the snapshot.

Once both packer and vultr is ready, let us use the script and config file. The source code is available here.

Two files are used in creating the image,

  1. setup.sh - contains some basic shell script to install docker and nomad in an ubuntu machine.
  2. ubuntu.pkr.hcl - packer template to build the image

Let us go through the contents of ubuntu.pkr.hcl file.

 variable "vultr_api_key" {
   type      = string
   default   = "${env("VULTR_API_KEY")}"
   sensitive = true
 }

 packer {
   required_plugins {
     vultr = {
       version = ">=v2.3.2"
       source = "github.com/vultr/vultr"
     }
   }
 }

 source "vultr" "ubuntu-nomad" {
   api_key              = "${var.vultr_api_key}"
   os_id                = "2179"
   plan_id              = "vc2-1c-2gb"
   region_id            = "bom"
   snapshot_description = "Ubuntu 23.04 Nomad ${formatdate("YYYY-MM-DD hh:mm", timestamp())}"
   ssh_username         = "root"
   state_timeout        = "25m"
 }

 build {
   sources = ["source.vultr.ubuntu-nomad"]

   provisioner "shell" {
     script = "setup.sh"
   }
 }
Enter fullscreen mode Exit fullscreen mode
  • The variable vultr_api_key is used to connect with vultr cloud securely. It acquires the value from VULTR_API_KEY env variable.

  • The above packer stanza download the correct vultr plugin to use in the machine image creation.

  • The source stanza contains information about the cloud provider, the region in which image required, base os used etc..

  • The build stanza connects to a source and use a provisioner to build the image. Here it execute the build process by running the setup.sh file provided by us.

Now let us see how to run this setup to build our customized os image.

  1. Clone the git repo https://github.com/justinepdevasia/nomad-terraform-vultr-cloud

  2. open terminal inside the repo and cd packer

  3. run command export VULTR_API_KEY="your-vultr-api-key"

  4. run command packer init .

  5. run command packer build .

packer build ongoing

This will trigger a packer build session, the application will connect to vultr cloud, spawn a temporary vm and install the packages. Once setup.sh file is fully executed, snapshot creation is triggered and we get back the snapshot id.

packer build completion

This snapshot id can be used to spawn new vms with all the packages, in this case nomad and docker in built.
Finally we can see the created snapshot in Vultr cloud dashboard.

Image description

Packer has really enabled the creation of machine images a smooth and enjoyable process. Combining it with the simplicity and ease of use of vultr, we get a powerful system for automation of cloud VM image creation. With this approach we can pre-install any package in a snapshot and spawn the VMs without any additional installation after launch.

In the upcoming article, we will discuss how to use this snapshot to deploy a Nomad cluster using Terraform.

💖 💪 🙅 🚩
justinepdevasia
justinepdevasia

Posted on March 3, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related