Before introducing Packer, let's review the steps to customize a VM image without using Packer. First start a virtual machine locally, start with installing the system, then do custom configuration or application installation, and finally package and compress it into an image. For detailed steps, you can refer to the document I wrote earlier on making a Centos 7 image:/2019/04/08/making-centos7-images-for-openstack/, make a Windows image:/2019/04/23/make-windows-server-2016-image-for-openstack/The public cloud can also be used to run a virtual machine instance directly, and then export the image. You can also rely on public clouds such as Ali Cloud or Huawei Cloud, you can directly run a virtual machine instance, and then the relevant configuration, and finally export the image, the use of public clouds compared to locally constructed images to save the time of installing the system, the relevant cloud configuration can also be reduced, you only need to customize the relevant configurations.
Whether you build VM images locally or on the cloud, there are a lot of manual steps required, and Packer is designed to automate the building of images, and Terraform, like Terraform, are from HashiCorp, the official website address:/In the official home page also puts forward the image that code (Images as code) concept, support for virtual machines and container image construction, I experienced a container build process, compared to dockerfile is slightly more complex and difficult to understand, the recommended container image is still written dockerfile, with Packer to build virtual machine images, container construction has been automated before. Virtual machine image construction or manual operation, Packer can greatly improve efficiency. The following is a hands-on demo.
Packer installation, refer to the official website:/packer/install, basic 1-2 commands to complete the installation, sample configuration file code repository:/robin-2016/terraform-demoAfter cloning, perform packer-demo in the directory
First look at the configuration file content structure, the first part of the packer part defines the use of the plug-in, here the use of the AWS cloud, AWS first registered to use a 750-hour free use plan (limited specifications). The second part of the source part of the definition of the mirror source, amazon-ebs logo is AWS cloud hard disk, ami_name for the final generation of the mirror name, {{timestamp}} for the timestamp variable, the multiple execution of the mirror name will be due to the execution of a different time and different, will not be name conflicts, instance_type for the virtual machine instance specifications In fact, the underlying principle of Packer still creates a virtual machine instance, the execution of script commands, export the image, and then delete the virtual machine instance, Packer is to automate the above steps, region for the instance to run in the region, ap-east-1 is the * region, source_ami for the base image ID, here the choice of the public Ubuntu Server 24 image is selected here. The third part of the build is to define the build process, mainly the shell part, you can write scripts to install and configure the application, the example is the redis program installed.
packer {
required_plugins {
amazon = {
version = ">= 1.2.8"
source = "/hashicorp/amazon"
}
}
}
source "amazon-ebs" "ubuntu" {
ami_name = "aws-demo-ubuntu-{{timestamp}}"
instance_type = ""
region = "ap-east-1"
source_ami = "ami-0ad7f83eab34d93a7"
ssh_username = "ubuntu"
}
build {
name = "learn-packer"
sources = [
""
]
provisioner "shell" {
environment_vars = [
"FOO=hello world",
]
inline = [
"echo Installing Redis",
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y redis-server",
"echo \"FOO is $FOO\" > ",
]
}
}
Configure the following two environment variables before execution, AWS AK and SK, Packer will read the environment variables will have permission to operate AWS.
export AWS_ACCESS_KEY_ID="<YOUR_AWS_ACCESS_KEY_ID>"
export AWS_SECRET_ACCESS_KEY="<YOUR_AWS_SECRET_ACCESS_KEY>"
Execute the following commands in turn, you can complete the construction of the image, the first initialization, formatting and verification of the configuration file commands are optional, and finally build the image, the operation looks very similar to Terraform.
#Initialization
packer init .
#format configuration-optional
packer fmt .
#validate configuration-optional
packer validate .
#Build the image
packer build
Wait for the build command to complete, the image is built, you can log into the AWS mirror service, you can see the Packer build the image, the demonstration is complete, such as do not need to remember to manually delete the image.
This example build image process took about 5 minutes, compared to the previous manual operation efficiency improved a lot, you can also add the configuration file to the code repository, with the code with the version management, and Jenkins and other tools to realize the pipeline to build the image, AWS support for Windows mirrors, you need to use Powershell scripts, refer to the official website documentation:/packer/integrations/hashicorp/amazon/latest/components/builder/ebs#windows-2016-sysprep-commands-for-amazon-windows-amis-only。
AWS supports building multiple images in parallel, refer to the official website documentation:/packer/tutorials/aws-get-started/aws-get-started-parallel-builds
The official Packer provider also supports Aliyun, Tencent Cloud and OpenStack, not Huawei Cloud, but there is documentation on building images using Packer in Huawei Cloud's own documentation, refer to the link:/bestpractice-ims/ims_bp_0031.htmlIn AliCloud, Tencent Cloud and Huawei Cloud, there is no documentation on Windows image support, and in comparison, it is still AWS that uses the most detailed documentation.
This is the end of Packer introduction and demo, if it helps you, please click a follow, hehehe.