Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A convenient way to create a custom image using the open source tool Packer

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of open source tool Packer convenient way to create a custom image, the article is very detailed, has a certain reference value, interested friends must read it!

Create a custom image using Packer

The open source tool Packer makes it easy to create custom images. It runs on most major operating systems. Refer to this article to install and use Packer to easily create a custom image.

Description

This article describes servers running Linux operating systems only.

1. Install Packer

You can go to the download page of the Packer official website and choose to download the version corresponding to your operating system. Refer to the following steps or visit the official installation instructions of Packer to install Packer.

Connect and log in to the Linux server. If your server is an ECS Linux instance, refer to the documentation for using username and password authentication to connect to the Linux instance.

Execute the command cd / usr/local/bin to enter the / usr/local/bin directory.

Description

The / usr/local/bin directory is the environment variable directory, where you can install Packer or other directories that have been added to the environment variable.

Execute the command wget https://releases.hashicorp.com/packer/1.1.1/packer_1.1.1_linux_amd64.zip to get the Packer installation package. You can visit the Packer download page to get other versions of the Packer installation package.

Execute the command unzip packer_1.1.1_linux_amd64.zip to extract the file.

Execute the command packer-v to verify the Packer installation status. If the Linux server returns the Packer version number, you have installed Packer correctly. If the Linux server prompts command not found to indicate that Packer is not installed correctly.

two。 Define Packer templates

Description

When you create a custom image using Packer, you need to create a template file in JSON format. In this template file, you need to specify the Alicloud Image Builder (generator) and Provisioners (configurator) that create the custom image. Packer has a variety of configurators, which can be used to configure the content generation method of custom images. The following takes the commonly used Shell configurator as an example to define Packer templates.

Create a json file named alicloud in the Linux server and paste the following.

{"variables": {"access_key": "{{env `ALICLOUD_SECRET_ KEY`}}", "secret_key": "{{env `ALICLOUD_SECRET_ KEY`}"}, "builders": [{"type": "alicloud-ecs", "access_key": "{{user `secret_ key`}", "secret_key": "{{user `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"]}]}

Description

You need to customize the following parameter values.

3. Create a custom image using Packer

Specify the Packer template file to generate a custom image:

Import your AccessKeyID: run export ALICLOUD_ACCESS_KEY= your AccessKeyID.

Import your AccessKeySecret: run export ALICLOUD_SECRET_KEY= your AccessKeySecret.

Execute the command packer build alicloud.json to create a custom image.

The running result of the example is as follows. The following example creates a custom 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 Found image ID b1ba08ea94b8colors = > alicloud-ecs: Start creating alicloud vpc--== > alicloud-ecs: Provisioning with shell script: / var/folders/3q/w38xx_ Js6cl6k5mwkrqsnw7w0000gn/T/packer-shell257466182alicloud-ecs: Loaded plugins: fastestmirror--alicloud-ecs: Total 1.3 MB/s | 650 kB 00:00alicloud-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

Next step

Create an instance using a custom image

The above is all the contents of the convenient way to create a custom image using the open source tool Packer. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 257

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report