This article comes from Alibaba cloud official mirror: Alibaba open source mirror - OPSX mirror - Alibaba cloud developer community ]
Original link: Create a custom image using Packer - alicloud developer community
background information
This article describes taking the server of Linux system as an example. For the operation of Windows system, see Packer official documents.
1, Installing Packer
1. Connect and log in to the Linux server. Log in to ECS Linux instance server, see Connect to a Linux instance using user name and password authentication.
2. Execute the command cd /usr/local/bin to enter the / usr/local/bin directory.
cd /usr/local/bin
Note: / usr/local/bin directory is the environment variable directory. You can install Packer in this directory or other directories added to the environment variable.
3. Execute the command wget https://releases.hashicorp.com/packer/1.1.1/packer_1.1.1_linux_amd64.zip Get the Packer installation package. visit Packer download page , get another version of Packer installation package.
wget https://releases.hashicorp.com/packer/1.1.1/packer_1.1.1_linux_amd64.zip
4. Execute the command unzip packer_1.1.1_linux_amd64.zip unzip the file.
unzip packer_1.1.1_linux_amd64.zip
5. Execute the command packer -v to verify the Packer installation status.
packer -v
- If the Linux server returns the Packer version number, it means that you have installed Packer correctly.
- If the Linux server prompts command not found, it indicates that the Packer is not installed correctly.
2, Define Packer template
When using Packer to create a custom image, you need to create a JSON format template file. In this template file, you need to specify the generator and configurator for creating custom images. For details, see Alicloud Image Builder and Provisioners . Packer has a variety of configurators, which can be used to configure the content generation method of user-defined images. Take the commonly used Shell configurators as an example to define the packer template.
Create a json file named alicloud in the Linux server and paste the following.
{ "variables": { "access_key": "{{env `ALICLOUD_ACCESS_KEY`}}", "secret_key": "{{env `ALICLOUD_SECRET_KEY`}}" }, "builders": [{ "type":"alicloud-ecs", "access_key":"{{user `access_key`}}", "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", "image_name":"packer_basic", "source_image":"centos_7_02_64_20G_alibase_20170818.vhd", "ssh_username":"root", "instance_type":"ecs.n1.tiny", "internet_charge_type":"PayByTraffic", "io_optimized":"true" }], "provisioners": [{ "type": "shell", "inline": [ "sleep 30", "yum install redis.x86_64 -y" ] }] }
The parameter values you need to customize are shown in the following table.
parameter | describe |
---|---|
access_key | Your AccessKeyID. For more details, see Create AccessKey. Note: because the AccessKey permission is too large, it is recommended that you create a RAM user and use a RAM sub account to create an AccessKey in order to prevent wrong operations. For specific steps, see Create RAM user and Create AccessKey. |
secret_key | Your AccessKeySecret. For more details, see Create AccessKey. |
region | The region where temporary resources are used when creating custom images. |
image_name | Name of the custom image. |
source_image | The name of the basic image can be obtained from the alicloud public image list. |
instance_type | The type of temporary instance generated when creating a custom image. |
internet_charge_type | The public bandwidth payment type of the temporary instance when creating a custom image. |
provisioners | The type of Packer configurator used when creating a custom image. For details, see Packer Configurator. |
3, Create a custom image using Packer
The steps to generate a custom image by specifying the Packer template file are as follows:
- Run the command export alicloud_ ACCESS_ Key = < your AccessKeyID > import your AccessKeyID.
- Run the command export alicloud_ SECRET_ Key = < your AccessKeySecret > import your AccessKeySecret.
- Run the command packer build alicloud JSON to create a custom image.
The running results of the example are as follows. The following example will create a user-defined image with Redis.
alicloud-ecs output will be in this color. ==> alicloud-ecs: Prevalidating alicloud image name... alicloud-ecs: Found image ID: centos_7_02_64_20G_alibase_20170818.vhd ==> alicloud-ecs: Start creating temporary keypair: packer_59e44f40-c8d6-0ee3-7fd8-b1ba08ea94b8 ==> alicloud-ecs: Start creating alicloud vpc --------------------------- ==> alicloud-ecs: Provisioning with shell script: /var/folders/3q/w38xx_js6cl6k5mwkrqsnw7w0000gn/T/packer-shell257466182 alicloud-ecs: Loaded plugins: fastestmirror --------------------------- alicloud-ecs: Total 1.3 MB/s | 650 kB 00:00 alicloud-ecs: Running transaction check --------------------------- ==> alicloud-ecs: Deleting temporary keypair... Build 'alicloud-ecs' finished. ==> Builds finished. The artifacts of successful builds are: --> alicloud-ecs: Alicloud images were created: cn-beijing: m-2ze12578be1oa4ovs6r9