How Packer and UCloud Streamline Multi‑Cloud Image Automation
This article explains how UCloud's Packer builder automates the creation of consistent, versioned cloud host images, compares it with manual console methods, outlines the full lifecycle, provides code examples, and shows how combining Packer with Terraform enables powerful multi‑cloud Infrastructure as Code workflows.
Background
Cloud hosts are among the most frequently used cloud products. As the number of cloud hosts grows, ensuring consistent versioned deployments becomes a common challenge. Traditionally, users must manually or script‑wise connect to each host and perform installations, a complex process that is hard to keep consistent and reliable.
What is Packer?
Packer is an open‑source tool from HashiCorp that automates the creation of immutable machine images. Cloud providers can add their own builders to Packer, allowing a single configuration file to generate consistent images across multiple cloud platforms in parallel.
UCloud Packer Builder
UCloud developed a builder that was adopted by the official Packer repository. Using Packer to create custom images reduces deployment time, improves reliability, and enhances automated deployment capabilities. HashiCorp released version 1.4.3 on August 14, which includes the UCloud Packer Builder.
Key Advantages
Rapid infrastructure deployment : Images can launch fully configured hosts in seconds.
Multi‑provider portability : The same image runs on any supported cloud.
High stability : Errors are caught during image build rather than at runtime.
High testability : Built images can be smoke‑tested before production use.
High extensibility : Plugins allow easy integration of additional tools.
Comparison with Console‑Based Image Creation
Using the console requires manual steps, offers low reusability, high operational complexity, and long creation time. Packer uses a configuration file, provides high reusability, low complexity, and short creation time.
Lifecycle
The full Packer workflow includes:
Write a JSON template and run
packer buildwith the UCloud builder.
Validate parameters.
Create temporary resources such as hosts and EIPs.
Connect via SSH/WinRM and run provisioners.
Shut down the host and create the image.
Copy the image.
Delete temporary resources.
Run post‑processing steps (e.g., import to local registry).
Usage Demo
A simple example builds an image containing an Nginx application by creating
test.jsonand executing:
<code>packer build test.json</code>Packer for Multi‑Cloud Management
UCloud has accumulated Packer experience internally. It is suitable for managing multi‑cloud environments, maintaining identical systems across public and private clouds, and building virtual‑machine images.
Example Builder Configuration
The following JSON defines a UCloud public‑cloud VM image. Packer creates a clean CentOS 7 instance, runs three shell scripts via the provisioner, and returns the image ID.
<code>{
"variables": {
"ucloud_public_key": "{{env `UCLOUD_PUBKEY`}}",
"ucloud_private_key": "{{env `UCLOUD_SECRET`}}",
"ssh_user": "root",
"ssh_password": "password",
"ucloud_project_id": "org-projectid",
"image_id": "uimage-dpdgyw",
"consul_version": "1.5.1",
"region": "cn-bj2",
"az": "cn-bj2-02"
},
"builders": [
{
"type": "ucloud-uhost",
"public_key": "{{user `ucloud_public_key`}}",
"private_key": "{{user `ucloud_private_key`}}",
"project_id": "{{user `ucloud_project_id`}}",
"region": "{{user `region`}}",
"availability_zone": "{{user `az`}}",
"instance_type": "n-basic-2",
"source_image_id": "{{user `image_id`}}",
"ssh_username": "{{user `ssh_user`}}",
"ssh_password": "{{user `ssh_password`}}",
"image_name": "consul-server-{{user `consul_version`}}"
}
],
"provisioners": [
{
"type": "shell",
"scripts": [
"scripts/config-yum.sh",
"scripts/consul-service.sh",
"scripts/consul-server.sh"
],
"environment_vars": [
"CONSUL_VERSION={{user `consul_version`}}"
]
}
]
}</code>Extending to Docker
To also produce a Docker image, modify the builder to type
dockerand add post‑processors for tagging and pushing to a registry.
<code>{
"variables": {
"consul_version": "1.5.1"
},
"builders": [
{
"type": "docker",
"image": "centos:7",
"commit": true,
"changes": [
"CMD [\"tail -f /dev/null\"]",
"ENTRYPOINT [\"\"]"
]
}
],
"provisioners": [
{
"type": "shell",
"scripts": [
"scripts/config-yum.sh",
"scripts/consul-service.sh",
"scripts/consul-server.sh"
],
"environment_vars": [
"CONSUL_VERSION={{user `consul_version`}}"
]
}
],
"post-processors": [
[
{
"type": "docker-tag",
"repository": "lonegunmanb/consulServer",
"tag": "0.1"
},
"docker-push"
]
]
}</code>Packer + Terraform = 1+1>2
Combined with Git, Terraform, and UCloud CLI, Packer enables Infrastructure as Code (IaC) for continuous integration and rapid delivery. Changes to the image configuration trigger Terraform, which runs
packer build, supplies the new image to Terraform, and finally replaces old instances with the new ones.
Summary
UCloud’s integration of Packer provides automated environment configuration for cloud hosts, works with cloud‑init and other automation tools, and, together with Terraform and CLI, achieves versioned, code‑driven infrastructure, precise delivery, fast deployment, and eventual consistency.
Official documentation: https://www.packer.io/docs/builders/ucloud-uhost.html
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.