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 build is complete. Therefore, a certain amount of costs will be incurred.
Overview
Packer is a lightweight open-source tool for automated packaging of images launched by Hashicorp. Cloud vendors can integrate their own Builder into Packer, and with a single configuration file, they can efficiently and in parallel create consistent images for multi-cloud platforms. Packer can run on common mainstream operating systems, and it is not an alternative for software such as Chef and Puppet, but it integrates and uses these automation configuration tools to pre-install software on the image. Together with SurferCloud Terraform and other tools, it can implement Infrastructure as Code (IaC), continuous integration and rapid delivery in multi-cloud DevOps scenarios.
As shown below, Packer creates immutable images that include various software types by integrating tools like Chef, Shell, and Puppet in the Provisioner, for use in multi-cloud platforms, cloud virtual machines, Docker, etc.
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 virtual machine and then manually deploying into the virtual machine, 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, 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 SurferCloud Builder;
- The parameters are validated in advance to ensure usability;
- Temporary resources related to creation like cloud virtual machine, 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 cloud virtual machine and create the 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
Official download page (opens in a new tab)
To install Packer
Open source repository address (opens in a new tab)
Welcome to contribute code to the SurferCloud Packer Builder
Configure the Environment
Install Packer
- Refer to the official installation document (opens in a new tab) 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 installed with nginx as an example. Firstly, create a clean empty folder as a workspace and switch to this directory, then write a JSON specification file (eg: test.json), as below:
{
"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 (opens in a new tab). A custom image can be built with one click by executing the command packer build test.json.