Packer自动构建虚拟机镜像

Packer

Packer是一个开源工具,用于从单个源配置为多个平台创建相同的机器镜像。

Builders(必须)

Builders用于为指定平台创建集群并生成镜像

相关参数

以下以vmware-vmx的builder为例

key value
type Packer Builder类型
source_path VMX文件路径
vm_name 指定创建的虚拟机名称
displayname 指定生成镜像的虚拟机名称(将覆盖生成的vmx文件的displayname参数)
headless 是否隐藏显示控制台
vnc_disable_password 禁用vnc密码
boot_wait 等待系统启动时间
boot_command 系统启动后执行的命令(也可交给Provisioners执行)
shutdown_command 系统关机执行的命令
output_directory 镜像输出路径
ssh_host ssh连接地址
ssh_timeout ssh超时时间
vmx_data 指定VMX文件的参数
vmx_data_post 与vmx_data一样,区别是在虚拟机关机后更新VMX文件

Provisioners

Provisioners用于通过内置和第三方软件在机器启动后为镜像执行相关的安装、配置

Post-Processors

Post-Processors用于在镜像生成后执行相关操作


通过vmware-vmx生成ova镜像

  • yms-deploy-vmware-ova.json

用于packer执行构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"variables": {
"headless": "true",
"boot_wait": "50s",
"ssh_host": "172.16.100.10",
"ssh_username": "root",
"ssh_password": "password",
"ssh_timeout": "1h",
"vmware_version": "10",
"cpu": "8",
"ram": "16384",
"net_driver": "vmxnet3",
"yms_rom_url": "https://10.200.110.68/publish/enterprise/download/YMS",
"yms_rom_version": "20.0.0.18"
},
"builders": [{
"type": "vmware-vmx",
"source_path": "source/vmware/CentOS7.5_mini.vmx",
"vm_name": "YMS_{{ user `yms_rom_version` }}",
"display_name": "YMS_{{ user `yms_rom_version` }}",
"vmx_data_post": {
"virtualHW.version": "{{ user `vmware_version` }}",
"numvcpus": "{{ user `cpu` }}",
"memsize": "{{ user `ram` }}",
"ethernet0.virtualDev": "{{ user `net_driver` }}"
},
"headless": "{{ user `headless` }}",
"vnc_disable_password": "true",
"ssh_username": "{{ user `ssh_username` }}",
"ssh_password": "{{ user `ssh_password` }}",
"ssh_host": "{{ user `ssh_host` }}",
"ssh_timeout": "{{ user `ssh_timeout` }}",
"shutdown_command": "sync && sudo shutdown -h now",
"output_directory": "packer_cache/vmware/{{ user `yms_rom_version` }}"
}],
"provisioners": [{
"type": "shell",
"environment_vars": [
"YMS_ROM_URL={{ user `yms_rom_url` }}",
"YMS_ROM_VERSION={{ user `yms_rom_version` }}"
],
"script": "scripts/apollo_install.sh"
}],
"post-processors": [{
"type": "shell-local",
"scripts": ["scripts/ovatool.bat"]
}]
}
  • yms-deploy-esxi-ova.json

vmware-vmx连接esxi目前只支持esxi5,且会自动调用voftool生成指定格式(format参数指定,默认ovf)的镜像。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"variables": {
"headless": "true",
"boot_wait": "50s",
"ssh_username": "root",
"ssh_password": "password",
"ssh_timeout": "1h",
"cpu": "8",
"ram": "16384",
"net_driver": "e1000",
"yms_rom_url": "https://10.200.110.68/publish/enterprise/download/YMS",
"yms_rom_version": "20.0.0.13"
},
"builders": [{
"type": "vmware-vmx",
"remote_host": "10.200.110.101",
"remote_type": "esx5",
"remote_username": "root",
"remote_password": "password",
"source_path": "Centos7.5_mini/Centos7.5_mini.vmx",
"remote_datastore": "datastore1_esxi5",
"skip_validate_credentials": true,
"vm_name": "yms-{{ user `yms_rom_version` }}",
"display_name": "yms-{{ user `yms_rom_version` }}",
"vmx_data_post": {
"numvcpus": "{{ user `cpu` }}",
"memsize": "{{ user `ram` }}",
"ethernet0.virtualDev": "{{ user `net_driver` }}"
},
"headless": "{{ user `headless` }}",
"vnc_disable_password": "true",
"ssh_username": "{{ user `ssh_username` }}",
"ssh_password": "{{ user `ssh_password` }}",
"ssh_timeout": "{{ user `ssh_timeout` }}",
"boot_wait": "{{ user `boot_wait` }}",
"shutdown_command": "sync && sudo shutdown -h now",
"output_directory": "yms-rom/{{ user `yms_rom_version` }}",
"format": "ova"
}],
"provisioners": [{
"type": "shell",
"environment_vars": [
"YMS_ROM_URL={{ user `yms_rom_url` }}",
"YMS_ROM_VERSION={{ user `yms_rom_version` }}"
],
"script": "scripts/apollo_install.sh"
}]
}
  • apollo_install.sh

