如何在Ubuntu 20.04/18.04/16.04上安装和使用Packer

时间:2020-02-23 14:33:10  来源:igfitidea点击:

如何在Ubuntu 20.04/18.04/16.04上安装packer?
Packer是一个开源工具,用于从单个源配置中为多个平台创建相同的机器图像。
它是一种跨平台命令行和轻量级应用,能够并行地为多个平台生成和高度机器图像。

如何在Ubuntu 20.04/18.04/16.04上安装packer?

封隔器可以从预编译的二进制文件或者源安装。
所有用户的简单和推荐方法是二进制安装方法。
这是本文中使用的。

首先,检查下载页面上的最新版本包。
然后下载最近的平台版本。
由于我们在Ubuntu 20.04/18.04/16.04上执行此操作,从而下载Linux版本。

export VER="1.5.6"
wget https://releases.hashicorp.com/packer/${VER}/packer_${VER}_linux_amd64.zip

解压缩下载的文件。

sudo apt install unzip
unzip packer_${VER}_linux_amd64.zip

移动 packer二进制到 /usr/local/bin目录。

$sudo mv packer /usr/local/bin
$packer version
Packer v1.5.6

安装packer后,验证安装正在运行,通过检查包装器可用:

$packer
Usage: packer [--version] [--help] 
 []
 Available commands are:
     build       build image(s) from template
     fix         fixes templates from old versions of packer
     inspect     see components of a template
     validate    check that a template is valid
     version     Prints the Packer version

如果将packer bi放在不同的目录中,则 PATH变量应包含此目录。

如何使用packer

构建器用于生成图像并为来自模板的各种平台创建计算机。
我们可以看到已支持的构建器列表。
模板是用于定义构建的图像的配置文件,它的格式是JSON。

在下面的示例中,我们将使用VirtualBox Builder创建Ubuntu虚拟机并以OVA或者OVF格式导出。

让我们创建我们的工作目录。

mkdir projects/packer/
cd projects/packer/

创建一个名为 build.json有以下内容

{
    "variables": {
        "hostname": "ubuntu",
        "domain": "local",
        "ssh_user": "packer",
        "ssh_pass": "Hyman@theitroad"
    },
    "builders": [{
        "type": "virtualbox-iso",
        "guest_os_type": "Ubuntu_64",
        "vm_name": "ubuntu-18.04-vbox-template",
        "iso_url": "http://cdimage.ubuntu.com/releases/18.04/release/ubuntu-18.04.2-server-amd64.iso",
        "iso_target_path": "/home/jmutai/iso/ubuntu-18.04.2-server-amd64.iso",
        "iso_checksum": "34416ff83179728d54583bf3f18d42d2",
        "iso_checksum_type": "md5",
        "memory": "1024",
        "cpus": "1",
        "disk_size": "20000",
        "format": "ova",
        "guest_additions_mode": "upload",
        "headless": "false",
        "http_directory": "http",
        "ssh_username": "{{user `ssh_user`}}",
        "ssh_password": "{{user `ssh_pass`}}",
        "ssh_wait_timeout": "20m",
        "shutdown_command": "sudo /sbin/halt -p",
        "boot_command": [
            "<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "/install/vmlinuz<wait>",
            " auto<wait>",
            " console-setup/ask_detect=false<wait>",
            " console-setup/layoutcode=us<wait>",
            " console-setup/modelcode=pc105<wait>",
            " debconf/frontend=noninteractive<wait>",
            " debian-installer=en_US<wait>",
            " fb=false<wait>",
            " initrd=/install/initrd.gz<wait>",
            " kbd-chooser/method=us<wait>",
            " keyboard-configuration/layout=USA<wait>",
            " keyboard-configuration/variant=USA<wait>",
            " locale=en_US<wait>",
            " netcfg/get_domain={{user `domain`}}<wait>",
            " netcfg/get_hostname={{user `hostname`}}<wait>",
            " grub-installer/bootdev=/dev/sda<wait>",
            " noapic<wait>",
            " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
            " -- <wait>",
            "<enter><wait>"
        ]
    }],
    "provisioners": [{
        "type": "shell",
        "inline": [
            "sleep 30",
            "sudo apt update",
            "sudo apt -y install bash-completion wget vim php php-fpm php-mysql"
        ]
    }]
}

更改设置以适应YPUR部署设计。
我们也需要一个 preseed使用packer自动安装Ubuntu 18.04的安装Ubuntu 18.04.

在JSON构建文件中创建一个http目录。

mkdir http

然后粘贴下面 http/preseed.cfg文件。

# Language and Locale
d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i localechooser/preferred-locale string en_US.UTF-8
d-i localechooser/supported-locales en_US.UTF-8
# Hostname/domain
d-i netcfg/get_hostname string ubuntu
d-i netcfg/get_domain string local

# Keyboard
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layout select USA
d-i keyboard-configuration/variant select USA
d-i keyboard-configuration/modelcode string pc105
# Timezone/Time
d-i time/zone string UTC
d-i clock-setup/utc-auto boolean true
d-i clock-setup/utc boolean true
# Server
tasksel tasksel/first multiselect standard, ubuntu-server
# No proxy
d-i mirror/http/proxy string
# Packages Policy
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select full-upgrade
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev
# Partitioning
d-i partman-auto/method string lvm
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/disk string /dev/sda
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm boolean true
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish

# Create packer user account.
d-i passwd/user-fullname string packer
d-i passwd/username string packer
d-i passwd/user-password password Hyman@theitroad
d-i passwd/user-password-again password Hyman@theitroad
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
d-i passwd/user-default-groups packer sudo

如果使用此示例,则需要更改要在Preseed文件中创建的用户的用户名和密码。

通过运行以下命令构建图像。

$packer build build.json