packer详解
packer是什么
组件有哪些
配置文件怎么编写
packer详解
1、安装pakcer
二进制文件,直接从网上下载
https://releases.hashicorp.com/packer/1.15.3/packer_1.15.3_linux_amd64.zip

[root@openstack02 ~]# mv packer /usr/bin/
[root@openstack02 ~]# /usr/bin/packer --help
Usage: packer [--version] [--help] <command> [<args>]
Available commands are:
build build image(s) from template
console creates a console for testing variable interpolation
fix fixes templates from old versions of packer
fmt Rewrites HCL2 config files to canonical format
hcl2_upgrade transform a JSON template into an HCL2 configuration
init Install missing plugins or upgrade plugins
inspect see components of a template
plugins Interact with Packer plugins and catalog
validate check that a template is valid
version Prints the Packer version
[root@openstack02 ~]# /usr/bin/packer --version
Packer v1.15.3
# 检查语法是否正确
packer inspect 1.hcl
后缀必须是pkr.hcl
2、packer插件openstack工作过程
第一步:packer认证连接openstack,使用admin账号和密码,连接keystone,拿到token,获得操作权限
第二步:创建一台临时虚拟机
第三步:等待虚拟机启动,获取ip
第四步:ssh登录,执行自定义操作
第五步:系统制作完成,生成镜像,推送到glance上
第六步:自动清理环境
3、packer模板
1、插件声明
packer {
required_plugins {
openstack = {
source = "github.com/hashicorp/openstack"
version = "~> 1.0.0"
}
}
}
定义packer需要加载的openstack 插件
执行的时候会自动的下载插件
或者手动下载
packer plugins install github.com/hashicorp/openstack
如果使用的是ubuntu的qcow2的话,默认是不知道密码的,因此使用密钥插件,将公钥注入进去,自己有私钥就能连接虚拟机,做一些定制化操作
sshkey插件
4、案例
首先下载这个openeuler的qcow2镜像
安装openstack插件
packer plugins install github.com/hashicorp/openstack
编写模板文件openstack.pkr.hcl
[root@openstack02 hcl]# cat openstacl.pkr.hcl
packer {
required_plugins {
openstack = {
version = "~> 1"
source = "github.com/hashicorp/openstack"
}
}
}
source "openstack" "example" {
domain_name = "default"
flavor = "m1-small"
identity_endpoint = "http://192.168.50.42:5000/v3"
image_name = "packer-euler-22.03-sp4.qcow2"
insecure = true
password = "9uwsCEfLqpPLlofWTYKZ7fUHlKKwXlZ35mPX35uc"
region = "RegionOne"
source_image_name = "openEuler-22.03-LTS-SP4-x86_64.qcow2"
networks = ["b2e56ca3-6808-464b-8f0d-709f8ca50029"]
floating_ip_network = "flat_test"
ssh_username = "root"
ssh_password = "openEuler12#$"
tenant_name = "admin"
username = "admin"
image_disk_format = "qcow2"
use_blockstorage_volume = true
}
build {
sources = ["source.openstack.example"]
provisioner "shell" {
inline = [
"echo Build image work is starting",
"yum install vim bash-comp* -y",
"echo 123 | passwd --stdin root",
"echo execute successful"
]
}
}
配置参数详解:
-
domain_nameopenstack默认域名 -
flavor实例规格 -
identity_endpointkeystone认证地址,通过这个地址登录openstack -
image_name最终新镜像生成名称,在openstack中看到的就是这个名字 -
insecure不验证https证书,使用的是http,因此不需要https -
passwordopenstack登录的密码 -
regionopenstack区域名称 -
source_image_name使用本地的镜像文件来创建虚拟机-
external_source_image_name 使用外部镜像的文件来创建,地址是一个url
-
source_image 使用glance中的镜像创建虚拟机
-
-
netwroks虚拟机使用的网络id,必须是uuid,是一个字符串列表 -
floating_ip_network浮动ip所在的外部网络名称,以便ssh登录 -
ssh_username登录虚拟机的账号 -
ssh_password登录虚拟机的密码 -
tenant_name项目名称,一般是admin -
usernameopenstack登录的用户名 -
image_disk_format输出镜像格式,一般是qcow2,但是要结合use_blockstorage_volume为true时才生效 -
use_blockstorage_volume是否使用块存储,就是使用了后端存储为ceph,我没有配置,因此不需要写,使用的本地存储 -
volume_size指定块存储的大小