用于provisioners执行YMS的部署

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
curl -O ${YMS_ROM_URL}/YMS_${YMS_ROM_VERSION}.tar.gz
tar zxf YMS_${YMS_ROM_VERSION}.tar.gz
cd apollo_install
tar zxf install.tar.gz
mkdir -p /usr/local/apollo/data/
cat > /usr/local/apollo/data/install.conf <<EOF
[global]
[manager-master]
ip=127.0.0.1
EOF
./install.sh
  • ovatool.bat批处理

用于将生成的镜像(vmx、vmdk)转换为ova格式

1
2
for /f tokens^=4^ delims^=^"  %%i in ('type yms-deploy-vmware-ova.json^|find /i "yms_rom_version"^|find /v "use"') do set yms_rom_version=%%i
"D:\Program Files (x86)\VMware\VMware Workstation\OVFTool\ovftool.exe" packer_cache\vmware\%yms_rom_version%\YMS_%yms_rom_version%.vmx yms-rom\YMS_%yms_rom_version%.ova


通过qemu生成qcow2格式镜像

  • yms-deploy-qemu.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
"variables": {
"iso_checksum": "55b190e5d9daf15898b53223e6343710",
"iso_checksum_type": "md5",
"iso_url": "/var/lib/libvirt/images/centos7.5_mini.qcow2",
"ssh_username": "root",
"ssh_password": "password",
"ssh_timeout": "1h",
"yms_rom_url": "https://10.200.110.68/publish/enterprise/download/YMS",
"yms_rom_version": "21.0.0.12"
},
"builders": [{
"type": "qemu",
"iso_checksum": "{{ user `iso_checksum` }}",
"iso_checksum_type": "{{ user `iso_checksum_type` }}",
"iso_url": "{{ user `iso_url` }}",
"vm_name": "YMS_{{ user `yms_rom_version` }}",
"accelerator": "kvm",
"qemu_binary": "/usr/libexec/qemu-kvm",
"headless": false,
"disk_image": true,
"disk_size": "512000",
"net_device": "virtio-net-pci",
"qemuargs": [
["-smp", "4,sockets=4,cores=1,threads=1"],
[ "-m", "8192M"],
[ "-netdev", "type=tap,script=scripts/qemu-ifup.sh,id=net0"],
[ "-net", "nic"],
[ "-net", "user,hostfwd=tcp::{{ .SSHHostPort }}-:22"],
[ "-device", "virtio-net-pci,netdev=net0"]
],
"shutdown_command": "sync && shutdown -h 0",
"ssh_username": "{{ user `ssh_username` }}",
"ssh_password": "{{ user `ssh_password` }}",
"vnc_bind_address": "0.0.0.0",
"ssh_timeout": "{{ user `ssh_timeout` }}",
"format": "qcow2",
"output_directory": "yms_rom/{{ user `yms_rom_version` }}"
}],
"provisioners": [{
"type": "shell",
"environment_vars": [
"YMS_ROM_URL={{ user `yms_rom_url` }}",
"YMS_ROM_VERSION={{ user `yms_rom_version` }}"
],
"script": "scripts/apollo_install.sh"
}],
"post-processors": [{
"type": "shell-local",
"inline": ["mv yms_rom/{{ user `yms_rom_version` }}/YMS_{{ user `yms_rom_version` }} yms_rom/{{ user `yms_rom_version` }}/YMS_{{ user `yms_rom_version` }}.qcow2"]
}]
}

通过openstack生成glance镜像

  • yms-deploy-openstack.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
"variables": {
"ssh_username": "root",
"ssh_password": "password",
"ssh_timeout": "1h",
"yms_rom_url": "https://10.200.110.68/publish/enterprise/download/YMS",
"yms_rom_version": "21.0.0.12"
},
"builders": [{
"type": "openstack",
"identity_endpoint": "http://10.200.111.250:35357/v3",
"tenant_name": "Operation",
"domain_name": "Default",
"username": "openstack",
"password": "password",
"region": "RegionOne",
"image_name": "YMS_{{ user `yms_rom_version` }}",
"source_image_name": "Centos7.5",
"flavor": "m5.medium",
"insecure": "true",
"instance_name": "YMS_{{ user `yms_rom_version` }}",
"networks": "a6d7a3f7-e3f8-4dbe-8fb9-01dfc96a68d5",
"ssh_username": "{{ user `ssh_username` }}",
"ssh_password": "{{ user `ssh_password` }}",
"ssh_timeout": "{{ user `ssh_timeout` }}"
}],
"provisioners": [{
"type": "shell",
"environment_vars": [
"YMS_ROM_URL={{ user `yms_rom_url` }}",
"YMS_ROM_VERSION={{ user `yms_rom_version` }}"
],
"script": "scripts/apollo_install.sh"
}],
"post-processors": [
{
"type": "manifest",
"output": "openstack_manifest.json",
"strip_path": true
},
{
"type": "shell-local",
"scripts": ["scripts/image_download.bat"]
}]
}
坚持原创技术分享,您的支持将鼓励我继续创作!
0%