Packer 新手入门篇 - 自动创建AMI

86 阅读4分钟

Packer 是 Hashicorp 推出的一款开源 VM 映像创建工具。它可以帮助您自动执行在云 和本地虚拟化环境中创建虚拟机映像的过程。

简而言之,您创建虚拟机映像时执行的任何手动步骤都可以通过简单的 Packer 配置模板 自动完成。您声明所需 VM 映像的状态,Packer 会为您构建它。

Packer 应用场景

  1. 黄金映像创建:使用 packer,您可以模板化黄金 VM 映像所需的配置,该映像可跨组织使用。
  2. 每月 VM 修补:您可以将 Packer 集成到每月 VM 映像修补管道中。
  3. 不可变基础设施:如果您想使用 VM 映像作为可部署工件来创建不可变基础设施,则可以在 CI/CD 管道中使用 Packer。

基本上,所有上述用例都是 CI 管道或 IaaC 代码管道的一部分,其中每个 Packer 模板都受版本控制。 即使是打包器模板的开发或更新也应该在部署到项目环境之前经过所有的 CI 测试。

  1. 在 HCL(Hashicorp 配置语言)或 JSON 文件中声明所有必需的 VM 配置。我们将其称为 Packer 模板。
  2. 要构建 VM 映像,请使用 Packer 模板执行 Packer。
  3. Packer 对远程云提供商进行身份验证并启动服务器。如果您从云环境执行 Packer,它会利用云服 务帐户进行身份验证。
  4. Packer 与服务器建立远程连接(SSH 或 Winrm)。
  5. 然后,它根据您在 Packer 模板中指定的配置程序(Shell 脚本、Ansible、Chef 等)配置服务器。
  6. 注册 AMI
  7. 删除正在运行的实例。

让我们开始设置 Packer。

安装 Packer

您可以在本地工作站或云实例上安装 Packer。在实际项目中,Packer 安装将是 Jenkins 代理或 CI/CD 流程中任何工具的一部分。

步骤 1:从 Packer 下载页面下载最新的 Packer 可执行文件。www.packer.io/downloads.h… 它适用于 Windows、Linux 和其他 Unix 平台。

对于 Linux,您可以从相应的包管理器安装 Packer,也可以从下载按钮获取下载链接并使用 wget 下载。

例如,在 Ubuntu 中

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packer

步骤 2:通过执行 packer 命令来验证 packer 的安装

ninjamac@ip-192-168-1-61 key % packer version
Packer v1.12.0

ninjamac@ip-192-168-1-61 key % packer
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

步骤3: 创建一个vm.pkr.hcl文件:

variable "ami_id" {
  type    = string
  default = "ami-00bdb82d267b6212b"
}

variable "app_name" {
  type    = string
  default = "httpd"
}

locals {
    app_name = "httpd"
}

source "amazon-ebs" "httpd" {
  ami_name      = "PACKER-DEMO-${local.app_name}"
  instance_type = "t2.micro"
  region        = "ap-southeast-2"
  source_ami    = "${var.ami_id}"
  ssh_username  = "ec2-user"
  tags = {
    Env  = "DEMO"
    Name = "PACKER-DEMO-${var.app_name}"
  }
}

build {
  sources = ["source.amazon-ebs.httpd"]

  provisioner "shell" {
    script = "script/script.sh"
  }

  post-processor "shell-local" {
    inline = ["echo foo"]
  }
}

创建一个shell 脚本,包含下面的内容,将文件保存在script文件夹中。

#!/bin/bash
sudo yum -y update
sudo yum install -y httpd

步骤4:配置AWS访问key和密钥。

步骤5:验证packer HCL模版

ninjamac@ip-192-168-1-61 packer-vm % packer validate vm.pkr.hcl 
The configuration is valid.

步骤6: build packer 镜像

ninjamac@ip-192-168-1-61 packer-vm % packer build vm.pkr.hcl 
amazon-ebs.httpd: output will be in this color.

==> amazon-ebs.httpd: Prevalidating any provided VPC information
==> amazon-ebs.httpd: Prevalidating AMI Name: PACKER-DEMO-httpd
    amazon-ebs.httpd: Found Image ID: ami-00bdb82d267b6212b
==> amazon-ebs.httpd: Creating temporary keypair: packer_67e75f1d-fd61-4d8a-3b29-9394e266ff49
==> amazon-ebs.httpd: Creating temporary security group for this instance: packer_67e75f23-eb37-4c5a-54d7-2aed976abcb5
==> amazon-ebs.httpd: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
    amazon-ebs.httpd:
    amazon-ebs.httpd: Complete!
==> amazon-ebs.httpd: Stopping the source instance...
    amazon-ebs.httpd: Stopping instance
==> amazon-ebs.httpd: Waiting for the instance to stop...
==> amazon-ebs.httpd: Creating AMI PACKER-DEMO-httpd from instance i-0961ae7e389c899b4
    amazon-ebs.httpd: AMI: ami-0f3010439ba7f5de4
==> amazon-ebs.httpd: Waiting for AMI to become ready...
    amazon-ebs.httpd (shell-local): foo
Build 'amazon-ebs.httpd' finished after 12 minutes 17 seconds.

==> Wait completed after 12 minutes 17 seconds

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs.httpd: AMIs were created:
ap-southeast-2: ami-0f3010439ba7f5de4

--> amazon-ebs.httpd: AMIs were created:
ap-southeast-2: ami-0f3010439ba7f5de4

过几分钟后,你就可以在console - my AMI中看到你创建的镜像了。

截屏2025-03-29 11.03.25.png

公众号Id.jpg

往期回顾

本文使用 markdown.com.cn 排版