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,
- setup.sh - contains some basic shell script to install docker and nomad in an ubuntu machine.
- 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"
}
}
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.
Clone the git repo https://github.com/justinepdevasia/nomad-terraform-vultr-cloud
open terminal inside the repo and
cd packer
run command
export VULTR_API_KEY="your-vultr-api-key"
run command
packer init .
run command
packer build .
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.
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.
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.
Top comments (0)