安装packer(这是在mac平台下安装):
ida@timbo ~/Desktop/ mkdir packer
ida@timbo ~/Desktop/ cd packer
ida@timbo ~/Desktop/packer wget https://releases.hashicorp.com/packer/1.4.5/packer_1.4.5_darwin_amd64.zip
ida@timbo ~/Desktop/ unzip packer_1.4.5_darwin_amd64.zip
ida@timbo ~/Desktop/ mv packer /usr/local/bin
✘ ida@timbo ~/Desktop/packer packer version
Packer v1.4.5
显示版本号说明packer正确安装了。
输出阿里云帐号的ak/sk 的环境变量
ida@timbo ~/Desktop/packer export ALICLOUD_SECRET_KEY="xxxxxxxxxxxxxQuJ9HLdOvF0R4uL"
ida@timbo ~/Desktop/packer export ALICLOUD_ACCESS_KEY="LTAI4Fd3Kxxxxxxxxxxx"
编写 带安装redis服务的ubuntu16.04 服务器的json文件,如下:
ida@timbo ~/Desktop/packer cat alicloud.json
{
"variables": {
"access_key": "{{env `ALICLOUD_ACCESS_KEY`}}",
"secret_key": "{{env `ALICLOUD_SECRET_KEY`}}"
},
"builders": [{
"type":"alicloud-ecs",
"access_key": "{{user `ALICLOUD_ACCESS_KEY`}}",
"secret_key": "{{user `ALICLOUD_SECRET_KEY`}}",
"region":"cn-beijing",
"image_name":"packer_test_image",
"source_image":"ubuntu_16_04_64_20G_alibase_20191112.vhd",
"ssh_username":"root",
"instance_type":"ecs.c5.xlarge",
"internet_charge_type":"PayByTraffic",
"io_optimized":"true"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y redis-server"
]
}]
}
用命令:packer validate alicloud.json 验证json文件正确性:
✘ ida@timbo ~/Desktop/packer packer validate alicloud.json
Template validated successfully.
运行packer build alicloud.json 构建自定义的镜像
ida@timbo ~/Desktop/packer packer build alicloud.json
alicloud-ecs output will be in this color.
==> alicloud-ecs: Prevalidating source region and copied regions...
==> alicloud-ecs: Prevalidating image name...
alicloud-ecs: Found image ID: ubuntu_16_04_64_20G_alibase_20191112.vhd
==> alicloud-ecs: Creating temporary keypair: packer_5de6158c-8b1a-16e6-4616-87f5eda79a83
==> alicloud-ecs: Creating vpc...
alicloud-ecs: Created vpc: vpc-2zeecb1xd6ww7jb9sulqq
==> alicloud-ecs: Creating vswitch...
alicloud-ecs: Created vswitch: vsw-2zepkgxdwg5s1fzh84ab8
==> alicloud-ecs: Creating security group...
alicloud-ecs: Created security group: sg-2ze2n9cnoeathz5tn2lj
==> alicloud-ecs: Creating instance...
alicloud-ecs: Created instance: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Allocating eip...
alicloud-ecs: Allocated eip: 123.56.249.148
alicloud-ecs: Attach keypair packer_5de6158c-8b1a-16e6-4616-87f5eda79a83 to instance: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Starting instance: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Using ssh communicator to connect: 123.56.249.148
==> alicloud-ecs: Waiting for SSH to become available...
==> alicloud-ecs: Connected to SSH!
==> alicloud-ecs: Provisioning with shell script: /var/folders/lf/fw7ws13n0fnbbw2vv8mzf7jc0000gp/T/packer-shell507092650
alicloud-ecs: Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial InRelease [247 kB]
alicloud-ecs: Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates InRelease [109 kB]
alicloud-ecs: Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security InRelease [109 kB]
alicloud-ecs: Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main Sources [868 kB]
alicloud-ecs: Get:5 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe Sources [7,728 kB]
alicloud-ecs: Get:6 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 Packages [1,201 kB]
alicloud-ecs: Get:7 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main i386 Packages [1,196 kB]
alicloud-ecs: Get:8 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main Translation-en [568 kB]
alicloud-ecs: Get:9 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 Packages [7,532 kB]
alicloud-ecs: Get:10 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe i386 Packages [7,512 kB]
alicloud-ecs: Get:11 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe Translation-en [4,354 kB]
alicloud-ecs: Get:12 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main Sources [344 kB]
alicloud-ecs: Get:13 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe Sources [264 kB]
alicloud-ecs: Get:14 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 Packages [1,079 kB]
alicloud-ecs: Get:15 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main i386 Packages [887 kB]
alicloud-ecs: Get:16 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main Translation-en [416 kB]
alicloud-ecs: Get:17 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe amd64 Packages [772 kB]
alicloud-ecs: Get:18 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe i386 Packages [700 kB]
alicloud-ecs: Get:19 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe Translation-en [324 kB]
alicloud-ecs: Get:20 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main Sources [158 kB]
alicloud-ecs: Get:21 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe Sources [114 kB]
alicloud-ecs: Get:22 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 Packages [786 kB]
alicloud-ecs: Get:23 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main i386 Packages [618 kB]
alicloud-ecs: Get:24 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main Translation-en [303 kB]
alicloud-ecs: Get:25 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe amd64 Packages [466 kB]
alicloud-ecs: Get:26 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe i386 Packages [401 kB]
alicloud-ecs: Get:27 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe Translation-en [191 kB]
alicloud-ecs: Fetched 39.2 MB in 7s (5,445 kB/s)
alicloud-ecs: Reading package lists...
alicloud-ecs: Reading package lists...
alicloud-ecs: Building dependency tree...
alicloud-ecs: Reading state information...
alicloud-ecs: The following package was automatically installed and is no longer required:
alicloud-ecs: libopts25
alicloud-ecs: Use 'sudo apt autoremove' to remove it.
alicloud-ecs: The following additional packages will be installed:
alicloud-ecs: libjemalloc1 redis-tools
alicloud-ecs: Suggested packages:
alicloud-ecs: ruby-redis
alicloud-ecs: The following NEW packages will be installed:
alicloud-ecs: libjemalloc1 redis-server redis-tools
alicloud-ecs: 0 upgraded, 3 newly installed, 0 to remove and 98 not upgraded.
alicloud-ecs: Need to get 519 kB of archives.
alicloud-ecs: After this operation, 1,507 kB of additional disk space will be used.
alicloud-ecs: Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB]
alicloud-ecs: Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe amd64 redis-tools amd64 2:3.0.6-1ubuntu0.4 [95.5 kB]
alicloud-ecs: Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe amd64 redis-server amd64 2:3.0.6-1ubuntu0.4 [344 kB]
==> alicloud-ecs: debconf: unable to initialize frontend: Dialog
==> alicloud-ecs: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
==> alicloud-ecs: debconf: falling back to frontend: Readline
==> alicloud-ecs: debconf: unable to initialize frontend: Readline
==> alicloud-ecs: debconf: (This frontend requires a controlling tty.)
==> alicloud-ecs: debconf: falling back to frontend: Teletype
==> alicloud-ecs: dpkg-preconfigure: unable to re-open stdin:
alicloud-ecs: Fetched 519 kB in 0s (17.3 MB/s)
alicloud-ecs: Selecting previously unselected package libjemalloc1.
alicloud-ecs: (Reading database ... 67949 files and directories currently installed.)
alicloud-ecs: Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ...
alicloud-ecs: Unpacking libjemalloc1 (3.6.0-9ubuntu1) ...
alicloud-ecs: Selecting previously unselected package redis-tools.
alicloud-ecs: Preparing to unpack .../redis-tools_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
alicloud-ecs: Unpacking redis-tools (2:3.0.6-1ubuntu0.4) ...
alicloud-ecs: Selecting previously unselected package redis-server.
alicloud-ecs: Preparing to unpack .../redis-server_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
alicloud-ecs: Unpacking redis-server (2:3.0.6-1ubuntu0.4) ...
alicloud-ecs: Processing triggers for libc-bin (2.23-0ubuntu11) ...
alicloud-ecs: Processing triggers for man-db (2.7.5-1) ...
alicloud-ecs: Processing triggers for systemd (229-4ubuntu21.16) ...
alicloud-ecs: Processing triggers for ureadahead (0.100.0-19) ...
alicloud-ecs: Setting up libjemalloc1 (3.6.0-9ubuntu1) ...
alicloud-ecs: Setting up redis-tools (2:3.0.6-1ubuntu0.4) ...
alicloud-ecs: Setting up redis-server (2:3.0.6-1ubuntu0.4) ...
alicloud-ecs: Processing triggers for libc-bin (2.23-0ubuntu11) ...
alicloud-ecs: Processing triggers for systemd (229-4ubuntu21.16) ...
alicloud-ecs: Processing triggers for ureadahead (0.100.0-19) ...
==> alicloud-ecs: Stopping instance: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Waiting instance stopped: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Creating image: packer_test_image
alicloud-ecs: Detach keypair packer_5de6158c-8b1a-16e6-4616-87f5eda79a83 from instance: i-2zeinznetzuyvl50t5zj
==> alicloud-ecs: Cleaning up 'EIP'
==> alicloud-ecs: Cleaning up 'instance'
==> alicloud-ecs: Cleaning up 'security group'
==> alicloud-ecs: Cleaning up 'vSwitch'
==> alicloud-ecs: Cleaning up 'VPC'
==> 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-2zeiqj5av1yatpwmwm9s
ida@timbo ~/Desktop/packer
可以到阿里云的控制台查看 刚生成的镜像 packer_test_image