环境准备
为目标实例安装并配置AliyunCLI
安装CLI
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-3.0.60-amd64.tgz
tar -zxvf aliyun-cli-linux-3.0.60-amd64.tgz
mv aliyun /usr/local/bin
配置CLI,指定profile为:default
aliyun configure
编写packer模板,以更新alicloud镜像为例(配置参数需要根据需求自定义)。
{
"builders": [
{
"type": "alicloud-ecs",
"region": "cn-hangzhou",
"profile": "default",
"image_name": "OOS_packer_update_image",
"source_image": "<source_image_id>",
"ssh_username": "user_1",
"instance_type": "ecs.g6.xlarge",
"io_optimized": "true",
"system_disk_mapping": {
"disk_category": "cloud_essd",
"disk_size": 40
},
"internet_charge_type": "PayByTraffic",
"image_force_delete": "true"
}
],
"provisioners": [
{
"type": "shell",
"inline": ["sleep 30", "yum install redis.x86_64 -y"]
}
]
}
登录OSS(对象存储)控制台,上传packer模板到Bucket。
执行模板
1.登录OOS控制台,点击公共任务模板,搜索ACS-ECS-RunPacker,点击创建执行。
2.点击下一步:设置参数。
3.选择上文中已配置AliyunCLI的实例(同时所选实例必须配置实例角色,并且实例角色中包含OSS读权限,详情参考批量下载文件到实例中角色配置和授予部分),点击下一步:确定。
sourceType选择oss
sourcePath输入上文中模板文件存储的目录
templateFile输入packer模板的名称
mode选择build
4.点击创建。
5.查看模板执行的情况,packer更新镜像过程较耗时,需要耐心等待一段时间。
流程说明
Packer更新AliCloud镜像流程
==> alicloud-ecs: Prevalidating source region and copied regions...
==> alicloud-ecs: Force delete flag found, skipping prevalidating image name.
alicloud-ecs: Found image ID: m-bp12i2mrva8etqnugcxn
==> alicloud-ecs: Creating temporary keypair: packer_5f90fbd1-27f5-3f5f-a7c3-9470b51183e2
==> alicloud-ecs: Creating vpc...
alicloud-ecs: Created vpc: vpc-bp1m0tkybn06u04v32ryn
==> alicloud-ecs: Creating vswitch...
alicloud-ecs: Created vswitch: vsw-bp1f67vlm6h72osjr2le5
==> alicloud-ecs: Creating security group...
alicloud-ecs: Created security group: sg-bp11tp7gcrgwtqlq9nhf
==> alicloud-ecs: Creating instance...
alicloud-ecs: Created instance: i-bp16p08isghg3hbcwijx
==> alicloud-ecs: Allocating eip...
alicloud-ecs: Allocated eip: 47.97.112.43
alicloud-ecs: Attach keypair packer_5f90fbd1-27f5-3f5f-a7c3-9470b51183e2 to instance: i-bp16p08isghg3hbcwijx
==> alicloud-ecs: Starting instance: i-bp16p08isghg3hbcwijx
==> alicloud-ecs: Using ssh communicator to connect: 47.97.112.43
==> alicloud-ecs: Waiting for SSH to become available...
==> alicloud-ecs: Connected to SSH!
==> alicloud-ecs: Provisioning with shell script: /tmp/packer-shell177009608
alicloud-ecs: CentOS-8 - AppStream 517 kB/s | 4.3 kB 00:00
alicloud-ecs: CentOS-8 - AppStream 38 MB/s | 5.8 MB 00:00
alicloud-ecs: CentOS-8 - Base 498 kB/s | 3.9 kB 00:00
alicloud-ecs: CentOS-8 - Base 28 MB/s | 2.2 MB 00:00
alicloud-ecs: CentOS-8 - Extras 196 kB/s | 1.5 kB 00:00
alicloud-ecs: CentOS-8 - Extras 247 kB/s | 8.1 kB 00:00
alicloud-ecs: Extra Packages for Enterprise Linux 8 - x86_64 614 kB/s | 4.7 kB 00:00
alicloud-ecs: Extra Packages for Enterprise Linux 8 - x86_64 80 MB/s | 8.1 MB 00:00
alicloud-ecs: Dependencies resolved.
alicloud-ecs: ================================================================================
alicloud-ecs: Package Arch Version Repository Size
alicloud-ecs: ================================================================================
alicloud-ecs: Installing:
alicloud-ecs: redis x86_64 5.0.3-2.module_el8.2.0+318+3d7e67ea AppStream 925 k
alicloud-ecs: Enabling module streams:
alicloud-ecs: redis 5
alicloud-ecs:
alicloud-ecs: Transaction Summary
alicloud-ecs: ================================================================================
alicloud-ecs: Install 1 Package
alicloud-ecs:
alicloud-ecs: Total download size: 925 k
alicloud-ecs: Installed size: 3.2 M
alicloud-ecs: Downloading Packages:
alicloud-ecs: redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_6 11 MB/s | 925 kB 00:00
alicloud-ecs: --------------------------------------------------------------------------------
alicloud-ecs: Total 11 MB/s | 925 kB 00:00
alicloud-ecs: Running transaction check
alicloud-ecs: Transaction check succeeded.
alicloud-ecs: Running transaction test
alicloud-ecs: Transaction test succeeded.
alicloud-ecs: Running transaction
alicloud-ecs: Preparing : 1/1
alicloud-ecs: Running scriptlet: redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64 1/1
alicloud-ecs: Installing : redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64 1/1
alicloud-ecs: Running scriptlet: redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64 1/1
alicloud-ecs: Verifying : redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64 1/1
alicloud-ecs:
alicloud-ecs: Installed:
alicloud-ecs: redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64
alicloud-ecs:
alicloud-ecs: Complete!
==> alicloud-ecs: Stopping instance: i-bp16p08isghg3hbcwijx
==> alicloud-ecs: Waiting instance stopped: i-bp16p08isghg3hbcwijx
==> alicloud-ecs: Deleting duplicated image and snapshot in cn-hangzhou: OOS_packer_test2
==> alicloud-ecs: Creating image: OOS_packer_test
alicloud-ecs: Detach keypair packer_5f90fbd1-27f5-3f5f-a7c3-9470b51183e2 from instance: i-bp16p08isghg3hbcwijx
==> 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 after 6 minutes 28 seconds.
==> Wait completed after 6 minutes 28 seconds
==> Builds finished. The artifacts of successful builds are:
--> alicloud-ecs: Alicloud images were created:
cn-hangzhou: m-bp1ah9sq4uzj3yfi62il
文档内容是否对您有帮助?