选择实例的大小必须大于40G,否则创建的临时虚拟机会失败的
创建的虚拟机使用的安全组需要放行22端口,这样才能连接上去,到临时创建的虚拟机里面自定义一些操作
输出信息
# 执行构建
(kolla) [root@openstack02 hcl]# /usr/bin/packer build openstacl.pkr.hcl
openstack.example: output will be in this color.
==> openstack.example: Loading flavor: m1.small
==> openstack.example: Verified flavor. ID: 97b61fde-020d-4c18-b17c-8ca4aa022862
==> openstack.example: Creating temporary RSA SSH key for instance...
==> openstack.example: Not using temporary keypair
==> openstack.example: Found Image ID: e6b07eb2-2f30-4ee1-be1f-068749700001
==> openstack.example: Launching server...
==> openstack.example: Launching server...
==> openstack.example: Server ID: 34337e36-4ff0-4de9-949b-f59ddf514778
==> openstack.example: Waiting for server to become ready...
==> openstack.example: Creating floating IP using network a1be2074-9d9e-4c08-9c2b-0a190d9575d8 ...
==> openstack.example: Created floating IP: '65a3040d-21cf-46f5-9af2-d32a2da44d7a' (192.168.50.132)
==> openstack.example: Associating floating IP '65a3040d-21cf-46f5-9af2-d32a2da44d7a' (192.168.50.132) with instance port...
==> openstack.example: Added floating IP '65a3040d-21cf-46f5-9af2-d32a2da44d7a' (192.168.50.132) to instance!
==> openstack.example: Using SSH communicator to connect: 192.168.50.132
==> openstack.example: Waiting for SSH to become available...
==> openstack.example: Connected to SSH!
==> openstack.example: Provisioning with shell script: /tmp/packer-shell2238320707
==> openstack.example: Build image work is starting
==> openstack.example: OS 553 kB/s | 3.4 MB 00:06
==> openstack.example: everything 6.0 MB/s | 17 MB 00:02
==> openstack.example: EPOL 4.2 MB/s | 4.7 MB 00:01
==> openstack.example: debuginfo 3.6 MB/s | 4.1 MB 00:01
==> openstack.example: source 1.9 MB/s | 1.8 MB 00:00
==> openstack.example: update 4.5 MB/s | 79 MB 00:17
==> openstack.example: update-source 1.5 MB/s | 1.8 MB 00:01
==> openstack.example: Dependencies resolved.
==> openstack.example: ================================================================================
==> openstack.example: Package Arch Version Repository Size
==> openstack.example: ================================================================================
==> openstack.example: Installing:
==> openstack.example: bash-completion noarch 1:2.11-3.oe2203sp4 OS 255 k
==> openstack.example: bash-completion-help noarch 1:2.11-3.oe2203sp4 OS 56 k
==> openstack.example: vim-enhanced x86_64 2:9.0-45.oe2203sp4 update 1.6 M
==> openstack.example: Installing dependencies:
==> openstack.example: gpm-libs x86_64 1.20.7-27.oe2203sp4 OS 14 k
==> openstack.example: vim-common x86_64 2:9.0-45.oe2203sp4 update 7.6 M
==> openstack.example: vim-filesystem noarch 2:9.0-45.oe2203sp4 update 7.2 k
==> openstack.example:
==> openstack.example: Transaction Summary
==> openstack.example: ================================================================================
==> openstack.example: Install 6 Packages
==> openstack.example:
==> openstack.example: Total download size: 9.6 M
==> openstack.example: Installed size: 38 M
==> openstack.example: Downloading Packages:
==> openstack.example: (1/6): gpm-libs-1.20.7-27.oe2203sp4.x86_64.rpm 45 kB/s | 14 kB 00:00
==> openstack.example: (2/6): bash-completion-help-2.11-3.oe2203sp4.no 117 kB/s | 56 kB 00:00
==> openstack.example: (3/6): bash-completion-2.11-3.oe2203sp4.noarch. 485 kB/s | 255 kB 00:00
==> openstack.example: (4/6): vim-filesystem-9.0-45.oe2203sp4.noarch.r 90 kB/s | 7.2 kB 00:00
==> openstack.example: (5/6): vim-enhanced-9.0-45.oe2203sp4.x86_64.rpm 4.7 MB/s | 1.6 MB 00:00
==> openstack.example: (6/6): vim-common-9.0-45.oe2203sp4.x86_64.rpm 1.2 MB/s | 7.6 MB 00:06
==> openstack.example: --------------------------------------------------------------------------------
==> openstack.example: Total 1.3 MB/s | 9.6 MB 00:07
==> openstack.example: retrieving repo key for OS unencrypted from http://repo.openeuler.org/openEuler-22.03-LTS-SP4/OS/x86_64/RPM-GPG-KEY-openEuler
==> openstack.example: OS 7.5 kB/s | 3.0 kB 00:00
==> openstack.example: Importing GPG key 0xB675600B:
==> openstack.example: Userid : "openeuler <openeuler@compass-ci.com>"
==> openstack.example: Fingerprint: 8AA1 6BF9 F2CA 5244 010D CA96 3B47 7C60 B675 600B
==> openstack.example: From : http://repo.openeuler.org/openEuler-22.03-LTS-SP4/OS/x86_64/RPM-GPG-KEY-openEuler
==> openstack.example: Key imported successfully
==> openstack.example: Running transaction check
==> openstack.example: Transaction check succeeded.
==> openstack.example: Running transaction test
==> openstack.example: Transaction test succeeded.
==> openstack.example: Running transaction
==> openstack.example: Preparing : 1/1
==> openstack.example: Installing : vim-filesystem-2:9.0-45.oe2203sp4.noarch 1/6
==> openstack.example: Installing : vim-common-2:9.0-45.oe2203sp4.x86_64 2/6
==> openstack.example: Installing : gpm-libs-1.20.7-27.oe2203sp4.x86_64 3/6
==> openstack.example: Installing : vim-enhanced-2:9.0-45.oe2203sp4.x86_64 4/6
==> openstack.example: Installing : bash-completion-help-1:2.11-3.oe2203sp4.noarch 5/6
==> openstack.example: Installing : bash-completion-1:2.11-3.oe2203sp4.noarch 6/6
==> openstack.example: Running scriptlet: bash-completion-1:2.11-3.oe2203sp4.noarch 6/6
==> openstack.example: Running scriptlet: vim-common-2:9.0-45.oe2203sp4.x86_64 6/6
==> openstack.example: Verifying : bash-completion-1:2.11-3.oe2203sp4.noarch 1/6
==> openstack.example: Verifying : bash-completion-help-1:2.11-3.oe2203sp4.noarch 2/6
==> openstack.example: Verifying : gpm-libs-1.20.7-27.oe2203sp4.x86_64 3/6
==> openstack.example: Verifying : vim-common-2:9.0-45.oe2203sp4.x86_64 4/6
==> openstack.example: Verifying : vim-enhanced-2:9.0-45.oe2203sp4.x86_64 5/6
==> openstack.example: Verifying : vim-filesystem-2:9.0-45.oe2203sp4.noarch 6/6
==> openstack.example:
==> openstack.example: Installed:
==> openstack.example: bash-completion-1:2.11-3.oe2203sp4.noarch
==> openstack.example: bash-completion-help-1:2.11-3.oe2203sp4.noarch
==> openstack.example: gpm-libs-1.20.7-27.oe2203sp4.x86_64
==> openstack.example: vim-common-2:9.0-45.oe2203sp4.x86_64
==> openstack.example: vim-enhanced-2:9.0-45.oe2203sp4.x86_64
==> openstack.example: vim-filesystem-2:9.0-45.oe2203sp4.noarch
==> openstack.example:
==> openstack.example: Complete!
==> openstack.example: Changing password for user root.
==> openstack.example: passwd: all authentication tokens updated successfully.
==> openstack.example: execute successful
==> openstack.example: Stopping server: 34337e36-4ff0-4de9-949b-f59ddf514778 ...
==> openstack.example: Waiting for server to stop: 34337e36-4ff0-4de9-949b-f59ddf514778 ...
==> openstack.example: Creating the image: packer-euler-22.03-sp4.qcow2
==> openstack.example: Image: 614c8481-5686-4ef3-bad6-041813d95df6
==> openstack.example: Waiting for image packer-euler-22.03-sp4.qcow2 (image id: 614c8481-5686-4ef3-bad6-041813d95df6) to become ready...
==> openstack.example: Deleted temporary floating IP '65a3040d-21cf-46f5-9af2-d32a2da44d7a' (192.168.50.132)
==> openstack.example: Terminating the source server: 34337e36-4ff0-4de9-949b-f59ddf514778 ...
Build 'openstack.example' finished after 6 minutes 546 milliseconds.
==> Wait completed after 6 minutes 554 milliseconds
==> Builds finished. The artifacts of successful builds are:
--> openstack.example: An image was created: 614c8481-5686-4ef3-bad6-041813d95df6
推送了一个镜像到上面

这样根据这个镜像创建的虚拟机,密码被设置为123
总结
packer是一个白盒的,知道做了哪些定制化的操作,看配置文件即可
而基于kvm创建的虚拟机,制作的镜像,是一个黑盒的,不知道里面具体的配置,太low了

浙公网安备 33010602011771号