docs
uhost
How Tos
Image
Packer

Use Packer to create custom images

Note: Temporary resources will be created during the process of using Packer to create custom images. These temporary resources will be automatically deleted after the build is completed, thus incurring certain charges.

Overview

Packer is a lightweight open-source tool for automated image packaging launched by Hashicorp. Cloud vendors can integrate their own Builder into Packer, and with a single configuration file, they can efficiently and concurrently create consistent images for multi-cloud platforms. Packer can run on commonly used mainstream operating systems. It is not a substitute for software like Chef, Puppet, but integrates and uses these automation configuration tools to pre-install software on images. Combined with tools like SurferCloud Terraform and Ucloud CLI, it can implement Infrastructure as Code (IaC), continuous integration, and rapid delivery in multi-cloud DevOps scenarios.

As shown in the figure below, Packer integrates tools such as Chef, Shell, Puppet, etc. in the Provisioner to create immutable images containing various software, which are used by cloud hosts of multi-cloud platforms, Docker, etc.

Comparison of Packer and Traditional Console Image Creation

Console Image CreationPacker Image Creation
UsageClick on the consoleBuild using configuration file
ReusabilityLow. The same operation needs to be performed each time, and image consistency cannot be guaranteed, no version managementHigh. Copy and modify the configuration file, version management possible
Complexity of OperationHigh. First, create a cloud host using the base image, and manually log in to the cloud host for deployment, then manually create the imageLow. Execute the configuration file, automatically execute the pre-configured automation script, and then automatically build the image
Creation TimeLong. Procedural operation, requires manual guarding, cannot accurately wait for each process to executeShort. Automated workflow operation, perfect polling waiting mechanism, seamless connection of each process

Lifecycle of Creating Images with Packer

  1. Users build a JSON template and execute the packer build command to call the SurferCloud Builder;
  2. Parameter validation is performed in advance to ensure availability;
  3. Create temporary resources related to cloud hosts, EIP, etc. (EIP is not required if configured in an intranet environment);
  4. Connect to the host through SSH or WinRM, etc., and execute the Provisioner process;
  5. Shut down the cloud host and create an image;
  6. Copy the image;
  7. Delete temporary resources such as hosts, EIP, etc.;
  8. Execute the post-processing process (such as local image import, etc.).

Quick Start

Related Links

Official Reference Document Address (opens in a new tab)

Used to query various parameters of SurferCloud Packer Builder

Official Download Page (opens in a new tab)

Used for installing Packer

Open Source Repository Address (opens in a new tab)

Welcome to contribute code to Surfercloud Packer Builder

Environment Configuration

Install Packer

Configure Default User

Set the keys SurferCloud_PUBLIC_KEY, SurferCloud_PRIVATE_KEY and set the project ID SurferCloud_PROJECT_ID as global environment variables (recommended), or explicitly specify public_key, private_key, project_id in the json file.

Writing 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, and switch to this directory, write a JSON specification file (e.g., test.json), as follows:

{
  "variables": {
    "ucloud_public_key": "{{env `UCLOUD_PUBLIC_KEY`}}",
    "ucloud_private_key": "{{env `UCLOUD_PRIVATE_KEY`}}",
    "ucloud_project_id": "{{env `UCLOUD_PROJECT_ID`}}"
  },
  "builders": [
    {
      "type": "ucloud-uhost",
      "public_key": "{{user `ucloud_public_key`}}",
      "private_key": "{{user `ucloud_private_key`}}",
      "project_id": "{{user `ucloud_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 SurferCloud-uhost Builders builder and a provisioners configurator (opens in a new tab). By executing the command packer build test.json, a custom image can be built with one click.

  • Company
  • ContactUs
  • Blog
Copyright © 2024 SurferCloud All Rights Reserved