Create Custom Images with Packer
Note: The process of creating custom images with Packer will create temporary resources, which will be automatically deleted after the creation is complete. Therefore, a certain amount of costs will be incurred.
Overview
Packer is a lightweight open-source tool for automated image packaging developed by Hashicorp. Cloud providers can integrate their own Builders into Packer. With a single configuration file, it efficiently and parallelly creates consistent images for multi-cloud platforms. Packer runs on common mainstream operating systems. It is not a substitute for tools like Chef or Puppet but integrates and uses these automated configuration tools to pre-install software on images. Combined with tools such as UCloud Global Terraform and UCloud Global CLI, it enables Infrastructure as Code (IaC), continuous integration, and rapid delivery in multi-cloud DevOps scenarios.
As shown in the diagram below, Packer integrates tools like Chef, Shell, and Puppet in Provisioner to create immutable images containing various software for use by cloud hosts, Docker, and other components across multi-cloud platforms.
Comparison of Packer and traditional console image creation
Console Image Creation | Image Creation with Packer | |
---|---|---|
Usage | Clicking on the console | Building using configuration files |
Reusability | Low. The same operation needs to be executed each time, and image consistency cannot be guaranteed and there’s no version control | High. Configuration files can be copied and modified, allowing version control |
Complexity | High. It requires firstly using a base image to create a UHost and then manually deploying into the UHost, followed by manual image creation | Low. Execute configuration files, automatically execute pre-configured automation scripts, and then automatically build images |
Creation Time | Long. Procedural operation requires human attendance, and it’s not possible to accurately wait for every procedure to execute | Short. Automated workflow operation, perfect polling waiting mechanism, and seamless connection of every procedure |
Lifecycle of Packer image creation
- The user builds the JSON template and executes the packer build command to call the UCloud Global Builder;
- The parameters are validated in advance to ensure usability;
- Create temporary resources like UHost, EIP, etc. (no EIP required if configured as an internal network environment);
- Connect to the host via SSH or WinRM, etc., and execute the Provisioner process;
- Shut down the UHost and create an image;
- Copy the image;
- Delete temporary resources like the host, EIP, etc.;
- Execute post-processing procedures (such as local image import, etc.).
Quickstart
Related Links
To install Packer
Open source repository address
Feel free to contribute codes to UCloud Global Packer Builder
Configure the Environment
Install Packer
- Refer to the official installation document to install Packer
Configure Default User
Set keys TEST_PUBLIC_KEY, TEST_PRIVATE_KEY and project ID TEST_PROJECT_ID as global environment variables (recommended), or explicitly specify public_key, private_key, project_id in the json file.
Write a JSON File
Let’s take building a custom image with Nginx installed as an example. First, create a clean empty folder as the workspace, switch to this directory, and write a JSON specification file (e.g., test.json) as follows:
{
"variables": {
"test_public_key": "{{env `TEST_PUBLIC_KEY`}}",
"test_private_key": "{{env `TEST_PRIVATE_KEY`}}",
"test_project_id": "{{env `TEST_PROJECT_ID`}}"
},
"builders": [
{
"type": "uhost",
"public_key": "{{user `test_public_key`}}",
"private_key": "{{user `test_private_key`}}",
"project_id": "{{user `test_project_id`}}",
"region": "cn-bj2",
"availability_zone": "cn-bj2-02",
"instance_type": "n-basic-2",
"source_image_id": "uimage-f1chxn",
"ssh_username": "root",
"image_name": "packer-test-basic-bj"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"yum install -y nginx"
]
}
]
}
The above defines a uhost Builders constructor and a provisioners configurator . A custom image can be built with one click by executing the command packer build test.json.